PHP send email message coding in utf-8
mail($to,utf8_decode($subject),utf8_decode($message),utf8_decode($headers));
How can I send email with UTF-8 encoding?
You are using Content-Type: text/plain; charset=UTF-8
to tell the mail reader that such message part uses UTF-8, which is fine, but... What does the $textContent
variable contain? That's the important bit. According to Content-Transfer-Encoding: 7bit
, it's a 7 bit encoding so it can't be raw UTF-8. However, you are not using any of the usual 7-bit encodings used for e-mail. Otherwise, there would be a (e.g.) Content-Transfer-Encoding: quoted-printable
header.
To sum up, you need to:
- Have a source string that contains valid UTF-8.
- Pick a encoding for the transfer, such as quoted_printable_encode().
- Add a header to tell which transfer encoding you chose.
You could also send the raw UTF-8 as-is and set Content-Transfer-Encoding: 8bit
but I would not recommend it. You risk breaking the SMTP standard just by sending very long lines. Also, you have no idea of what kind of legacy programs this will go through.
E-mail is harder than it seems, that's why sooner or later you end up using a third-party library: PHP Mailer, Swift Mailer, PEAR Mail...
How to send utf-8 e-mail?
You should just add 'utf-8'
argument to your MIMEText
calls (it assumes 'us-ascii'
by default).
For example:
# -*- encoding: utf-8 -*-
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
msg = MIMEMultipart("alternative")
msg["Subject"] = u'テストメール'
part1 = MIMEText(u'\u3053\u3093\u306b\u3061\u306f\u3001\u4e16\u754c\uff01\n',
"plain", "utf-8")
msg.attach(part1)
print msg.as_string().encode('ascii')
Sending UTF-8 email through Postfix sendmail utility
You can't MIME-encode email address. Support for "Internationalized Email Headers" is described in RFC 6532. You need to make sure that all SMTP servers your emails are going through support SMTP Extension for Internationalized Email. See also Overview and Framework for Internationalized Email
Related Topics
Special Characters in Property Name of Object
How to Perform Static Code Analysis in PHP
PHP - Get Bool to Echo False When False
Using Jquery Ajax to Retrieve Data from MySQL
Get the Last Inserted Id Using Laravel Eloquent
Simple Jquery, PHP and Jsonp Example
Insert Array into MySQL Database With PHP
Max Size of Url Parameters in _Get
Redraw Google Chart Based on User Input Via Ajax Request
Example of How to Use Bind_Result VS Get_Result
What Kind of String Is This? How to Unserialize This String
PHP Fatal Error: Cannot Redeclare Class