使用swiftmailer发送邮件的PHP问题

时间:2013-12-22 19:01:37

标签: php email pdf

所以我在发送带有PHP邮件库的邮件时遇到了一些问题 - swiftmailer。 我想发送一些带有pdf附件的邮件,我已经用http://swiftmailer.org/pdf/Swiftmailer.pdf学习了swiftmailer的一些bacis,但是我得到的错误很少,我不知道如何解决它们。

我的PHP代码:

 <?php
 require_once 'lib/swift_required.php';
 if (isset ( $_POST ['email'] )) {
$email = $_POST ['email'];
setlocale ( LC_ALL, 'sl_SI.UTF-8' ); // with this I include so I can use č,š in ž
$array = array (
     "c",
     "č",
     "s",
     "š",
     "z",
     "ž"
      );
     usort ( $array, 'strcoll' );

     $subject = "Name of the mail";
     $message = "Dear sir or madame, this is my email message and you have some pdf attachments";

//the code of library
$message = Swift_Message::newInstance()
->setSubject($subject)
->setFrom(array('robertp@gmail.com' => 'Robert P'))
->setTo(array($email => 'Reciver'))
->setBody(->attach(Swift_Attachment::fromPath('home/public_html/Attachment1.pdf')->setFileName('Name of my PDF file1.pdf'))
          ->attach(Swift_Attachment::fromPath('home/public_html/Attachment2.pdf')->setFileName('Name of my PDF file2.pdf'));

 echo "The mail has been send";
 }

 ?>

我的问题:

 Fatal error: Uncaught exception 'Swift_RfcComplianceException' with message 'Address in mailbox 
 given [] does not comply with RFC 2822, 3.6.2.' 
 in /home/public_html/lib/classes/Swift/Mime/Headers/MailboxHeader.php:352 Stack trace: 

 #0 /home/public_html/Flib/classes/Swift/Mime/Headers/MailboxHeader.php(264):                     
 Swift_Mime_Headers_MailboxHeader->_assertValidAddress('') 
 #1 /home/public_html/lib/classes/Swift/Mime/Headers/MailboxHeader.php(108): 
 Swift_Mime_Headers_MailboxHeader->normalizeMailboxes(Array) 
 #2 /home/public_html/lib/classes/Swift/Mime/Headers/MailboxHeader.php(65): 
 Swift_Mime_Headers_MailboxHeader->setNameAddresses(Array) 
 #3 /home/public_html/lib/classes/Swift/Mime/SimpleHeaderFactory.php(60): 
 Swift_Mime_Headers_MailboxHeader->setFieldBodyModel(Array) 
 #4 /home/public_html/lib/classes/Swift/Mime/SimpleHeaderSet.php(70): 
 Swift_Mime_SimpleHeaderFactory->createMailboxHeader('To', Array) 
 #5 /home/ in /home/public_html/lib/classes/Swift/Mime/Headers/MailboxHeader.php on line 352

1 个答案:

答案 0 :(得分:-1)

array($email => 'Reciver')问题是Reciver不是有效的电子邮件地址。 something@like.this有效。所以尝试像array($email => 'something@like.this')

这样的东西