Page 1 of 1

NativeMail not working where as PHPMail is

Posted: Fri Jul 06, 2007 3:15 pm
by Technocrat
Just to make sure I am assuming that Swift_Connection_NativeMail uses the standard PHP mail() correct?

If that is true I have 1 customer where using Swift_Connection_NativeMail the email doesn't go out. If I comment it out and use mail() instead it works fine.

Any suggestions?

Posted: Fri Jul 06, 2007 9:10 pm
by Chris Corbyn
Code please :)

Posted: Sat Jul 07, 2007 12:35 am
by Technocrat
When I say 1 customer that means somewhere in the neighborhood of 5K other people are using it just fine. Which is why I think its a problem on their server end. Just like the last problem I posted here.

Code: Select all

function evo_mail($to, $subject, $content, $header='', $params='', $batch=false, $from='', $replyto='') {
    global $board_config, $nukeconfig, $cache;

    if (empty($to)) return false;

    require_once (NUKE_INCLUDE_DIR.'mail/Swift.php');
    require_once (NUKE_INCLUDE_DIR.'mail/Swift/Connection/Multi.php');
    require_once (NUKE_INCLUDE_DIR.'mail/Swift/Connection/Sendmail.php');
    require_once (NUKE_INCLUDE_DIR.'mail/Swift/Connection/SMTP.php');
    require_once (NUKE_INCLUDE_DIR.'mail/Swift/Connection/NativeMail.php');

    //If for some reason there is a , in the $to and its not a swift recipient list
    if (!is_object($to) && strchr($to, ',')) {
        //Break it up
        $tos = explode(',', $to);
        //Mak it a swift recipient list
        $to =& new Swift_RecipientList();
        foreach ($tos as $address) {
            $to->addTo($address, $address);
        }
    }

    //If there is no from
    if (empty($from)) {
        //If the nuke config is empty or is set to default
        if (!isset($nukeconfig['adminmail']) || empty($nukeconfig['adminmail']) || $nukeconfig['adminmail'] == 'webmaster@------.---') {
            //If the board email is empty or set to default
            if (!isset($board_config['board_email']) || empty($board_config['board_email']) || $board_config['board_email'] == 'Webmaster@MySite.com') {
                //Give an error
                die(_ERROR_EMAIL);
            } else {
                $from = $board_config['board_email'];
            }
        } else {
            $from = $nukeconfig['adminmail'];
        }
    }

    //If for some reason there is a , in the from
    if (strchr($from, ',')) {
        //Get rid of it
        $from = substr($from, 0, strpos($from, ','));
    }

    //If the forums are set to SMTP
    if (isset($board_config['smtp_delivery']) && $board_config['smtp_delivery'] == '1') {
        //If the board is set to use SMTP but there is no username for it
        if (empty($board_config['smtp_username']) || empty($board_config['smtp_username'])) {
            $swift =& new Swift(new Swift_Connection_SMTP($board_config['smtp_password']));
        } else {
            $smtp =& new Swift_Connection_SMTP($board_config['smtp_host'], 25);
            $smtp->setUsername($board_config['smtp_username']);
            $smtp->setpassword($board_config['smtp_password']);
            $swift =& new Swift($smtp);
        }
    //If not using SMTP
    } else {
        //Uncomment define at the top to use sendmail
        if (defined('SENDMAIL')) {
            $sendmail_path = @ini_get('sendmail_path');
            $sendmail_path = str_replace(' -i', '', $sendmail_path);
            if (function_exists('proc_open') && !empty($sendmail_path)) {
                $swift =& new Swift(new Swift_Connection_Sendmail($sendmail_path));
            } else {
                $swift =& new Swift(new Swift_Connection_NativeMail());
            }
        } else {
            $swift =& new Swift(new Swift_Connection_NativeMail());
        }
    }

    //Change new lines to HTML new lines
    $content = str_replace("\r\n", "<br />", $content);
    $content = str_replace("\n", "<br />", $content);

    //If cache is valid and using file cache
    if ($cache->valid && $cache->type == 1) {
        //Use it with swift cache
        Swift_CacheFactory::setClassName("Swift_Cache_Disk");
        Swift_Cache_Disk::setSavePath(NUKE_CACHE_DIR);
    }

    //Start a new message
    $message =& new Swift_Message($subject, $content, "text/html");

    //If there was a reply to
    if (!empty($replyto)) {
        $message->setReplyTo($replyto);
    }

    //If its a batch job
    if ($batch && is_object($to)) {
        $sent = $swift->batchSend($message, $to, $from);
    } else {
        $sent = $swift->send($message, $to, $from);
    }

    //Disconnect from the mail server
    $swift->disconnect();

    return $sent;
}
So if I take out $sent = $swift->send($message, $to, $from); and use mail() instead it goes out just fine.

Posted: Wed Jul 18, 2007 12:23 pm
by Technocrat
Anything you can think of? I have 3 other people with the same type of issue. Though it seems really rare, there must be some reason for it.

Posted: Wed Jul 18, 2007 2:41 pm
by Chris Corbyn
Sorry, no resolution as yet. I can't recreate it. I know there's a security patch for PHP which prevents newlines being put in the subject/recipient parameters of mail(). Perhaps the server has this patch on it? Can you run with full error reporting and display_errors on? :)

Posted: Wed Jul 18, 2007 5:23 pm
by Technocrat
Ok I will see if he will let me back on to try it.