PHP Mail Special Characters Utf8

php mail special characters utf8

Did you try iconv_set_encoding ?

This should work :

<?php
iconv_set_encoding("internal_encoding", "UTF-8");

$subject = "Testmail — Special Characters";
$msg = "Hi there,\n\nthis isn’t something easy.\n\nI haven’t thought that it’s that complicated!";

mail(utf8_decode($to), utf8_decode($subject), utf8_decode($msg), utf8_decode($from)."\nContent-Type: text/plain; charset=UTF-8\nContent-Transfer-Encoding: 8bit\n");?>

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"

PHP send email message coding in utf-8

mail($to,utf8_decode($subject),utf8_decode($message),utf8_decode($headers));

how can I send emails where the subject has special characters?

PHP has case sensitive class properties, which means that when a property has a name CharSet, it has to be defined like this and not in lowercase $mail->charset. This also aplies to $mail->$subject and $mail->addAddress, addAddress case is like this. Here is an example with greek text that's working without any text conversion:

EDIT

I've tested this code above and it's working:

$email = 'some@email.com';
$message = 'Όνομα: <br/>'.
'eMail: <br/>'.
'Τηλέφωνο: <br/>'.
'(Ç, ç, äöüßÄÖÜ sollte hier gehen, açores)';

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

$mail = new PHPMailer(true);

try {
$mail->isMail();
$mail->addAddress($email);
$mail->setFrom('test@email.com', 'Mailer');
$mail->CharSet = PHPMailer::CHARSET_UTF8;
$mail->Subject = "[ΣΥΜΜΕΤΟΧΗ] Νέα φόρμα δήλωσης συμμετοχής (Ç, ç, äöüßÄÖÜ sollte hier gehen, açores)";

$mail->msgHTML($message);
$mail->send();
echo 'Message has been sent';
} catch(Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

And here is the screenshot of the received email:

Result email screenshot

PHP mail - special characters don't work

As noted by Deadooshka in its comment, you have to change the line

$from .= "Content-type: text/html; charset=UTF-8;";

to

$from .= "Content-Type: text/html; charset=utf-8\r\nContent-Transfer-Encoding: base64\r\n";

PHP mail special characters in subject field

Try for subject:

$sub = '=?UTF-8?B?'.base64_encode($subject).'?=';

And then:

mail($to, $sub, $message, $headers);

Use special characters in PHP mail() function

Make sure you set your internal encoding to UTF8 with iconv_set_encoding.



Related Topics



Leave a reply



Submit