I have a word with a special character in it that does not display properly in the subject line of an email (via mail()).
The word is O’Conner and the special character is ’
"as is" the word appears as O’Conner
in the subject line of the incoming email message.
When I use html_entities_decode()the word appears as O’Conner
in the subject line of the incoming email message.
When I use mb_encode_mimeheader("O’Conner", "UTF-8", "Q") the word appears as O’Conner
in the subject line of the incoming email message.
Is there a way to get the word to appear is it should?
Other similar or seemingly duplicate question/answers do not see to resolve this particular issue.
I even tried UTF-7 -- that did not work.
Here's the email function:
/**
* Send an html/smtp email
*/
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
function htmlEmailer( $email, $type, $parameter ) {
require('/etc/phpmailer/vendor/phpmailer/phpmailer/src/Exception.php');
require('/etc/phpmailer/vendor/phpmailer/phpmailer/src/PHPMailer.php');
require('/etc/phpmailer/vendor/phpmailer/phpmailer/src/SMTP.php');
mb_internal_encoding('UTF-8'); // 2021-06-18 added per https://stackoverflow.com/questions/7669668/how-to-use-special-characters-in-recipients-name-when-using-phps-mail-function/7670192#7670192
// Instantiation and passing "true" enables exceptions
$mail = new PHPMailer(true); // create a new object
//Server settings
$mail->SMTPDebug = 0; // see best_practices.txt file for settings
$mail->isSMTP(); // set mailer to use SMTP
$mail->Host = SMTP_SERVER; // specify main and backup SMTP servers separated by semi-colon (defined in config.php)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Username = SMTP_USER; // SMTP username (defined in config.php)
$mail->Password = SMTP_PASSWORD; // SMTP password (defined in config.php)
$mail->SMTPSecure = 'tls'; // enable encryption [tls or ssl]
$mail->Port = 587; // TCP port to connect to [465 or 587]
//Recipients
$mail->setFrom('info@example.com', mb_encode_mimeheader(SENDER_LAST_NAME, 'UTF-8', 'Q'));
$mail->addAddress($email, ''); // add a recipient [2nd parameter "recipient's name" is optional and is separated by a comma]
$mail->addReplyTo('no_reply@example.com', 'Do Not Reply');
//$mail->addCC('me@example.com');
$mail->addBCC('you@example.com');
// Attachments
//$mail->addAttachment('/home/cpanelusername/attachment.txt'); // Add attachments
//$mail->addAttachment('/home/cpanelusername/image.jpg', 'new.jpg'); // Optional name
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'The message subject';
$mail->Body = 'HTML message body'; // html formatted body
$mail->AltBody = "Plain Text message body"; // plain text body
// Send the Message
$mail->send();
} // htmlEmailer()