检查wp_mail是否正常工作?
-
-
现在剩下的问题是:什么是mv_mail_token(),它返回什么,常量sender_signature从哪里来?它包含什么?Now the remaining questions are: What is `mv_mail_token()` and what does it return and where does the constant `sender_signature` come from and what does it contain?
- 0
- 2013-06-23
- kaiser
-
Kaiser,听着,请尝试回答问题.wp_mail_token()返回一个字符串,而sender_signature很好.Kaiser, listen, please try to answer the questions ... wp_mail_token() returns a string and sender_signature does at well.
- 1
- 2013-06-24
- helle
-
[调试要点](https://gist.github.com/franz-josef-kaiser/4063197)[debug gist](https://gist.github.com/franz-josef-kaiser/4063197)
- 2
- 2013-06-24
- kaiser
-
5 个回答
- 投票数
-
- 2015-11-01
Wordpress依靠PHPMailer类通过PHP的
mail
函数发送电子邮件.由于PHP的
mail
函数在执行后返回的信息很少(仅TRUE或FALSE),因此我建议暂时将您的mv_optin_mail
函数最小化,以查看是否wp_mail
函数起作用.示例:
$mailResult = false; $mailResult = wp_mail( '[email protected]', 'test if mail works', 'hurray' ); echo $mailResult;
由于您已经测试了PHP的
mail
函数,因此该邮件应会到达.否则,问题出在函数的其他语句或PHPMailer类中.
在这种情况下,我通常将函数重命名为:
function mv_optin_mail_backup( $id, $data ) {
并添加一个具有相同名称的临时函数,像这样:
function mv_optin_mail( $id, $data ) { $mailResult = false; $mailResult = wp_mail( '[email protected]', 'test if mail works', 'hurray' ); echo $mailResult; }
确定问题出在哪里后,我再次开始使用备份版本.
要直接使用PHPMailer发送邮件,您可以执行以下操作(不用于生产,仅用于调试):
add_action( 'phpmailer_init', 'my_phpmailer_example' ); function my_phpmailer_example( $phpmailer ) { $phpmailer->isSMTP(); //$phpmailer->Host = 'smtp.example.com'; // $phpmailer->SMTPAuth = true; // Force it to use Username and Password to authenticate $phpmailer->Port = 25; // $phpmailer->Username = 'yourusername'; // $phpmailer->Password = 'yourpassword'; // Additional settings… //$phpmailer->SMTPSecure = "tls"; // Choose SSL or TLS, if necessary for your server $phpmailer->setFrom( "[email protected]", "From Name" ); $phpmailer->addAddress( "[email protected]", "Your name" ); $phpmailer->Subject = "Testing PHPMailer"; $phpmailer->Body = "Hurray! \n\n Great."; if( !$phpmailer->send() ) { echo "Mailer Error: " . $phpmailer->ErrorInfo; } else { echo "Message sent!"; } }
Wordpress relies on the PHPMailer class to send email through PHP's
mail
function.Since PHP's
mail
function returns very little information after execution (only TRUE or FALSE), I suggest temporarily stripping down yourmv_optin_mail
function to a minimum in order to see if thewp_mail
functions works.Example:
$mailResult = false; $mailResult = wp_mail( '[email protected]', 'test if mail works', 'hurray' ); echo $mailResult;
Since you've tested PHP's
mail
function already, the mail should arrive.If it does not, the problem lies in the other statements of your function or in the PHPMailer class.
In cases like this, I usually rename my function to something like:
function mv_optin_mail_backup( $id, $data ) {
And add a temporary function with the same name to mess around with like so:
function mv_optin_mail( $id, $data ) { $mailResult = false; $mailResult = wp_mail( '[email protected]', 'test if mail works', 'hurray' ); echo $mailResult; }
When I have figured out what the problem is, I start using the backup version again.
To send a mail using PHPMailer directly you can do something like this (not for production, just for debugging):
add_action( 'phpmailer_init', 'my_phpmailer_example' ); function my_phpmailer_example( $phpmailer ) { $phpmailer->isSMTP(); //$phpmailer->Host = 'smtp.example.com'; // $phpmailer->SMTPAuth = true; // Force it to use Username and Password to authenticate $phpmailer->Port = 25; // $phpmailer->Username = 'yourusername'; // $phpmailer->Password = 'yourpassword'; // Additional settings… //$phpmailer->SMTPSecure = "tls"; // Choose SSL or TLS, if necessary for your server $phpmailer->setFrom( "[email protected]", "From Name" ); $phpmailer->addAddress( "[email protected]", "Your name" ); $phpmailer->Subject = "Testing PHPMailer"; $phpmailer->Body = "Hurray! \n\n Great."; if( !$phpmailer->send() ) { echo "Mailer Error: " . $phpmailer->ErrorInfo; } else { echo "Message sent!"; } }
-
- 2017-05-21
您可以使用" wp_mail_failed"操作来捕获发送错误.
https://developer.wordpress.org/reference/hooks/wp_mail_failed/
You can use the 'wp_mail_failed' action to catch a send error.
https://developer.wordpress.org/reference/hooks/wp_mail_failed/
-
- 2015-11-01
我通常要测试
wp_mail()
是否正确发送电子邮件的方法是,在我的网站上注册另一个电子邮件地址,然后查看电子邮件是否到达.如果是这样,则意味着WordPress正在正确发送电子邮件(它使用wp_mail()
发送注册电子邮件),并且您应该检查邮件发送功能是否有错误.为此,正如@Tobias建议的那样,您应该从邮件发送功能中删除所有内容,而只保留基本内容:function wpse_100047() { echo wp_mail( '[email protected]', 'WP Mail Test', 'Mail is working' ); }
此外,WordPress发送的电子邮件(如PHP的
mail()
函数发送的所有电子邮件)可能会被某些电子邮件服务器阻止(或标记为垃圾邮件),因此始终在实时网站上对电子邮件使用SMTP(执行此操作的多个插件)是一个好主意.What I usually do to test if
wp_mail()
is sending e-mails properly is just registering on my website with another e-mail address and seeing if the e-mail arrives. If it does, it means that WordPress is properly sending e-mails (it useswp_mail()
to send registration e-mails) and you should inspect your mail sending function for any errors. To do that, as @Tobias suggested, you should strip everything from your mail sending function and only leave the basic:function wpse_100047() { echo wp_mail( '[email protected]', 'WP Mail Test', 'Mail is working' ); }
Additionally, the e-mails sent by WordPress (as all e-mails sent by PHP's
mail()
function might be blocked by some e-mail servers (or marked as spam) so it's always a good idea to use SMTP (multiple plugins that do that) for e-mails on the live website. -
- 2014-04-22
我首先要在wp-config中启用 WP_DEB ,然后看看是否能显示有关您的代码的任何信息或wp_mail函数的代码.这就是使用WP进行开箱即用的调试.
此外,您可以使用 Easy WP SMTP 并启用调试和/或进行设置使用SMTP.WordPress.org上也有类似的插件,但我知道这是一个不错的调试选项.如果类似GMail的方法有效,那么您会知道这是服务器设置,而不是此代码.
I would start by enabling WP_DEBUG in wp-config and see if that shows you anything about your code or the code for the wp_mail function. That is about it for debugging right out of the box with WP.
Also, you can use Easy WP SMTP and enable debugging and/or set that up to use SMTP. There are similar plugins on WordPress.org but I know this one has a good debug option. If something like GMail works then you'll know it is a server setting and not this code.
-
- 2020-04-14
该文档中有非常有趣的建议.
https://github.com/PHPMailer/PHPMailer/wiki/疑难解答 特别是与Azure虚拟机一起使用时,SELinux配置阻止了传出连接.
如果您看到类似的错误
SMTP -> ERROR: Failed to connect to server: Permission denied (13)
,您可能会遇到SELinux阻止PHP的情况 或Web服务器发送电子邮件.特别是在 RedHat/Fedora/Centos.使用getsebool
命令,我们可以检查 httpd守护程序被允许通过网络建立连接,并且 发送电子邮件:getsebool httpd_can_sendmail getsebool httpd_can_network_connect
此命令将打开或关闭布尔值.如果关闭,我们可以将其打开:
sudo setsebool -P httpd_can_sendmail 1 sudo setsebool -P httpd_can_network_connect 1
如果您通过fastcgi运行PHP-FPM,则您 可能需要将此应用到fpm守护程序而不是httpd.
Very interesting suggestions were in this document.
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
In particular working with Azure Virtual Machine the SELinux config was blocking the outgoing connections.
If you see an error like
SMTP -> ERROR: Failed to connect to server: Permission denied (13)
, you may be running into SELinux preventing PHP or the web server from sending email. This is particularly likely on RedHat / Fedora / Centos. Using thegetsebool
command we can check if the httpd daemon is allowed to make a connection over the network and send an email:getsebool httpd_can_sendmail getsebool httpd_can_network_connect
This command will return a boolean on or off. If it's off, we can turn it on:
sudo setsebool -P httpd_can_sendmail 1 sudo setsebool -P httpd_can_network_connect 1
If you're running PHP-FPM via fastcgi, you may need to apply this to the fpm daemon rather than httpd.
我正在尝试使用
wp_mail
(在本地计算机上测试),但是没有收到邮件.php.ini
设置了smtp_port = 25
,并且phpmail()
迄今可以正常工作.这是我的邮件功能的代码:
我没有任何错误.有没有办法为wordpress切换错误登录?
noreply_address
是noreply@root