• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Bug in Email class caused issue when used with qmail due to crlf - [SOLVED]

#1
[eluser]yongkhun[/eluser]
I have faced lots of issues with the email class in CodeIgniter. When I tried to send email using SMTP, it gives me error as follow:

Code:
-----------------------------------------
220 qmail-relay-norm-0.netfirms.com ESMTP

hello: 250-qmail-relay-norm-0.netfirms.com
250-AUTH LOGIN CRAM-MD5 PLAIN
250-AUTH=LOGIN CRAM-MD5 PLAIN
250-PIPELINING
250 8BITMIME
from: 250 ok
to: 250 ok
data: 354 go ahead
451 See http://pobox.com/~djb/docs/smtplf.html.
The following SMTP error was encountered: 451 See http://pobox.com/~djb/docs/smtplf.html.
Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.

User-Agent: Silkron
Date: Mon, 25 Jun 2007 19:59:22 +0800
From: "Fonod"
Return-Path:
To: abc@xyz.com
Subject: Fonod: Testing email
Reply-To: "abc@xyz.com"
X-Sender: abc@xyz.com
X-Mailer: Silkron
X-Priority: 3 (Normal)
Message-ID: <467fae1ae0636@xyz.com>
Mime-Version: 1.0


Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit

testing
---------------------------------------

#2
[eluser]yongkhun[/eluser]
I found that the member function set_newline() in Email class has a bug (Email.php). It will never set "\r\n" due to the if checking in the beginning of the function because it will always set to "\n" no matter what other escape characters you are trying to set it with. So, to correct the bug, just replace the OR with XOR instead as follow:

Code:
function set_newline($newline = "\n")
{
    if ($newline != "\n" XOR $newline != "\r\n" XOR $newline != "\r")
    {
        $this->newline    = "\n";    
        return;
    }
    
    $this->newline    = $newline;    
}

Also, when you initialize the email config, be careful not to use single-quote for the newline value. Otherwise, the escape characters of \r or \n will not be taken properly. Although this is PHP fundamental, but it might be easily overlooked especially when misled by the example in codeigniter manual when setting other email config.

Code:
$mailconfig['protocol'] = 'smtp';       // no issue with single-quote here as in the example of codeigniter manual
$mailconfig['newline'] = "\r\n";    // must be enclosed with double-quotes
$mailconfig['wordwrap'] = TRUE;
$CI->email->initialize($mailconfig);

Hope this saves the time for others when encountered with the same issue.

#3
[eluser]Dennis Geus[/eluser]
Yes, that was the solution, thank you
Don't use single quotes.

#4
[eluser]hyperfire[/eluser]
I'm having this same error.
Tryied the sugested "fix" with no luck so far.
Still have the same error msg:

451 See http://pobox.com/~djb/docs/smtplf.html.
The following SMTP error was encountered: 451 See http://pobox.com/~djb/docs/smtplf.html.
Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.

What should I do?

Using qmail on a default MT DV with CI 2.1 with this code

Code:
$mail_msg = $this->mBody($tpl, $data, $subject);
                        
        $this->_CI->load->library('email');
                  
        $this->_CI->email->from($this->common['mailing_mail'],$this->common['site_title']);
        $this->_CI->email->to($to->email);      
        $this->_CI->email->subject($subject);
        $this->_CI->email->message($mail_msg);
        
        $mailSent = $this->_CI->email->send() == TRUE;

        if ($mailSent) {
            return TRUE;
        } else {
            print_r($mailSent);
            echo $this->_CI->email->print_debugger();
            exit();
            return FALSE;
        }


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.