What's wrong with this subject line? Error 451 CodeIgniter Email class

1.3k views Asked by At

So this is weird.

I'm getting a 451 error on an email that's being generated, except that what causes it is the plain text portion of the subject line :s

This subject line gets through fine:

$this->email->subject('New task in "'.$data['property_name'].'"');

This one causes the 451 error:

$this->email->subject('A user has completed their task in "'.$data['property_name'].'"');

For reference, Error 451 is for bare LF's (http://cr.yp.to/docs/smtplf.html). It's normally caused by not declaring the line ending rules in the settings, or using single quotes, i.e. '/r/n/' instead of "/r/n". My settings are correct and emails work fine.

Something notable in the the debugger, is that the longer line appears as so:

Subject: =?utf-8?Q?A_user_has_completed_their_task_in_"TASKNAME
?=
 =?utf-8?Q?"?=

Whereas the working one appears like:

Subject: =?utf-8?Q?New_task_in_"TASKNAME"?=

Is this a CI bug?

2

There are 2 answers

7
stormdrain On

Where are you getting the 451 error from? I'm seeing 451 being reported as a local error in processing.

The Email class breaks down the subject string if it's longer than 76 characters:

109 // Line length must not exceed 76 characters, so we adjust for
110 // a space, 7 extra characters =??Q??=, and the charset that we will add to each line

You can see it in action:

function test()
{
    echo "<pre>";
    print_r($this->_prep_q_encoding('A user had completed their task in "Going to change string to something"'));
}


function _prep_q_encoding($str, $from = FALSE)
{
    $this->crlf= "\n";  
    $this->charset = 'utf-8';
    $str = str_replace(array("\r", "\n"), array('', ''), $str);

    // Line length must not exceed 76 characters, so we adjust for
    // a space, 7 extra characters =??Q??=, and the charset that we will add to each line
    $limit = 75 - 7 - strlen($this->charset);

    // these special characters must be converted too
    $convert = array('_', '=', '?');

    if ($from === TRUE)
    {
        $convert[] = ',';
        $convert[] = ';';
    }

    $output = '';
    $temp = '';

    for ($i = 0, $length = strlen($str); $i < $length; $i++)
    {
        // Grab the next character
        $char = substr($str, $i, 1);
        $ascii = ord($char);

        // convert ALL non-printable ASCII characters and our specials
        if ($ascii < 32 OR $ascii > 126 OR in_array($char, $convert))
        {
            $char = '='.dechex($ascii);
        }

        // handle regular spaces a bit more compactly than =20
        if ($ascii == 32)
        {
            $char = '_';
        }

        // If we're at the character limit, add the line to the output,
        // reset our temp variable, and keep on chuggin'
        if ((strlen($temp) + strlen($char)) >= $limit)
        {
            $output .= $temp.$this->crlf;
            $temp = '';
        }

        // Add the character to our temporary line
        $temp .= $char;
    }

    $str = $output.$temp;

    // wrap each line with the shebang, charset, and transfer encoding
    // the preceding space on successive lines is required for header "folding"
    $str = trim(preg_replace('/^(.*)$/m', ' =?'.$this->charset.'?Q?$1?=', $str));

    return $str;
}

Which outputs

=?utf-8?Q?A_user_had_completed_their_task_in_"Going_to_change_string_to_?=
 =?utf-8?Q?something"?=

I've seen other's have problems with the way email configs are set. Do you have

$config['crlf'] = "\r\n"; //double quotes ("), not single (') 
$config['newline'] = "\r\n";  //double quotes ("), not single (') 

set?

6
Mudshark On

I think you're mixing up single and double quotes. Try:

$this->email->subject('A user has completed their task in '.$data['property_name']);