1

我在这里遇到了一个小问题,当我尝试使用 swiftmailer 发送电子邮件时,出现错误。

这是我的代码:

<?php

session_start();
include("config.php");
$dir = dirname(__FILE__);
$session = $_SESSION[serialize];

// Load the SwiftMailer files
        require_once($dir.'/swift/swift_required.php');

$hold = 'TIDAK';
// Kondisi - Kondisi yang ada
if($_GET[phase]=='confirm'){
        $to = "test@127.0.0.1";
        $kalimat = "Invoice Dengan nomor ".$_GET[nosc]." Telah di confirm, mohon di cek di sistem";
        $kalimat2 = "Berikut terlampir data Invoice Customer , mohon di pastikan.";
        $title = "CONFIRMATION NOTIFICATION";

}                

                // Convert string dlu



        $mailer = new Swift_Mailer(new Swift_MailTransport()); // Create new instance of SwiftMailer
                $message = Swift_Message::newInstance()
                                           ->setSubject($title) // Message subject
                       ->setTo(array($to)) // Array of people to send to
                       ->setBcc(array($bcc)) // Bcc:
                                           ->setFrom(array($_SESSION[mailadmin] => $_SESSION[namauser].''.store('namasite'))) // From:
                                           ->setBody($kalimat, 'text/html'); // Attach that HTML message from earlier



        // Send the email, and show user message
                if($hold<>'YA'){
                    if ($mailer->send($message)){
            echo json_encode(array('success'=>true));
                    } else {
            echo json_encode(array('success'=>false));
                    }
                }
?>

然后当我尝试通过 jquery 将数据发布到这个文件时,我得到了一个错误,这个错误是我从 firebug 得到的。

<br />
<b>Fatal error</b>:  Uncaught exception 'Swift_RfcComplianceException' with message 'Address in mailbox given [] does not comply with RFC 2822, 3.6.2.' in C:\xampp\htdocs\Colosus\core\swift\classes\Swift\Mime\Headers\MailboxHeader.php:319
Stack trace:
#0 C:\xampp\htdocs\Colosus\core\swift\classes\Swift\Mime\Headers\MailboxHeader.php(249): Swift_Mime_Headers_MailboxHeader-&gt;_assertValidAddress(NULL)
#1 C:\xampp\htdocs\Colosus\core\swift\classes\Swift\Mime\Headers\MailboxHeader.php(107): Swift_Mime_Headers_MailboxHeader-&gt;normalizeMailboxes(Array)
#2 C:\xampp\htdocs\Colosus\core\swift\classes\Swift\Mime\Headers\MailboxHeader.php(71): Swift_Mime_Headers_MailboxHeader-&gt;setNameAddresses(Array)
#3 C:\xampp\htdocs\Colosus\core\swift\classes\Swift\Mime\SimpleHeaderFactory.php(74): Swift_Mime_Headers_MailboxHeader-&gt;setFieldBodyModel(Array)
#4 C:\xampp\htdocs\Colosus\core\swift\classes\Swift\Mime\SimpleHeaderSet.php(87): Swift_Mime_SimpleHeaderFactory-&gt;createMailboxHeader('Bcc', Array)
#5 C:\xampp\htdocs\Colosus\core\swift\classes\Swift\Mime in <b>C:\xampp\htdocs\Colosus\core\swift\classes\Swift\Mime\Headers\MailboxHeader.php</b> on line <b>319</b><br />

所以,伙计们,有人可以在这里帮助我吗?我真的不知道这里发生了什么。

4

2 回答 2

13

如果您在发送指令周围使用 try/catch 会更好:

try {
     if ($mailer->send($message)){
        ...
     }

} catch (Exception $e) {
    $log_error->write_error($e->getMessage() . ';');
}

或者,如果您在发送之前验证电子邮件:

if(!Swift_Validate::email($email)){ //if email is not valid
                //do something, skip them or log them
                $log_error->log($email);
}
于 2013-07-16T14:06:43.433 回答
1

Please make sure there is no white space before or after the email address given.

于 2015-07-24T15:36:48.767 回答