Question

Codes:

$mail = new PHPMailer();
$mail->isSendmail();
$mail->setFrom('info@rene.org', 'Rene');
$mail->addAddress($_POST['email'],$_POST['name']);
$mail->Subject = 'Confirmación';

Ive used the own "ó", and the subject will appear as: Confirmación in my email account. I've also tried:

$mail = new PHPMailer();
$mail->isSendmail();
$mail->setFrom('info@rene.org', 'Rene');
$mail->addAddress($_POST['email'],$_POST['name']);
$mail->Subject = 'Confirmación';

And it appear like: Confirmaci & o a c u t e ;n. How can i solve this guys. It needs to appear on emails inboxes like: Confirmación

Was it helpful?

Solution

You seem to be encountering your first character encoding bug. Congratulations! You've taken your first step into a world of crippling legacy and enormous mistakes that shall be preserved for all of time. Let's dive right in.

Ive used the own "ó" and the subject will appear as: Confirmación in my email account.

Before I begin explaining why this happened, I want to make something clear: you had every reason to believe this would work. In a perfect world, it would have. Your tools have failed you.

When you type 'ó' in your PHP source, you're not actually telling PHP what you think you are. What you want to say is, "Give me a string containing the codepoint U+00F3 LATIN SMALL LETTER O WITH ACUTE". PHP doesn't speak Unicode, though; its strings are actually byte vectors. What PHP heard is "Give me the byte vector < C3 B3 >", and so it does. By the time the message hits your screen, something has decided those bytes must be characters encoded according to Microsoft Codepage 1252:

$ echo 'ó' | iconv -f cp1252
ó

This is probably PHPMailer behavior, but the documentation for PHPMailer::$Subject only says

The Subject of the message.

so that's not exactly a big help.


Okay, I've dug around on the PHPMailer bug tracker a bit, and I found this conversation. There's a lot of stuff there that doesn't apply to this situation, but I did manage to learn that the PHPMailer::$CharSet property also applies to the headers. (Documentation: "The character set of the message.") So it appears the correct fix for your situation is to add the line

$mail->CharSet = 'UTF-8';

to the bottom of your first script.

OTHER TIPS

Take a look at this post and try the options there, setting the encoding should solve it.

php mail special characters utf8

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top