【问题标题】:What's wrong with this subject line? Error 451 CodeIgniter Email class这个主题行有什么问题?错误 451 CodeIgniter 电子邮件类
【发布时间】:2013-12-17 14:22:39
【问题描述】:

所以这很奇怪。

我在正在生成的电子邮件中收到 451 错误,但导致它的原因是主题行的纯文本部分:s

这个主题行很好:

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

这个导致451错误:

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

作为参考,错误 451 是针对裸 LF (http://cr.yp.to/docs/smtplf.html)。这通常是由于没有在设置中声明行结束规则,或者使用单引号,即 '/r/n/' 而不是 "/r/n"。我的设置是正确的,电子邮件工作正常。

在调试器中值得注意的是,较长的行是这样显示的:

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

而工作的看起来像:

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

这是一个 CI 错误吗?

【问题讨论】:

    标签: php codeigniter qmail


    【解决方案1】:

    我认为您混淆了单引号和双引号。试试:

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

    【讨论】:

    • 嗨 Mudshark - 没有那些双引号是字符串的一部分,即 - “Foo”中的新任务。
    • @NathanHornby 他们可能会破坏该请求中字符串的编码。您是否尝试过删除引号以查看它是否在没有它们的情况下通过。
    • 嗨@micb - 即使是这种情况,它也没有提供解决方案,因为它们需要在那里 - 为什么它只会在之前的文本稍长时拒绝它们?我会试一试,它对我来说似乎有点不合适。在我看来,行的长度是问题所在,至少考虑到调试中的输出差异。至少如果这是我想的问题,那将是一个更明确的错误。
    • 只是一个想法,这 stackoverflow.com/questions/11794698/max-line-length-in-mail 会不会是问题所在,因为失败的那个比另一个长得多,并且因为引号将进入一个新行,所以它们会破坏它?
    • 非常有趣!这可以解释为什么它将它推到两行(当您期望行列无关紧要时)。在这种情况下,您可能部分正确 - 因为它是引起问题的引号,而不是通过任何语法问题,而是由于邮件解析器的行为(我猜 SMTP 的工作原理与我看到的输出相同)。我还没有机会对此进行全面测试 - 但似乎我们必须非常小心主题行,因为需要包含不同的长度值。
    【解决方案2】:

    您从哪里得到 451 错误?我看到 451 被报告为 local error in processing

    如果主题字符串超过 76 个字符,Email 类会分解它:

    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
    

    您可以看到它的实际效果:

    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;
    }
    

    哪些输出

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

    我看到其他人的电子邮件配置设置方式存在问题。你有吗

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

    设置?

    【讨论】:

    • 你好,风暴排水系统。看起来您的电子邮件是使用邮件而不是 SMTP 发送的,考虑到 451 错误是 SMTP 错误,这可能是一个重大差异。
    • 编辑:另外值得注意的是,来自“property_name”变量的结果字符串比“TASKNAME”示例长得多,类似“Lorem ipsum dolor sit amet pro”这样的内容可以进行更好的测试字符串,因为我觉得长度很重要。感谢您的意见!
    • 我很抱歉。是的,长度很重要。 “TASKNAME”字符串是否有任何特殊字符(单/双引号、& 等)?
    • 不,没有特殊字符!这也是我的第一个猜测:)
    • 我更新了“答案”。不确定是否有帮助。需要澄清一下。
    猜你喜欢
    • 2011-03-03
    • 2011-08-13
    • 2013-01-27
    • 2012-01-11
    • 1970-01-01
    • 2010-11-07
    • 1970-01-01
    • 2016-08-12
    • 1970-01-01
    相关资源
    最近更新 更多