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:
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
Ruby and PHP Hmacs Not Agreeing
Using Sprockets as a Standalone Service for a PHP Application
How So I Convert This Sha256 + Base64 from Swift to PHP
Preg_Match Equivalent in Swift
How to Send a Image to a PHP File Using Swift
Connect to PHP Server Using Swift
Swift Sync Data of Core Data an MySQL Database
Automatically Detect User's Current Local Time with JavaScript or PHP
Data from Two Tables with Same Column Names
Laravel 4 Custom Named Password Column
Long Integer Is Transformed When Inserted in Shorter Column, Not Truncated. Why? What Is the Formula
Format Text in Excel File via PHP
Avoid Public Folder of Laravel and Open Directly the Root in Web Server
Correct Indentation of HTML and PHP Using Vim
Laravel Mail: Pass String Instead of View