1

I have setup dkim on my server using opendkim(centos). when php executes mail command(on website eg forgot.php). it sends an email signed with DKIM. but i also setup a cron job to run mailcron.php which sends out emails.

i setup cron by typing crontab -e [ENTER] */5 * * * * php -q /var/www/html/mailcron.php [save]

OUTPUT from cron log

Jul 21 03:15:01 veepiz crond[3819]: (root) CMD (/etc/webmin/status/monitor.pl)
Jul 21 03:15:01 veepiz crond[3820]: (root) CMD (/etc/webmin/virtual-server/collectinfo.pl)
Jul 21 03:15:01 veepiz crond[3821]: (root) CMD (php -q /var/www/html/mailcron.php)

why are emails sent using cron not signing while those running directly from the site signing.

<?php
    set_time_limit(0);    
    error_reporting(E_ALL);
    $dbserver='localhost';
    $dbuser='cccc';
    $dbpassword='xxxx';
    $dbname='veepiz';
    $homepath="http://www.veepiz.com";
    $supportemail="[email protected]";
    $localpath = '';
    $domainpath = '.veepiz.com';
    $dblink=mysql_connect($dbserver,$dbuser,$dbpassword);
    require("class.phpmailer.php"); 
    if (!$dblink)
    {
        die('Could not connect to database.<br/>'.mysql_error());
    }
    if (!(mysql_select_db($dbname,$dblink)))
    {
        die('Could not select database<br/>'.mysql_error());
    }

    $q="SELECT * FROM emailcron ORDER BY id ASC";
    $r=mysql_query($q);
    if ($r)
    {
        if (mysql_num_rows($r)>0)
        {
            $message="";
            $n=0;
            $c=0;
            while ($o=mysql_fetch_object($r))
            {
                $x = new PHPMailer();
                $x->From = "[email protected]";
                $x->FromName = "Veepiz";
                $x->AddAddress($o->emailaddress,$o->toname);
                $x->AddReplyTo("[email protected]", "Veepiz");
                $x->WordWrap = 50;                                 // set word wrap to 50 characters
                $x->IsHTML(true);                                  // set email format to HTML
                $x->Subject = $o->subject;
                $x->Body    = nl2br($o->content);
                $x->AltBody = strip_tags($o->content);
                if(!$x->Send())
                {   $c++;
                    $message .= "Message could not be sent. <br/>\n"; 
                    $message .= "Mailer Error: " . $x->ErrorInfo."<br/>\n"; 
                } else { $n++; mysql_query("DELETE FROM emailcron WHERE id=".$o->id); }                
            }
            if (strlen($message)>0) 
            {
                echo $message;      
            } else
            {
                echo  "Mails sent:$n; Mails Failed:$c";
            }
        }    
    }    
?>
2
  • Because something's different between how PHP sends mails as opposed to how cron sends mails. Your mail config and PHP scripts will be required to diagnose this.
    – womble
    Jul 21, 2011 at 3:45
  • i dnt understand why this question has been down voted. its very valid
    – Sir Lojik
    Jul 21, 2011 at 3:53

1 Answer 1

1

The cron job's shell environment is often different from a login shell. This should be the reason.

Also make sure you give the full path to PHP like

*/5 * * * * * /usr/local/php5/bin/php -q /var/www/html/mailcron.php

http://web.cecs.pdx.edu/~akshay/2009/11/the-cron-environment/

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .