我的数据库中有一个表格,我有通过电子邮件发送的消息
这是我的代码
<?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中的邮件格式还是更改代码。
答案 0 :(得分:1)
您是否尝试将多个值作为数组传递,例如:
$cc = array('email1@example.com', 'email2@example.com');
请参阅文档的Syntax for Addresses小节。
如果可行,并且您希望将数据库中的逗号分隔字符串转换为数组,则explode()
函数应该有帮助。
编辑:
可能是这样的:
$cc = array_map('trim', explode(',', $row['cc']));
其中explode将字符串拆分为逗号上的数组,array_map()
函数将trim()
函数应用于每个数组的元素,删除任何一端的剩余空格。
答案 1 :(得分:0)
cc必须以有效的数组形式传递
$array = array("email1@example.com", "email2@example.com");
$subject = "hrere";
$message = new Swift_Message($subject);
$message->setCc($array);