Cron作业不发送电子邮件

问题描述:

我在我的hostgator服务器上设置了一个cron作业,返回值1(发送电子邮件),但实际上并未发送电子邮件。当我在浏览器中手动调用php文件时,会发送电子邮件。它不通过cron运行。Cron作业不发送电子邮件

本月早些时候,我将我的网站从hostgator上的一台服务器移至另一台服务器(位于hostgator上),以便我可以获得SSL和专用IP地址。由于移动站点,cron作业工作正常,除了发送电子邮件的部分(即数据库功能正常工作)。我已经联系了hostgator技术支持,但他认为问题出在我的代码中。

认为我的服务器信息可能不正确,我切换到使用smtp.gmail.com并使用gmail帐户发送邮件,但这也不起作用。请帮忙!

cron作业设置是这样的:

* 7 * * * /opt/php56/bin/php /home/user/public_html/somefolder/sendmailcron.php 

(测试时,我把它改为每2分钟运行:*/2 * * * *)

这里的sendmailcron.php脚本:

<?php 

$now = date("Y-m-d H:i:s"); 

$msgcontent = []; 
$msgcontent['email'] = "[email protected]"; 
$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 = "[email protected]"; 
    $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,但未发送电子邮件。

任何建议将不胜感激!

+0

检查邮件日志文件?如果从命令行运行脚本,会发生什么情况? – nogad

+0

当我从浏览器运行它时,邮件被发送。从PuTTY shell或cron作业运行时,不会发送电子邮件。 – rottlover

+0

SOLUTION: 事实证明,邮件被SMTP服务器的垃圾邮件过滤器阻塞。用hostgator技术支持2.5小时后,看起来我们已经解决了问题。我很高兴这不是一个编码问题。 ;) – rottlover

您的cron作业使用不同的php.ini配置文件,而不是您的浏览器使用。

尝试通过禁用safe_mode,像这样运行cron作业:

/opt/php56/bin/php -d safe_mode=Off /home/user/public_html/somefolder/sendmailcron.php 

,并检查邮件现在发送。

+0

非常感谢您的建议,但不幸的是,我做了这个改变后没有发送邮件。 :( – rottlover