Skip to content
Advertisement

PHP Mail header for emails with special characters in the subject or message

My code:

$to      = 'example@example.com';
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
$header = "From: noreply@example.comrn"; 
$header.= "MIME-Version: 1.0rn"; 
$header.= "Content-Type: text/html; charset=ISO-8859-1rn"; 
$header.= "X-Priority: 1rn"; 

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!

Advertisement

Answer

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      = 'example@example.com';
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
$header = "From: noreply@example.comrn"; 
$header.= "MIME-Version: 1.0rn"; 
$header.= "Content-Type: text/plain; charset=utf-8rn"; 
$header.= "X-Priority: 1rn"; 

mail($to, $subject, $message, $header);
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement