我想为使用格式
的邮件系统创建一个循环stan.smith@americandadcia.fx, Stan Smith
这样我就可以通过我的函数传递它
function SendEmail($to,$fromName, $subject, $message)
{
date_default_timezone_set('Etc/UTC');
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = smtp_host;
$mail->Port = smtp_port;
$mail->SMTPSecure = smtp_protocol;
$mail->SMTPAuth = true;
$mail->Username = smtp_user;
$mail->Password = smtp_pass;
$mail->setFrom(smtp_user, $fromName);
$mail->addReplyTo(smtp_user, $fromName);
$mail->addAddress($to);
$mail->Subject = $subject;
$mail->msgHTML($message);
$mail->send();
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
}
这段代码是经过一个循环,所以说你有 斯坦史密斯,罗杰史密斯和弗朗辛史密斯的形式输入将有
stan.smith@americandad.fx, Stan Smith; roger.smith@amongyou.ufo, Roger Smith; francine.smith@desperatehousewives.fox, Francine Smith
然后地址将被传递到我的函数文件中,并通过该函数运行以发送电子邮件
$mail = new MailSystem();
$emails=$_POST['emailTo'];
$email=explode(";",$emails);
foreach($email as $address) {
echo $mail->SendEmail($address, SITE_NAME." Newsletter", $_POST['emailSubject'], $_POST['emailBody']);
}
我的问题是,当电子邮件尝试发送邮件时,我会尝试运行$mail->addAddress($address)
并收到回复信息,表明它是无效的地址。我可以进入并使用my email", "My name"
手动设置addAddress,但是当它通过这种方式时,它会解析为无效地址。
答案 0 :(得分:2)
在addAddress()
上,您必须拆分接收者姓名和地址。您的功能应如下所示:
function SendEmail($to,$fromName, $subject, $message)
{
date_default_timezone_set('Etc/UTC');
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = smtp_host;
$mail->Port = smtp_port;
$mail->SMTPSecure = smtp_protocol;
$mail->SMTPAuth = true;
$mail->Username = smtp_user;
$mail->Password = smtp_pass;
$mail->setFrom(smtp_user, $fromName);
$mail->addReplyTo(smtp_user, $fromName);
//split the to on , to get mail address and receiver name.
$address = explode(',', $to);
//check if the name is available.
if (!isset($address[1])) {
$mail->addAddress($address[0]);
} else {
$mail->addAddress($address[0], $address[1]);
}
$mail->Subject = $subject;
$mail->msgHTML($message);
//remove the following line because duplicate sending!!!
//$mail->send();
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
}