码迷,mamicode.com
首页 > 其他好文 > 详细

codeigniter 2 发送邮件问题

时间:2015-08-31 21:15:38      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:

codeigniter自带了一个Email类,一直都在用它。最近发现发送邮件到网易邮箱时中文subject异常,但其他邮箱(如:腾讯邮箱)未发现有此类情况,如图:

技术分享

 

通过万能的google,知道原来邮件头信息中的各字段值通常都会设置 =?utf-8?Q? 指定编码方式。再看看已收到的邮件subject,已经是这样的格式,说明CI已经帮我们转换成了这种格式,而且已经被截断了。为什么会被截断呢,再查一下即可知道是因为换行符。知道问题所在即可改之:

 

    /**
	 * Prep Q Encoding
	 *
	 * Performs "Q Encoding" on a string for use in email headers.  It‘s related
	 * but not identical to quoted-printable, so it has its own method
	 *
	 * @access	public
	 * @param	str
	 * @param	bool	// set to TRUE for processing From: headers
	 * @return	str
	 */
	protected function _prep_q_encoding($str, $from = FALSE)
	{
		$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));

		//网易邮箱subject被截断bug修复
		$str = str_replace(array("\r", "\n"), array(‘‘, ‘‘), $str);

		return $str;
	}

  

 

codeigniter 2 发送邮件问题

标签:

原文地址:http://www.cnblogs.com/hanyingchun/p/4774007.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!