PHP Mail header for emails with special characters in the subject or message
Solution 1
Content-Type: text/html
If you set this header that means you have to send HTML to the user. You can either decide to use something like TinyMCE to let the user write the message in a Word-style editor and use the HTML output from that. Or set your headers to plaintext.
Content-Type: text/plain
EDIT: Try this
$to = '[email protected]';
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
$header = "From: [email protected]\r\n";
$header.= "MIME-Version: 1.0\r\n";
$header.= "Content-Type: text/plain; charset=utf-8\r\n";
$header.= "X-Priority: 1\r\n";
mail($to, $subject, $message, $header);
Solution 2
I have used this header and this is worked for me...
$headers = '';
$headers = 'MIME-Version: 1.0'.PHP_EOL;
$headers .= 'Content-type: text/html; charset=iso-8859-1'.PHP_EOL;
$headers .= 'From: [email protected]<From: [email protected]>'.PHP_EOL;
Solution 3
Don't use mail() function. Use a fully crafted class that does (correctly) the job for you.
http://code.google.com/a/apache-extras.org/p/phpmailer/
Laurent
Updated on July 09, 2022Comments
-
Laurent almost 2 years
My code:
$to = '[email protected]'; $subject = $_REQUEST['subject'] ; $message = $_REQUEST['message'] ; $header = "From: [email protected]\r\n"; $header.= "MIME-Version: 1.0\r\n"; $header.= "Content-Type: text/html; charset=ISO-8859-1\r\n"; $header.= "X-Priority: 1\r\n"; mail($to, $subject, $message, $header);
When i send a mail with special characters such as
®ð-˚©-ʼ“æ,˚ˍðß©
, in the message, it works but spacing is no longer dealt with (every new line or space gets removed) And the second problem is that the special characters are not displayed in the subject. They just output like:øʼªʼ
Thanks in advance!
-
Laurent about 11 yearsThanks, but by doing this i do get spacing right, but the output is like
�����ˍ�ˇ˚
-
Laurent about 11 yearsthanks, this solves the spacing problem and the caracters in the subject. The message however gives me something like:
¨®ð¥þƒˍ©ˇ˚
-
TFennis about 11 yearsUpdated my answer with a suggestion to fix that issue as well. Let me know if it worked
-
Laurent about 11 yearsthis also keeps the spacing but outputs something like
����.�˚��˚
in both the subject and the message -
Babblo about 11 yearsBe sure you are setting the right charset in the mail() funcion and also save the file in the same charset.
-
TFennis about 11 yearscouldn't it be that the request parameters you get are broken then? Try var_dump($_REQUEST) and see what kind of data you get.
-
oelna almost 9 yearsIs there any benefit in using \r\n over PHP_EOL?
-
Ivijan Stefan Stipić over 6 years@oelna allways use
PHP_EOL
to be shure you have right encoding. -
movAX13h over 6 yearsWhen I see something like 'composer' or 'autoload' for a simple task like sending mails, I immediately close the tab. This is no solution, it's a ton of new problems.
-
Ghigo over 6 yearsWhen some hacker will exploit your mail() routine or all your email will go into spam, you'll reopen the tab :) Btw I'm using plain library, no auto load, no composer. Just 2 includes.