Page 1 of 1

mail() - spanish accents

Posted: Mon Aug 07, 2006 1:48 pm
by alexislalas
hello.

my website is in spanish, and im sending a mail with the mail() function, but dont know how to put the accents. is there a way, like in html?

thanks

Posted: Mon Aug 07, 2006 2:47 pm
by Chris Corbyn
You can use HTML entities, or you can send your email in UTF-8 format. Swift Mailer will manage the encoding for you if you're sending such characters since SMTP has certain restrictions on this -- particularly in the headers such as the subject line.

Posted: Mon Aug 07, 2006 5:48 pm
by bokehman
If it is a plain text email use the following headers:

Code: Select all

Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: 8bit
As for the subject line:

Code: Select all

$subject = 'Español';
$charset = 'ISO-8859-1';
$subject_encoded = '=?'.$charset.'?B?' . base64_encode($subject) . '?=';
print $subject_encoded;
Saludos

Posted: Mon Aug 07, 2006 6:00 pm
by Chris Corbyn
bokehman wrote:If it is a plain text email use the following headers:

Code: Select all

Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: 8bit
As for the subject line:

Code: Select all

$subject = 'Español';
$charset = 'ISO-8859-1';
$subject_encoded = '=?'.$charset.'?B?' . base64_encode($subject) . '?=';
print $subject_encoded;
Saludos
That doesn't work if you saved the file as UTF-8 with your editor. Tried and tested with a lot of hairs pulled out.

Posted: Mon Aug 07, 2006 6:05 pm
by bokehman
d11wtq wrote:That doesn't work if you saved the file as UTF-8 with your editor. Tried and tested with a lot of hairs pulled out.
Which bit? I don't really see your point, the email should be saved as it would be sent... not in some alien format.

Posted: Mon Aug 07, 2006 6:14 pm
by Chris Corbyn
bokehman wrote:
d11wtq wrote:That doesn't work if you saved the file as UTF-8 with your editor. Tried and tested with a lot of hairs pulled out.
Which bit? I don't really see your point, the email should be saved as it would be sent... not in some alien format.
Yeah, basically you need to make sure the charset matches that of the charset it was saved in.

Also, if the SMTP server does not support 8BITMIME you need to base64 or quoted-printable encode the entire email.

Here's how I handle it:

Detecting for UTF-8 ( Graçias in UTF-8 is not the same as Graçias in iso-8859-1 (editor settings) )

Code: Select all

/**
	 * Detect if a string contains multi-byte non-ascii chars that fall in the UTF-8 tanges
	 * @param mixed input
	 * @return bool
	 */
	public function detectUTF8($string_in)
	{
		foreach ((array)$string_in as $string)
		{
			if (preg_match('%(?:
			[\xC2-\xDF][\x80-\xBF]				# non-overlong 2-byte
			|\xE0[\xA0-\xBF][\x80-\xBF]			# excluding overlongs
			|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}	# straight 3-byte
			|\xED[\x80-\x9F][\x80-\xBF]			# excluding surrogates
			|\xF0[\x90-\xBF][\x80-\xBF]{2}		# planes 1-3
			|[\xF1-\xF3][\x80-\xBF]{3}			# planes 4-15
			|\xF4[\x80-\x8F][\x80-\xBF]{2}		# plane 16
			)+%xs', $string)) return true;
		}
		return false;
	}
Then for the headers I check if they contain UTF-8 using that function, and if they do, I encode them like you did but with a UTF-8 charset.

For the message body I apply the same principle by checking if it;'s UTF-8 then setting the charset of the email to UTF-8.

For the 8BITMIME issue (some servers will deny your email if it's contains characters outside the 7-bit asciii range since it doesn't fall into the generic SMTP implementation) I do this where $string is the ESMTP extension list of the server:

Code: Select all

/**
	 * Check if the server allows 8bit emails to be sent without quoted-printable encoding
	 * @param string EHLO response
	 */
	private function check8BitMime($string)
	{
		if (strpos($string, '8BITMIME')) $this->_8bitmime = true;
	}
If that does not return true the message is quoted-printable encoded. There's a lot of stuff involved with 8bit mail sending that people aren't aware of. :)

Posted: Mon Aug 07, 2006 6:26 pm
by bokehman
I see what you are getting at now. When a mail client sends an email it waits for the result of the EHLO before building the mail. And then builds the email based on that response. But what happens if a mail exchanger further down the chain finds the coding unacceptable? Does the sending mail exchanger rewrite the email to a different format?

Posted: Mon Aug 07, 2006 6:38 pm
by Chris Corbyn
bokehman wrote:Does the sending mail exchanger rewrite the email to a different format?
Glad you asked :)

The good MTAs do recode the email to make it supported. The bad ones do not or at least, they do it wrongly.

Read this: http://en.wikipedia.org/wiki/8BITMIME