Utf-8 Encoding for Subject in Contact Form Email

UTF-8 encoding for subject in contact form email

here is how i did it:

$head = "From: \"=?ISO-8859-15?Q?".imap_8bit("äöüßÄÖÜ sollte hier gehen")."?=\" <info@mydomain.de>\n";
$subject = "=?ISO-8859-15?Q?".imap_8bit("äöüßÄÖÜ sollte hier gehen")."?=";
mail($mail,$subject,$text,$head);

that is ofc just Latin-15 (German) encoding. utf-8 works the same way:
look here for a great explanation on how to use character encoding in mail headers:
http://ncona.com/2011/06/using-utf-8-characters-on-an-e-mail-subject/

for your code you have to change this in the sendmail class:

if (mail($this->to, '=?utf-8?B?'.base64_encode($this->subject).'?=', $this->body, $this->headers))

! this only works properly if your php file is utf-8 encoded !

still very annoying. then i switched to phpmailer. that does everything for you. way more easy. i would suggest you use that.

Special characters (utf-8) in PHP contact message

Since you're using UTF-8, consider using php multibyte functions link mb_*. For example:

echo strlen('è'); // output 2
echo mb_strlen('è', 'UTF-8'); // output 1

Other mb_* functions you might be interested in:

strrpos() to mb_strrpos()

substr() to mb_substr()

For full list of multibyte functions, look here.

If you don't want to pass 'UTF-8' in every mb_ function, call mb_internal_encoding('UTF-8') in the beginning of your script.

For the mail() function to work properly with UTF-8, pass the following in the header:

$header = "Content-Type: text/html; charset=UTF-8"

Charset UTF-8 not working on ?php contact form

You aren't setting the mail header there, you are setting the http header. This function header is sending a raw HTTP header, it isn't doing anything for the email you are sending

   header('Content-Type: text/html;charset=UTF-8');

You need to add the header "Content-Type: text/html; charset=UTF-8" (for HTML Email bodies) or "Content-Type: text/plain; charset=UTF-8" (for Plain Text Email bodies) to your mail function. Like this.

$headers = array("Content-Type: text/html; charset=UTF-8");
mail($to, $subject, $message, $headers)

Additionally, for email, each lines should be separated with a CRLF (\r\n) instead of merely using a linefeed (\n). A fully example end result might look more so like this:

<?php

$crlf = "\r\n";

//Get Data
$name = strip_tags($_POST['name']);
$email = strip_tags($_POST['email']);
$service = strip_tags($_POST['service']);
$phone = strip_tags($_POST['phone']);
$phoneconfirm = strip_tags($_POST['phoneconfirm']);
$priority = strip_tags($_POST['priority']);
$subject = strip_tags($_POST['subject']);
$message = strip_tags($_POST['message']);

// Parse/Format/Verify Data
$to = "THETOEMAIL@GOES.HERE";
$from = 'THEFROMEMAIL@GOES.HERE';
$subject = "Via Website";
$message = "De: $name$crlf E-Mail: $email$crlf Serviço: $service$crlf
Telefone/Celular: $phone$crlf Ligar/Retornar: $phoneconfirm$crlf
Prioridade: $priority$crlf Assunto: $subject$crlf Mensagem:$crlf
$message";

// Setup EMAIL headers, particularly to support UTF-8
// We set the EMAIL headers here, these will be sent out with your message
// for the receiving email client to use.
$headers = 'From: ' . $from . $crlf .
'Reply-To: ' . $from . $crlf .
'Content-Type: text/plain; charset=UTF-8' . $crlf .
'Para: WebSite' . $crlf .
'X-Mailer: PHP/' . phpversion();

// Then we pass the headers into our mail function
mail($to, $subject, $message, $headers);
?>

Reference:

  • header function
  • mail function


Related Topics



Leave a reply



Submit