Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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="support@veepiz.com";
    $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 = "support@veepiz.com";
                $x->FromName = "Veepiz";
                $x->AddAddress($o->emailaddress,$o->toname);
                $x->AddReplyTo("support@veepiz.com", "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";
            }
        }    
    }    
?>
share|improve this question
    
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 '11 at 3:45
    
i dnt understand why this question has been down voted. its very valid –  Sir Lojik Jul 21 '11 at 3:53
add comment

1 Answer

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/

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.