我必须使用PhpMailer发送多封邮件。以下是我的代码:
<?php
include 'class.phpmailer.php';
$friendsarr=array('test1@gmail.com','test2@example.com');
$mail = new PHPMailer();
$mail->From = 'test3@test.com';
$mail->FromName = 'CK';
$mail->Subject = 'FW:'.'Test';
$mail->AltBody = '';
$i=0;
foreach ($friendsarr as $value) {
$mail2 = clone $mail;
$friendmail=$value;
$message='<table border="0" align="left" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td height="28">
<div>
<div align="right"><a href="http://integrationyantra.com/en/?join=1" style=""><strong>Subscribe Me!</strong></a></div>
<div align="left">Dear Recipient,<br/>CK has forwarded this email to you with the following message: test</div><br/><div><strong>Please Note: </strong>You have <strong>NOT</strong> been added to any email lists. If you no longer wish to recieve these messages, please contact <a href="#">test4@test.com</a>.</div>
<br/><div align="left">test here'.$i.'</div>
</div>
</td>
</tr>
</tbody>
</table>';
$mail2->MsgHTML($message);
$mail2->AddAddress($value, 'Recipient');
$sent=$mail2->Send();
echo $sent.' '.$value.'<br/>';
$mail2->ClearAddresses();
$i++;
}
?>
值$sent
显示1
,但邮件不会发送到任何邮件地址。也没有错误。谁能告诉我缺少什么?
答案 0 :(得分:1)
尝试此功能
我们已经创建了一个函数,因此它可以工作
/* send_mail
* function definition to send mail by phpmailer function
* @param $to, $body, $subject, $fromaddress, $fromname
* @return boolean true/false
*/
require(DOC_ROOT."/class.phpmailer.php");
function send_mail($to, $body, $subject, $fromaddress='', $fromname=''){
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->WordWrap = 50; // set word wrap to 50 characters
$mail->IsHTML(true); // set email format to HTML
if($fromname!='') {
$mail->FromName = $fromname;
} else {
$mail->FromName = "Site Name";
}
if($fromaddress!='') {
$mail->From = $fromaddress;
} else {
$mail->From = "info@test.com";
}
$mail->AddAddress($to); // set receiver email from here
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AltBody = strip_tags($body);
if(!$mail->Send())
{
//echo "Message could not be sent. <p>";
//echo "Mailer Error: " . $mail->ErrorInfo;
return false;
} else {
//print_r($mail);
//echo "Message has been sent";
return true;
}
}
@send_mail($to, $body, $subject, $from, $fromname);
答案 1 :(得分:1)
这听起来像是一个环境问题,但我已经修复了代码中的几个问题 - 没有必要在循环中克隆PHPMailer实例!
默认情况下,它使用PHP mail()函数发送消息,因此它将通过您的sendmail二进制文件提交给localhost。使用该路线时,您无法获得调试信息的方式 - 您可以通过使用SMTP获得更多(尽管速度较慢) - 只需将呼叫更改为IsMail()
至IsSMTP()
即可将获得大量的调试输出。要测试通过mail()发送,您可以将php.ini中的sendmail路径设置为指向PHPMailer测试文件夹中的fakesendmail.sh
脚本,它只会将您的消息传递到临时文件夹中。
除此之外,您应该检查邮件服务器日志(通常是/var/log/mail.log
)。
<?php
require 'PHPMailerAutoload.php';
$friendsarr=array('test1@gmail.com','test2@example.com');
$mail = new PHPMailer;
$mail->SMTPDebug = 3;
$mail->isMail();
$mail->Host = 'localhost';
$mail->Port = 25;
$mail->From = 'test3@test.com';
$mail->FromName = 'CK';
$mail->Subject = 'FW:'.'Test';
$mail->AltBody = '';
$i=0;
foreach ($friendsarr as $value) {
$message='<table border="0" align="left" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td height="28">
<div>
<div align="right"><a href="http://integrationyantra.com/en/?join=1" style=""><strong>Subscribe Me!</strong></a></div>
<div align="left">Dear Recipient,<br/>CK has forwarded this email to you with the following message: test</div><br/><div><strong>Please Note: </strong>You have <strong>NOT</strong> been added to any email lists. If you no longer wish to recieve these messages, please contact <a href="#">test4@test.com</a>.</div>
<br/><div align="left">test here'.$i.'</div>
</div>
</td>
</tr>
</tbody>
</table>';
$mail->MsgHTML($message);
$mail->AddAddress($value, 'Recipient');
$sent=$mail->Send();
echo $sent.' '.$value.'<br/>';
$mail->ClearAddresses();
$i++;
}