Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP E-Mail Encoding?

I am having some trouble with foreign characters when sending an e-mail. Could someone advise me on what to do? I suspect the problem could be one of three things.

  1. The html page encoding is incorrect. (Would this affect the POST data from the form?)
  2. The mail function doesn't have any encoding. Thus the program doesn't know how to read it. (Most likely)
  3. The file itself doesn't have the right encoding and thus is making problems. (Probably quite unlikely)

Are there any other possible causes?

I am trying to knock these out 1 by 1 until I find the problem. I think that option 2 is the most likely cause. How do I add proper - universal encoding to a mail function?

This is what I have at the moment.

$mail_sent = mail($client_email, $title, $message, "From: {$visitor_email}"); 

I am currently aware that form does not send polish or Swedish characters.

I would be very grateful if someone could point out any other possible causes and tell me what encoding I need to use to send e-mails.

Thanks a lot.

like image 385
JasonS Avatar asked Feb 15 '10 11:02

JasonS


People also ask

How does PHP mail work?

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. mail( to, subject, message, headers, parameters );

Does PHP mail function work on localhost?

But the PHP mail() function will not work in localhost. In this tutorial, we'll show how you can send email from localhost in PHP. Using this example script you can send email from any localhost server (XAMPP, WAMP, or any others) using PHP. We will use the PHPMailer library to send emails from localhost using PHP.

How do I add a charset to my email?

If you prefer, you can also change the default charset for all your future emails. Just select Settings (in the sidebar panel), click on Advanced Settings and scroll until you see the Messages section. From there you can select your new charset from the list.


2 Answers

As far as I know PHP does not support UTF-8 as default encoding for its strings. You need to use the relevant encoding/handling functions for the encoding you would prefer.

Also add a Content-Type:text/html;charset=utf-8 to your email headers so the email clients will display the characters correctly (or replace with your encoding of choice).

like image 135
Belrog Avatar answered Sep 21 '22 17:09

Belrog


You didn’t specify the type and encoding of your content. Try this:

$headerFields = array(     "From: {$visitor_email}",     "MIME-Version: 1.0",     "Content-Type: text/html;charset=utf-8" ); $mail_sent = mail($client_email, $title, $message, implode("\r\n", $headerFields)); 
like image 35
Gumbo Avatar answered Sep 20 '22 17:09

Gumbo