mail() - spanish accents

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
alexislalas
Forum Commoner
Posts: 44
Joined: Sun Feb 19, 2006 10:09 pm

mail() - spanish accents

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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. :)
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post 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?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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
Post Reply