-1

我的数据库中有一张表,我有通过电子邮件发送的味精

这是我的代码

    <?php
require_once 'lib/swift_required.php';

$con=mysqli_connect("localhost","root","","ex_smartcard");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$query = mysqli_query($con,"Select autosend.to,
  autosend.cc,
  autosend.bcc,
  autosend.subject,
  autosend.msg,
  autosend.start,
  autosend.expiry,
  autosend.freq,
  autosend.timestamp
From autosend
Where autosend.start <= CurDate() And autosend.expiry >= CurDate() And
  autosend.freq = 3");
while($row = mysqli_fetch_assoc($query))

{

$to = $row['to'];
$cc = $row['cc'];
$subject = $row['subject'];
$emailBody="";
$emailBody .= $row['msg'];




// Create the Transport
$transport = Swift_SmtpTransport::newInstance('192.168.0.1', 25)
  ->setUsername('smartcard@example.com')
  ->setPassword('123456')
  ;

/*
You could alternatively use a different transport such as Sendmail or Mail:

// Sendmail
$transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs');

// Mail
$transport = Swift_MailTransport::newInstance();
*/

// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);

// Create the message
$message = Swift_Message::newInstance()

  // Give the message a subject
  ->setSubject($subject)

  // Set the From address with an associative array
  ->setFrom(array('smartcard@lotus.edu.in' => 'Lotus Reminder'))

  // Set the To addresses with an associative array
  ->setTo(array($to))

  // Using setCc() to set all recipients in one go
  ->setCc(array($cc))


  // Give it a body
  ->setBody($emailBody)



  ;

$headers = $message->getHeaders();
// Send the message
$result = $mailer->send($message);
printf("Sent %d messages\n", $result);
echo $cc;

}

?>

$to = email1@example.com $cc = email2@example.com, email3@example.com

我收到此错误

Fatal error: Uncaught exception 'Swift_RfcComplianceException' with message 'Address in mailbox given [email1@example.com, email2@example.com] does not comply with RFC 2822, 3.6.2.' in C:\xampp\htdocs\PHPMailer\Swift\lib\classes\Swift\Mime\Headers\MailboxHeader.php:352 Stack trace: #0 C:\xampp\htdocs\PHPMailer\Swift\lib\classes\Swift\Mime\Headers\MailboxHeader.php(264): Swift_Mime_Headers_MailboxHeader->_assertValidAddress('ishan@lotus.edu...') #1 C:\xampp\htdocs\PHPMailer\Swift\lib\classes\Swift\Mime\Headers\MailboxHeader.php(108): Swift_Mime_Headers_MailboxHeader->normalizeMailboxes(Array) #2 C:\xampp\htdocs\PHPMailer\Swift\lib\classes\Swift\Mime\Headers\MailboxHeader.php(65): Swift_Mime_Headers_MailboxHeader->setNameAddresses(Array) #3 C:\xampp\htdocs\PHPMailer\Swift\lib\classes\Swift\Mime\SimpleHeaderFactory.php(60): Swift_Mime_Headers_MailboxHeader->setFieldBodyModel(Array) #4 C:\xampp\htdocs\PHPMailer\Swift\lib\classes\Swift\Mime\SimpleHeaderSet.php(70): Swift_Mime_SimpleHeaderFactory->createMailboxHeader('Cc', A in C:\xampp\htdocs\PHPMailer\Swift\lib\classes\Swift\Mime\Headers\MailboxHeader.php on line 352

告诉我如何发送它们我在 cc 列中有许多用逗号分隔的邮件,我应该更改 mysql 中邮件的格式或更改代码。

4

2 回答 2

1

您是否尝试过将多个值作为数组传递,例如:

$cc = array('email1@example.com', 'email2@example.com');

请参阅文档的地址语法小节。

如果可行,并且您希望将从数据库中获取的逗号分隔字符串转换为数组,那么该explode()函数应该会有所帮助。

编辑:

也许是这样的:

$cc = array_map('trim', explode(',', $row['cc']));

其中 explode 通过逗号将字符串拆分为一个数组,该函数将该array_map()函数应用于trim()数组的每个元素,删除两端的任何剩余空格。

于 2013-09-23T17:43:29.903 回答
0

cc 必须以有效的数组形式传递

$array = array("email1@example.com", "email2@example.com");
$subject = "hrere";
$message = new Swift_Message($subject);
$message->setCc($array);
于 2013-09-23T17:46:03.920 回答