2014-02-05 58 views
2

我真的很難理解以下問題 - 任何人都可以給我一些關於如何解決這個問題的輸入?我真的在這方面陷入死衚衕。當運行下面的代碼,我得到這個錯誤:致命錯誤:未知的異常'Swift_RfcComplianceException'帶有消息'地址在郵箱給出

http://pastebin.com/uXB5Kx4s

Fatal error: Uncaught exception 'Swift_RfcComplianceException' with message 'Address in mailbox given

下面是代碼:

<?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 = '[email protected]'; 

    $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); 
} 

?> 
+0

這聽起來像'$ _POST ['email']'中的無效電子郵件地址。 –

+0

這已經修復了它,在我的結尾真的很愚蠢的錯誤。非常感謝你 – user3274696

+0

很酷。我已經寫了一個正確的答案。 –

回答

3

Swift_RfcComplianceException異常被拋出時,你給斯威夫特梅勒用無效E-郵件地址(無效,因爲「不符合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

簡單的方法來檢查

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('[email protected]') 
           ->setPassword('Tks9tkeU5PZIqHU-UVSZLw'); 
       $mailer = Swift_Mailer::newInstance($transport); 
       //Create a message 
       $message = Swift_Message::newInstance($subject) 
           ->setFrom(array('[email protected]' => '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; 
+0

這是你的真實SMTP用戶名和密碼嗎?您可能想要更改... –

相關問題