简体   繁体   English

PHP mail()未发送,但未引发异常

[英]PHP mail() not sending, but no exception thrown

I'm using CakePHP to send an email. 我正在使用CakePHP发送电子邮件。 My controller code looks like: 我的控制器代码如下:

        if ($this->User->save($this->request->data)) {
            $email = new CakeEmail();
            $email->from(array('noreply@mydomain.com' => 'My Domain'));
            $email->to($this->request->data['User']['email']);
            $email->subject('My Domain Confirmation');
            $email->replyTo('noreply@mydomain.com');
            $email->sender('noreply@mydomain.com', 'My Domain');
            $email->emailFormat('html');
            $email->template('confirmation');
            $email->send();
            $email->viewVars(array(
                'name' => $this->request->data['User']['username'],
                'id' => $this->User->getLastInsertID(),
                'code' => $this->request->data['User']['confirm_code']));
        }

I also included at the top of this controller: App::uses('CakeEmail', 'Network/Email'); 我还包括了该控制器的顶部: App::uses('CakeEmail', 'Network/Email');

If I print_r on $email->send(), I get: 如果我在$ email-> send()上进行print_r,则会得到:

Array
(
    [headers] => From: My Domain
Reply-To: noreply@mydomain.com
X-Mailer: CakePHP Email
Date: Thu, 23 Feb 2012 00:40:00 -0800
Message-ID: <4f45fb60a0fc46cd926f305a32396397@mydomain.com>
MIME-Version: 1.0
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 8bit
    [message] => 



    Hi there,


Welcome to my site! While you can now vote on submissions and leave comments, your own submissions will be screened and not appear to the public until you click on the confirmation link below:

Click here to confirm your account

We hope to see you around and thanks for joining the community!

So it's obviously using my html email template and passing the right variables to it, and throwing no exceptions. 因此,显然使用我的html电子邮件模板并将正确的变量传递给它,并且不引发异常。 So I decided to just do a basic mail() test within one of my view files eg: 因此,我决定只在一个视图文件中进行一个基本的mail()测试,例如:

$to = "mytestemail@example.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse@example.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>

Which echoed "Mail Sent.", but nothing actually came to my mailbox. 它回显“已发送邮件”,但实际上没有任何内容进入我的邮箱。 I checked my file in /var/spool/mail/root and the last email sent was on the same server on Jan. 9, 2012. So it's definitely worked before. 我在/var/spool/mail/root检查了我的文件,最后发送的电子邮件是在2012年1月9日在同一台服务器上。因此以前肯定可以使用。 I just recently upgraded to Cake 2.0, but this doesn't explain why plain ol' mail() isn't working. 我最近才升级到Cake 2.0,但这不能解释为什么普通ol'mail mail()无法正常工作。

What other debugging methods can I check to make sure it's not my server preventing the email from being sent? 我还能检查哪些其他调试方法以确保不是服务器阻止电子邮件发送?

PHP's mail() won't throw any exceptions. PHP的mail()不会引发任何异常。 You need to check the return status. 您需要检查退货状态。 If that's false, then your MTA isn't accepting mail. 如果那是错误的,则说明您的MTA不接受邮件。 Even if it returns true, that doesn't actually mean much of anything . 即使返回true, 也实际上没有任何意义

Take a look at the mail logs in /var/log/. 看一下/ var / log /中的邮件日志。 Hopefully those can help you figure out more. 希望这些可以帮助您找到更多。

also check your firewall settings. 还要检查您的防火墙设置。 sometimes it stops all outgoing smtp requests if not from specific sources. 有时,如果不是来自特定来源,它将停止所有传出的smtp请求。

The server you are running your Cake app on probably requires (SMTP) authentication before it allows you to send anything, which is a pretty common configuration. 在运行Cake应用程序的服务器上,可能需要先进行(SMTP)身份验证,然后才能发送任何内容,这是一种非常常见的配置。

Copy the app/Config/email.php.default file to app/Config/email.php and adjust it to match your setup (usually it's just localhost and you can use one of your mailbox logins for authentication). app/Config/email.php.default文件复制到app/Config/email.php并进行调整以匹配您的设置(通常它只是localhost,并且您可以使用邮箱登录名之一进行身份验证)。

Also see the book on this subject. 另请参阅有关此主题的书

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM