EllisLab text mark
Advanced Search
     
invalid email From: header syntax per RFC 822
Posted: 11 May 2009 12:28 PM
Joined: 2009-04-22
8 posts

Hey,

I think I’ve stumbled upon a bug with the Email library of CI, latest version 1.7.

I am using Mercury as a localhost SMTP server as part of XAMPP and it throws 501 Bad recipient address syntax errors at me.

I have dug a little deeper and found that CI is sending what appears to be a bad From: header syntax.

The correct syntax for From headers as far as I can make out from the RFC is

From: .(JavaScript must be enabled to view this email address)
From: Code Igniter <joe@bloggs.com>
From: .(JavaScript must be enabled to view this email address), .(JavaScript must be enabled to view this email address)

If I use CI->email->send(“joe@bloggs.com”) then CI sends

From: <joe@bloggs.com>

And CI->email->send(“joe@bloggs.com”, “Code Igniter”) sends

From: “Code Igniter” <joe@bloggs.com>

Both of which are syntactically wrong, or if not wrong they do not match the syntax I have found from the RFC and the mail server I am sending to does not like it either.

If I modify the CI source for the from function to account for the syntax.

function from($from$name '')
    
{
        
if (preg_match'/\<(.*)\>/'$from$match))
        
{
            $from 
$match['1'];
        
}
        
        $return_path 
$from;

        if (
$this->validate)
        
{
            $this
->validate_email($this->_str_to_array($from));
        
}

        
// prepare the display name
        
if ($name != '')
        
{
            
// only use Q encoding if there are characters that would require it
            
if ( ! preg_match('/[\200-\377]/'$name))
            
{
                
// add slashes for non-printing characters, slashes, and double quotes, and surround it in double quotes
                
$name addcslashes($name"\0..\37\177'\"\\");
            }
            else
            {
                
$name = $this->_prep_q_encoding($name, TRUE);
            }
            
            
$from = ' <'.$from.'>';
        }

        
$this->_set_header('From', $name.$from);
        
$this->_set_header('Return-Path', '<'.$return_path.'>');
    } 

This produces

[From] => .(JavaScript must be enabled to view this email address)
[Return-Path] => <joe@bloggs.com>

Which works when not using a name in the from() call.

The above function, when using a name to the from() function will produce as per the syntax

[From] => Code Ingiter <joe@bloggs.com>
[Return-Path] => <joe@bloggs.com>

However, this still breaks the mail server and I get the 501 error again despite this matching the correct syntax.

So not too sure about what is happening when using a from name.

Any ideas/suggestions? Am I wrong about the syntax here or is there an issue going on?

Cheers

 
Posted: 11 May 2009 04:33 PM   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2007-07-30
2144 posts

RFC 2822 made RFC 822 obsolete and introduced the patterns you are seeing in CodeIgniter’s output (RFC 2822 is obsolete as well, RFC 5322 is the latest).

The patterns <user@example.com> and “A User” <user@example.com> are both valid according to the addr-spec (Example A.1.2 features both of these formats in addition to others).

I’m leaning to Mercury being misconfigured (or maybe an older version of Mercury, but I doubt that RFC 2822 was released in April 2001).

 Signature 

Follow me on twitter here.
MichaelWales.com | MichaelWales.info