[英]PHPMailer fails after one email in Cron Job
我有通过客户端电子邮件循环并使用PHPMailer向其发送报告的php代码。 在浏览器中运行时,它可以完美运行。 我创建了一个cron作业,该作业可以打开并运行php文件。 但是,在执行cron作业时,将发送第一封电子邮件,但程序将停止发送电子邮件。 经过测试,我意识到在发送第一封电子邮件后,其余代码将停止工作,并且永远不会循环到下一个“ while”状态。
使用的cron作业(确实会打开并运行文件):
/usr/bin/php -q site1/temp/test.php
下面的代码在浏览器中打开时将发送3封电子邮件,但仅在使用cron作业运行时才发送一封电子邮件:
<?php
$index = 0;
while($index < 3){
require (dirname(__FILE__) . DIRECTORY_SEPARATOR .'PHPMailer/PHPMailerAutoload.php');
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'my smtp provider'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'name'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 25; // TCP port to connect to
$mail->From = 'email';
$mail->FromName = 'ME';
$mail->AddAddress( "test@yahoo.com" );
$mail->isHTML(true);
$mail->Subject = 'Test email';
$mail->Body = "test";
$mail->send();
// nothing else is sent at this point after the first loop through
}//end while
?>
注意:使用Linux Server(Parallels Plesk)
试试这个代码:
<?php
require (dirname(__FILE__) . DIRECTORY_SEPARATOR .'PHPMailer/PHPMailerAutoload.php');
$index = 0;
while($index < 3){
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'my smtp provider'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'name'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 25; // TCP port to connect to
$mail->From = 'email';
$mail->FromName = 'ME';
$mail->AddAddress( "test@yahoo.com" );
$mail->isHTML(true);
$mail->Subject = 'Test email';
$mail->Body = "test";
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
$mail->clearAddresses();
$index++;
}
?>
使用$ mail-> clearAddresses(); 在循环内。 并检查您得到的错误类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.