zend框架2中的邮件异常处理

时间:2013-09-18 12:02:43

标签: exception-handling zend-framework2 sendmail

我正在尝试使用Mail \ Transport \ Sendmail()在zend框架2中发送电子邮件。 它在无效的电子邮件ID上显示运行时异常。

错误消息是“无法发送邮件:mail():SMTP服务器响应:550 5.1.1 ...用户未知”

的Zend \邮件\异常\ RuntimeException的

$ options = new Mail \ Transport \ SmtpOptions($ config_setting);

    // render Email Content
    $this->renderer = $this->getServiceLocator()->get('ViewRenderer');
    $content = $this->renderer->render($config['mail']['templates']. $messageTemplate, $messageParam);

    // make a header as html
    $html = new MimePart($content);
    $html->type = $config['mail']['content_type'];
    $body = new MimeMessage();
    $body->setParts(array($html,));

    // instance mail
    $mail = new Mail\Message();
    $mail->setBody($body); // will generate our code html from template.phtml
    $mail->setFrom($config['mail']['from_email'],$config['mail']['from_name']);
    $mail->setTo($mailTo);
    $mail->setSubject($subject);
    //$transport = new Mail\Transport\Smtp($options);
    $transport = new Mail\Transport\Sendmail();

    try{
        $response = @$transport->send($mail);
        return $response;
    }
    catch(Zend\Mail\Exception\RuntimeException $ex)
    {
        $ex->getMessage();
        $response = array('error' => 1, 'msg' => $ex->getMessage());
        //return $response;
    }

我想忽略此异常消息。

1 个答案:

答案 0 :(得分:1)

您有两个传输,一个Sendmail和一个SMTP。 Sendmail是你电脑上的内部邮件服务器,应该可以正常工作。 SMTP是允许(可能的外部)邮件服务器发送电子邮件的协议。您可以使用自己的SMPT服务器,也可以连接到例如Google或Hotmail通过SMTP发送邮件。

你的代码中有这个:

//$transport = new Mail\Transport\Smtp($options);
$transport = new Mail\Transport\Sendmail();

所以您在代码中使用Sendmail传输,但例外来自SMTP:

  

无法发送邮件:mail():SMTP服务器响应:550 5.1.1 ...用户未知

因此,在我看来,您在代码中意外地使用了SMTP。切换到Sendmail,它应该可以正常工作,或者使用您发布的代码再次检查您的问题。