Discussions


Blogroll

.

PHP mail() function -character encoding and newline issues solved

Published by admin in PHP at August 21, 2008 | 1 x discussed | | | | |

I have created my firs PHP script for sending emails directly from a website (isnt included on Soshallnetwork -yet). It is an AJAX form, which collects user data. In particular it collects name, phone no. email and of course the message to be mailed to a specific adress.

 

Following examples in the PHP manual I quickly constucted the usual function - mail($to,$subject,$longmsg,$headers), where variable values come from the AJAX generated form results.

 

Things worked pretty quickly and the script was functional after 15 minutes of work. I had two problems though, which took me two hours of googling and forum scanning.

 

Namely:

  1. UTF-8 encoding worked only if I sent mail to a gmail account, but not in Outlook, which meant I lost the special language characters encoded in UTF-8 format (site is in Slovenian language)
  2. Whatever I seemed to try, I couldn`t  get the lines to brake in mail body (neither " \n " - nor " \r\n " -  commands didnt work.

 

Sollution to 1.:

Use $headers variable (in the proper order inside mail() function ) to assign character encoding to be sent with the message.

So besides the usual $headers = 'From: ' .$email ; add 'Content-type: text/plain; charset=UTF-8' to $headers. You might want to do it like so:

 

$headers = 'Content-type: text/plain; charset=UTF-8' . "\r\n";
$headers .= 'From: ' .$email. "\r\n";

 

[note: using "\r\n" is a must otherwise it may not work properly]

 

Sollution to 2.:

Originally I used $headers = 'Content-type: text/html; charset=UTF-8, but my form does not provide any html elements as output, as those are trimmed away in the processing script, before sent via email.

 

I spent some hours searching for the sollution, couldnt find why my newline command " \n " wont work, finally I found the sollution: declare Content-type as text/plain if you wish to declare line break with " \n " or  "\r\n". If <"br"> is prefered Content-type must be text/html.

 

So the sollution for me was:

 

 

$headers = 'Content-type: text/plain; charset=UTF-8' . "\r\n";
$headers .= 'From: ' .$email. "\r\n";

 

In particular:

 

$longmsg =  $name. "\r\n" ;
$longmsg .= ", telefonska stevilka:" .  trim($_GET["phone"]) . "\r\n";
$longmsg .= "pravi: ". trim($_GET["msg"]) ;

 

This combained would brake lines at "\r\n" command. If you wih to use html line brake instead of "\r\n" or if you wish the mail be sent as html, change Content-type: accordingly. Also if you wish to send images or other files, the answer is in propper Content-type: settings, wich are sent with the email itself as part of $headers variable.

 

Conslusion. Even a slight trivial feature as making characters appear as wanted in PHP generated "on-line" mail and a wish to break the online generated message to multiple lines can cost you hours of exploration. I hope this article helps some one.

 


April 29, 2010, 22:09 | dfasfas said:

teasrt


Does that make sense?