我有PHP代码循环客户端电子邮件并使用PHPMailer发送报告。在浏览器中运行时,它可以完美运行。我创建了一个能够打开并运行php文件的cron作业。但是,当cron作业运行时,会发送第一封电子邮件,但程序会停止发送电子邮件。经过测试后,我意识到在发送第一封电子邮件之后,剩下的代码就会停止工作,而且它永远不会循环到下一个电子邮件中。条件。
使用了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服务器(Parallels Plesk)
答案 0 :(得分:0)
试试这段代码:
<?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-&gt; clearAddresses();循环内。并检查你得到的错误类型。