Cron job doesn’t send email

I have a cron job set up on my hostgator server that returns a value of 1 (email sent), but doesn’t actually send the email. When I call the php file manually in the browser, the email gets sent. It doesn’t if run via cron.

Earlier this month, I moved my site from one server on hostgator to another server (on hostgator) so I could get SSL and a dedicated IP address. Since moving the site, the cron jobs work OK except for the part that sends the email (ie, database functions work fine). I’ve contacted hostgator tech support, but he thinks the problem is in my code.

Thinking that maybe my server info was incorrect, I switched to using smtp.gmail.com and using a gmail account to send the mail, but that didn’t work either. Please help!

The cron job is set up like this:

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

(While testing, I changed it to run every 2 minutes: */2 * * * * )

Here’s the sendmailcron.php script:

<?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
}

?>

The return value is always 1, but no email is sent.

Any suggestions would be greatly appreciated!


Source: stackoverflow-php