[英]sent mail succed on my machine but failed the server using phpmailer
[英]Mail not sent using PhpMailer
我必须使用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
,但是邮件不会发送到任何邮件地址。 也没有错误。 谁能告诉我缺少了什么?
试试这个功能
我们创建了一个函数think,因此它将起作用
/* 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);
这听起来像是一个环境问题,但是无论如何我已经解决了您的代码中的几个问题-无需在循环内克隆PHPMailer实例!
默认情况下,它使用PHP mail()函数发送消息,因此它将通过sendmail二进制文件提交到localhost。 使用该路由时,您无法获得太多的调试信息-通过使用SMTP可以得到更多(尽管速度较慢)-只需IsMail()
的调用更改为IsSMTP()
,您会得到很多调试输出。 要测试通过mail()进行的发送,您可以在php.ini中将sendmail路径设置为指向PHPMailer的test文件夹中的fakesendmail.sh
脚本,它将简单地将您的消息通过管道发送到temp文件夹中。
除此之外,您还应该检查邮件服务器日志(通常是/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++;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.