Search code examples
phpemailcharacter-encodingheadermime

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


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!


Solution

  • 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);