Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 902
Laurent Avatar asked Mar 06 '13 14:03

Laurent


People also ask

Can email subjects have special characters?

The best practice is to use one special character per email subject line. At most, use two. But if you remember back to our prior example — the one with the 1, 2, 3 special characters — that also broke the no-more-than-two rule.

Can you use PHP in email?

Sending plain text email PHP makes use of mail() function to send an email. This function requires three mandatory arguments that specify the recipient's email address, the subject of the the message and the actual message additionally there are other two optional parameters.

What is a PHP email?

PHP mail is the built in PHP function that is used to send emails from PHP scripts. The mail function accepts the following parameters: Email address. Subject. Message.


2 Answers

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);
like image 196
TFennis Avatar answered Sep 22 '22 06:09

TFennis


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; 
like image 21
user3820294 Avatar answered Sep 22 '22 06:09

user3820294