Jump to content
Sign in to follow this  

Problem with SwiftMailer and CRON Jobs

Recommended Posts

I have been using SwiftMailer for few years now to sent confirmation emails upon registration and password recovery emails is requested; with great success. 


I have first stared to code the page and manually triggering it by going to the URL directly with my browser. Entire thing runs in little under one second, completes in full, no error and all intended people get emails as they should. Here is the relevant code: 

require '../../scripts/swift-mailer/lib/swift_required.php';
require '../../scripts/swift-mailer/confirmation_smtp.php';	

// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance()
    ->setSubject("Daily Update")
    ->setBody($body, 'text/html');
// Send the Message
    $result = $mailer->send($message);

And the confirmation_smtp.php file is:

	$transport = Swift_SmtpTransport::newInstance('www.mydomain.com', 465, 'ssl')

My "send to" email address is currently set to my gmail address for testing purposes. Right now I am trying to use CRON jobs to send handful of emails to few people every 24 hours if certain conditions are met. I have added the command line in "php /home/username/../../../emails.php" and it runs as scheduled, I get no errors in the logs. As you can see I send a BCC to my local email, archive@mydomain.com. With CRON job I get the BCC email to my local email address (archive@mydomain.com) with the email structure as it should be, as well as "To" address showing as my gmail account in headers. However, my gmail account gets no messages with my code being triggered by CRON job itself (I have checked spam). If, however, I manually type in the URL where the same code is stored, it runs, and only then I do get the email to gmail (or yahoo, or any other external email). 


I am not sure why CRON job is not sending emails to outside domains, just local ones. I have shared business hosting with HostGator, with dedicated IP address, cPanel control, and correctly configured Email Authentication records for mailing. Any thoughts? Thanks

Edited by pioneerx01

Share this post

Link to post
Share on other sites

Do you have access to the SMTP server logs? Or your hosting provider? Can you confirm that the server is receiving an email message as expected? Alternatively you could (maybe) set up a local sendmail that logs everything, then send your email through that and compare the messages when sent through cron and the browser.


In other words, gather information. Confirm that stuff is working at it should at each step after your code. Compare what you see with and without cron.

Share this post

Link to post
Share on other sites

And under which account do the cronjobs run? The webserver account (e. g. www-data)?

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.