Php Cron作业没有';不要发送电子邮件
我在hostgator服务器上设置了一个cron作业,该作业返回值1(已发送电子邮件),但实际上不发送电子邮件。当我在浏览器中手动调用php文件时,会发送电子邮件。如果通过cron运行,则不会 本月早些时候,我将我的站点从hostgator上的一台服务器移动到另一台服务器(hostgator上),以便获得SSL和专用IP地址。移动站点后,cron作业正常工作,但发送电子邮件的部分除外(即,数据库功能正常工作)。我已经联系了hostgator技术支持,但他认为问题出在我的代码中 考虑到我的服务器信息可能不正确,我转而使用smtp.gmail.com并使用gmail帐户发送邮件,但这也不起作用。请帮忙 cron作业的设置如下所示:Php Cron作业没有';不要发送电子邮件,php,email,cron,swiftmailer,Php,Email,Cron,Swiftmailer,我在hostgator服务器上设置了一个cron作业,该作业返回值1(已发送电子邮件),但实际上不发送电子邮件。当我在浏览器中手动调用php文件时,会发送电子邮件。如果通过cron运行,则不会 本月早些时候,我将我的站点从hostgator上的一台服务器移动到另一台服务器(hostgator上),以便获得SSL和专用IP地址。移动站点后,cron作业正常工作,但发送电子邮件的部分除外(即,数据库功能正常工作)。我已经联系了hostgator技术支持,但他认为问题出在我的代码中 考虑到我的服务器信
* 7 * * * /opt/php56/bin/php /home/user/public_html/somefolder/sendmailcron.php
/opt/php56/bin/php -d safe_mode=Off /home/user/public_html/somefolder/sendmailcron.php
(测试时,我将其更改为每2分钟运行一次:**/2****)
下面是sendmailcron.php脚本:
<?php
$now = date("Y-m-d H:i:s");
$msgcontent = [];
$msgcontent['email'] = "recipient@example.com";
$msgcontent['name'] = "Recipient Name";
$msgcontent['textpart'] = "This is the text version of the email body.";
$msgcontent['htmlpart'] = "<p>This is the <strong>HTML</strong> version of the email body.</p>";
$msgcontent['subject'] = "Test email sent at " . $now;
$result = sendMyMail($msgcontent, "HTML");
exit();
function sendMyMail($msgcontent, $format="HTML") {
require_once '/home/user/public_html/somefolder/swiftmailer/lib/swift_required.php';
$result = 0;
$subject = $msgcontent['subject'];
$email = $msgcontent['email'];
if (strlen($email) == 0) {
return 0;
}
$name = $msgcontent['name'];
$emailbody = $msgcontent['textpart'];
$emailpart = $msgcontent['htmlpart'];
switch($format) {
case "TEXT":
$msgformat = 'text/plain';
break;
case "HTML":
$msgformat = 'text/html; charset=UTF-8';
break;
default:
$msgformat = 'text/html';
break;
}
$adminemailaddress = "me@mydomain.com";
$adminemailpwd = 'myadminpwd';
$sendername = 'My Real Name';
$transport = Swift_SmtpTransport::newInstance('mygator1234.hostgator.com', 465, "ssl")
->setUsername($adminemailaddress)
->setPassword($adminemailpwd)
;
$mailer = Swift_Mailer::newInstance($transport);
// Create the message
if($format == "TEXT") {
$message = Swift_Message::newInstance($subject)
->setFrom(array($adminemailaddress => $sendername))
->setTo(array($email => $name))
->setBody($emailbody)
->setContentType($msgformat)
;
}
else {
$message = Swift_Message::newInstance($subject)
->setFrom(array($adminemailaddress => $sendername))
->setTo(array($email => $name))
->setBody($emailpart)
->setContentType($msgformat)
;
}
//This is where we send the email
try {
$result = $mailer->send($message); //returns the number of messages sent
} catch(\Swift_TransportException $e){
$response = $e->getMessage() ;
}
return $result; //will be 1 if success or 0 if fail
}
?>
返回值始终为1,但不发送电子邮件
如有任何建议,将不胜感激 cron作业使用的php.ini配置文件与浏览器使用的不同 通过禁用
safe\u模式
尝试运行cron作业,如下所示:
* 7 * * * /opt/php56/bin/php /home/user/public_html/somefolder/sendmailcron.php
/opt/php56/bin/php -d safe_mode=Off /home/user/public_html/somefolder/sendmailcron.php
并检查电子邮件是否已发送。是否检查了邮件日志文件?如果从命令行运行脚本会发生什么情况?当我从浏览器运行脚本时,邮件将被发送。当从PuTTY shell或cron作业运行时,不会发送电子邮件。解决方案:结果是,这些电子邮件被SMTP服务器的垃圾邮件过滤器拦截。经过2.5小时的hostgator技术支持,问题似乎已经解决。我很高兴这不是一个编码问题非常感谢您的建议,但不幸的是,在我做出此更改后,没有发送电子邮件(