致命错误:未捕获异常'Swift_RfcComplianceException',并显示消息'给出邮箱中的地址

时间:2014-02-05 10:16:37

标签: php email swiftmailer

我真的很难理解以下问题 - 有人能给我一些关于如何解决这个问题的意见吗?我真的走到了尽头。运行以下代码时,出现此错误:

http://pastebin.com/uXB5Kx4s

  

致命错误:未捕获的异常'Swift_RfcComplianceException',并显示消息'给出邮箱中的地址

以下是代码:

<?php
include_once 'Swift-5.0.3/lib/swift_required.php';


if(isset($_POST['button'])) {


    $name = $_POST['name'];
    $email = $_POST['email'];
    $comments = $_POST['comments'];
    $organisation = $_POST['organisation'];
    $number = $_POST['number'];
    $rec_email = 'ben@bubbledesign.co.uk';

    $message = "Comments : " . $comments . "<br>" . "organisation: " . $organisation . "<br>" . "Phone Number: " . $number ;

    $transport = Swift_MailTransport::newInstance();

    $mailer = Swift_Mailer::newInstance($transport);

    $message = Swift_Message::newInstance('Subject')
        ->setFrom($email)
        ->setTo($rec_email)
        ->setBody($comments);

    $result = $mailer->send($message);
}

?>

2 个答案:

答案 0 :(得分:3)

使用无效的电子邮件地址向Swift Mailer提供时,会引发Swift_RfcComplianceException异常(无效,如“不遵循RFC文档中定义的语法”,而不是“帐户不存在” “)。这可能发生在需要电子邮件地址的任何字段中(发件人,收件人,CC ...)。

由于您接受来自不受信任来源的地址,因此您需要确保您的代码能够应对此问题。最简单的方法是捕获异常:

try{
    // ...
}catch(Swift_RfcComplianceException $e){
    // Assume $_POST['email'] is invalid, abort processing, notify the user
}

...但是,为了确保错误的地址正好是$_POST['email'],您可以事先验证它:

if( !Swift_Validate::email($_POST['email']) ){
    // $_POST['email'] *is* invalid, abort processing, notify the user
}

答案 1 :(得分:-1)

简便方法检查

if(!Swift_Validate::email($email))
            { 
                 $msg    = "Email is not valid";  
                 $status = "error";  
            }
            else
            {    
                //Create the Transport 
                $transport = Swift_SmtpTransport::newInstance('smtp.mandrillapp.com', 587)
                                  ->setUsername('nishrit@stalkbuylove.com')
                                 ->setPassword('Tks9tkeU5PZIqHU-UVSZLw');
                $mailer   = Swift_Mailer::newInstance($transport);
                //Create a message 
                $message  = Swift_Message::newInstance($subject)
                               ->setFrom(array('contact@stalkbuylove.com' => 'StalkBuyLove'))
                               ->setTo($to)
                              ->setBody($body, 'text/html');
                //Send the message 
                $result   = $mailer->send($message);
                if($result>0):
                     $msg    = "Email send successfully ";  
                     $status = "success";  
                else:
                    $msg    = "Error in send email. Please try again";  
                    $status = "error";  
                endif;
            }
            $array  = array("status"=>$status,'msg' => $msg); 
            return $array;