开发者

Spanish Accent Marks in Form Submission

I have a form where spanish can be submitted and using PHP, I send an emai开发者_运维知识库l with the data. Unfortunately, the accent marks get totally screwed up when they get emailed.

If I submit the following:

Testing Accent Marks á é í ó ú ñ

I end up with the following in the body of my email...

Testing Accent Marks á é à ó ú ñ

The code that is processing the email is simply placing the $_POST info directly into the body of the email. I assume I need to have htmlentities() or something but I have tried and nothing works...

I also will need to be placing the same data into a MySQL database and retrieving it later. What do I need to be aware of when I do that?

Thanks! Drew


You have touched the fine subject of charsets. Try to create and convert everything to utf-8. Database, files, forms and the like. Look up some information on the internet about what header to use in the e-mail to make it utf-8. Also convert you e-mails from standard 7-bit to 8-bit with these headers.

"Content-Type: text/plain; charset=utf-8"
"Content-Transfer-Encoding: 8bit"

For the database you need to set, in case of mysql, the collation.


To answer your question about storing data in the database:

After you have opened a connection to MySQL using mysql_connect(), send the following query as your first command:

mysql_query("SET NAMES utf8");

(or mysqli_query(), depending on the library you use).

This command lets MySQL database know that:

  • all data you'll be providing will be encoded as UTF-8, and
  • you also want to receive everything in UTF-8.

Strictly speaking, the database itself can be in whatever collation and encoding (MySQL is happy to transcode stuff for you on the fly), but of course, it's best to have the database in UTF-8 as well.


If you're using utf8 subjects, you need to base64 encode them as such first:

$subject = "=?utf-8?b?".base64_encode($subject)."?=";

And set the content type as such:

$headers.="Content-Type: text/html; charset=utf-8\r\n";

I also recommend you use a system like SwiftMailer to prevent header injection attacks. For the database part, I believe that would be better off as a separate question to get proper tags and more focused responses.


Your script is receiving data encoded in UTF-8, but your email client believes that the email is encoded in Latin-1. There are two ways you might deal with this: add a header to the email indicating that the character encoding is UTF-8, or use utf8_decode() on the submitted content (if you are sure you will only ever use Latin-1 characters).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜