php mail - special character
hai
I have a Spanish site in php. In this a mail body contain a subject "Solicitud de cotización" but the subject appears in hot mailbox like Solicitud de cotización. But it's appear correct开发者_如何学C in mail section. How I avoid this problem. Does any one know this?
Hai! =) To encode the subject you have to do this:
$subject = 'Solicitud de cotización';
// =?UTF-8?B?U29saWNpdHVkIGRlIGNvdGl6YWNpw7Nu?=
$subject = '=?UTF-8?B?' . base64_encode($subject) . '?=';
If you're using PHP 5.3+ you can use quoted_printable_encode()
instead:
$subject = 'Solicitud de cotización';
// =?UTF-8?Q?Solicitud de cotizaci=C3=B3n?=
$subject = '=?UTF-8?Q?' . quoted_printable_encode($subject) . '?=';
You need to encode the subject header in whatever text encoding you are using. See http://metacpan.org/pod/Encode::MIME::Header and http://www.faqs.org/rfcs/rfc2047.html which talk about doing so.
Essentially, your subject line should look like this:
Subject: Solicitud de =?UTF-8?Q?cotizaci=C3=B3n?=
Then any MUA that knows about MIME should render the subject correctly, using the correct character set.
EDIT: It is worth mentioning that RFC2822 specifies ASCII as the character encoding for mail message headers, which is why the quoting is necessary. Also it specifies that lines should not be longer than 72 characters, so folding may be necessary and you should take that into consideration when generating messages destined for processing in RFC(2)822 mail systems. Finally, use of the B encoding does not make a great deal of sense for a string like you have supplied, since the Q encoding takes up less space (and for that matter, you only want to quote a run of words that actually use characters outside of the ASCII character set).
It is technically possible to just brute-force with B encoding for the whole string, but it's generally speaking bad form due to being wasteful, and it is much more likely that you will exceed the hard limit of 9,999 characters in a single line specified by the RFC(2)822 standards that way if ever you have a long subject line.
I had the same issue, the way that now it works good for me is using a class (phpMailer):
<?php
require_once('class.phpmailer.php');
$mail = new PHPMailer(); // defaults to using php "mail()"
// Here's the code that allows special chars in subject and body
$mail->CharSet = 'UTF-8';
$mail->Encoding = 'quoted-printable';
// From and reply data
$mail->AddReplyTo('name@yourdomain.com', 'First Last');
$mail->SetFrom('name@yourdomain.com', 'First Last');
$mail->AddReplyTo('name@yourdomain.com', 'First Last');
// Receiver
$address = 'whoto@otherdomain.com';
$mail->AddAddress($address, 'John Doe');
// Message
$mail->Subject = 'PHPMailer Test Subject via mail(), basic';
$mail->Body = 'Message sent from website';
// Attachment(s)
$mail->AddAttachment('images/phpmailer.gif');
$mail->AddAttachment('images/phpmailer_mini.gif');
// Try to send mail
if( ! $mail->Send())
{
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
else
{
echo 'Message sent!';
}
?>
Try setting the charset
to utf-8
in the email headers:
charset=UTF-8
Or see:
UTF8 ready php mail function
UTF8_mail(
“fromname <my@email.com>”,
“First Last <your@email.com>”,
“Solicitud de cotización”,
“Κείμενο Text”,
“”,
“Solicitud de cotización<hidden_friend@email.com>”
);
精彩评论