Using mail() to sent UTF8 e-mail - php

I'm trying to send an e-mail with PHP, including multi-language characters. The whole site (html header, php header) are set to UTF8 aswell as the form charset.
To PHP code I have now is:
$to = "email#email.com";
$subject = "Subject";
$message = "Question is ".$question;
$from = "auto#from.com";
$headers = "From:" . $from . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-UTF-8' . "\r\n";
$sendmail = #mail($to,$subject,$message,$headers);
I supose I'm wrong somewhere ?
I'm getting '?' characters with Japanese letters for examnple.

$headers .= "MIME-Version: 1.0"."\r\n";
$headers .= "Content-type: text/html; charset=utf-8"."\r\n";
should be all you need.
I may be wrong, but as far as I know iso-UTF-8 is not a valid charset, at least it doesn't look like it is.

Related

Send apostrophe s ('s) in email message using was error encode using PHP?

Send apostrophe s ('s) in email message using was error encode using PHP ?
<?PHP
include("connect.php");
$to = "example#example.com";
$subject = "TEST SUBJECT";
$message = "peter’s test";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: EXAMPLE <noreply#example.com>' . "\r\n";
$headers .= 'Return-Path: return#example.com' . "\r\n";
mail($to, $subject, $message, $headers, '-freturn#example.com');
?>
When i open my email , i see peter’s ’s test
How can i send apostrophe s ('s) in email message using php ?
you need to use mb_convert_encoding on the string containing the apostrophe:
$message = mb_convert_encoding("peter’s test", 'UTF-8');
You are using the character encoding charset=iso-8859-1 in your mail header. I used utf-8 and it worked fine. Try replacing charset=iso-8859-1 with charset=utf-8

PHP mail() changing some characters to htmlentities?

My PHP mail() script changes the ! exclamation mark on some email clients such as hotmail to %21 when the ! is in a tag in the email body.
Here's my script
$to = "myemail#outlook.com";
$subject = "Password Reset";
$body = "Link 1
<br><br>
Without href: http://example.com/#!/page - regular text
";
$headers = "From: no-reply#example.com\r\n";
$headers .= "Reply-To: no-reply#example.com\r\n";
$headers .= "Return-Path: no-reply#example.com\r\n";
$headers .= "X-Mailer: PHP5\n";
$headers .= 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
mail($to,$subject,$body,$headers);
So in the script above, the ! is changed to %21 only when it's a link, the regular text keeps it as /#!/instead of transforming it to /#%21/
How would I go about fixing this issue so it doesn't change to %21?
Some characters are encoded because they are not valid URLs. In your case URL code points are replaced with percent-encoded byte %.

PHP mail command won't work if "http://" is in the body

Strangest thing I've ever seen. If the string "http://" is used in the mail body, the mail command does not work. If any other combo of these characters is used, it DOES send. For example I can type "ttp://" or "http:/" and that will send fine. As soon as I have the exact string "http://" anywhere in the body, the mail does not send.
Doesn't work:
$mail_body = 'http://';
$subject = "Test subject";
$recipient = "myemail#myemail.com";
$header = "MIME-Version: 1.0\n" ;
$header .= "Content-type: text/html; charset: utf8\r\n";
$header = $header . 'From: test#test.com';
mail($recipient, $subject, $mail_body, $header) or die('mail could not be sent');
The above DOES work if you change $mail_body, for example:
$mail_body = 'ttp://';
or
$mail_body = 'http:/';
or
any other combination of any other characters imaginable, just not when it says http://
If you are adding "http" in your body, try to send email using "html" format. It will work, since we have send may emails contains link "http"
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
Use the above character set and your code will work
Replace http:// with http%3A//

Sending Hebrew subject in php mail goes Klingon...?

I'm trying to send email with hebrew content/subject like so:
$to = 'email#email.com';
$subject = "איזה יום יפה היום";
$message = 'ממש יום יפה';
$headers = 'From: email#email.com' . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to, $subject, $message, $headers);
But what I get in the subject is more Klingon than modern Hebrew. The message itself comes out fine, it's just the subject that's all messed up.
What can I do? (I'm open to any hacks you got)
The Content-Type does only describe the message content but not the header. You need to apply the encoded-word encoding on the Subject value. See my answer on PHP email header subject encoding problem for further information.

How do I send emails with Arabic content via PHP's mail function?

I'm having a challenge with sending emails with arabic content using PHP's mail function. Let's say I have this simple arabic string:
بريد
I've tried several ways to utilize the headers, but the emails content all still end up with something like: X*X1X(X1Y X/. However, the email subject is correctly encoded if I use arabic characters (thanks to the base64_encode, see function below)
Here's one of the email functions I've tried
function sendSimpleMail($to,$from,$subject,$message) {
$headers = 'MIME-Version: 1.0' ."\r\n";
$headers .= 'To: '.$to ."\r\n";
$headers .= 'From: '.$from . "\r\n";
$headers .= 'Content-type: text/plain; charset=UTF-8; format=flowed' . "\r\n";
$headers .= 'Content-Transfer-Encoding: 8bit'."\r\n";
mail($to, '=?UTF-8?B?'.base64_encode($subject).'?=',$message, $headers);
}
Any suggestions on alternative ways to achieve this goal?
Unfortunately, 8bit encoding is not reliable in e-mail. Many mail transport agents will remove the top bit of every byte in the mail body. بريد is "\xD8\xA8\xD8\xB1\xD9\x8A\xD8\xAF" in UTF-8 bytes; remove the top bit from those bytes and you get ASCII "X(X1Y\nX/".
The way to get non-ASCII characters into a mail body is to set Content-Transfer-Encoding to either base64 or quoted-printable, and the encode the body with base64_encode or quoted_printable_encode, respectively.
(quoted-printable is better if the mail is largely ASCII as it retains readability in the encoded form and is more efficient for ASCII. If the whole mail is Arabic, base64 would probably be the better choice.)
$boundary = uniqid(rand(), true);
$headers = "From: $from\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/alternative; boundary = $boundary\n";
$headers .= "This is a MIME encoded message.\n\n";
$headers .= "--$boundary\n" .
"Content-Type: text/plain; charset=UTF-8 \n" .
"Content-Transfer-Encoding: base64\n\n";
$headers .= chunk_split(base64_encode($plaintext));
$headers .= "--$boundary\n" .
"Content-Type: text/html; charset=ISO-8859-1\n" .
"Content-Transfer-Encoding: base64\n\n";
$headers .= chunk_split(base64_encode($msg));
$headers .= "--$boundary--\n" .
mail($address, $subject, '', $headers);
This one works for me.
Try this
$headers .= 'From: =?UTF-8?B?'.base64_encode($from). "\r\n";
Your code works for me as-is.
Are you sure that $message contains a valid UTF-8 string?

Categories