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

57,668

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/

Share:
57,668
Laurent
Author by

Laurent

Updated on July 09, 2022

Comments

  • Laurent
    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: ø&#700;ª&#700;

    Thanks in advance!

  • Laurent
    Laurent about 11 years
    Thanks, but by doing this i do get spacing right, but the output is like �����&#717;�&#711;&#730;
  • Laurent
    Laurent about 11 years
    thanks, this solves the spacing problem and the caracters in the subject. The message however gives me something like: ¨®ð¥þƒ&#717;©&#711;&#730;
  • TFennis
    TFennis about 11 years
    Updated my answer with a suggestion to fix that issue as well. Let me know if it worked
  • Laurent
    Laurent about 11 years
    this also keeps the spacing but outputs something like ����.�&#730;��&#730; in both the subject and the message
  • Babblo
    Babblo about 11 years
    Be sure you are setting the right charset in the mail() funcion and also save the file in the same charset.
  • TFennis
    TFennis about 11 years
    couldn'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
    oelna almost 9 years
    Is there any benefit in using \r\n over PHP_EOL?
  • Ivijan Stefan Stipić
    Ivijan Stefan Stipić over 6 years
    @oelna allways use PHP_EOL to be shure you have right encoding.
  • movAX13h
    movAX13h over 6 years
    When 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
    Ghigo over 6 years
    When 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.