0

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!

rottlover
  • 83
  • 13
  • checked the mail log file? what happens if you run the script from the command line? –  Apr 26 '17 at 22:44
  • When I run it from the browser, the mail is sent. When run from a PuTTY shell or cron job, no email is sent. – rottlover Apr 27 '17 at 11:47
  • SOLUTION: It turns out, the emails were being snagged by the SMTP server's spam filter. After 2.5 hours with hostgator tech support, it looks like we have the issue resolved. I'm glad it wasn't a coding issue. ;) – rottlover Apr 27 '17 at 15:23

1 Answers1

0

Your cron job uses a different php.ini configuration file, than your browser uses.

Try running the cron job by disabling safe_mode, like this:

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

And check if the email is now sent.

foxtrot
  • 1,056
  • 1
  • 8
  • 24