Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP mail special characters in subject field

I would like to insert special characters in the subject of HTML e-mails sent with the PHP mail() function.

I want my subject to look like this:

★ Your new account

I have tried with an HTML entity like ★ (★) or by pasting the symbol directly in my code but that doesn't work either, except on a few e-mail clients.

$to = '[email protected]';
$subject = '★ Your new account or ★ Your new account';
$message = 'HTML message...';

$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= 'From: Me <[email protected]>' . "\r\n";

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

Any advice on how to get this to work properly? Thank you.

like image 809
MrUpsidown Avatar asked Dec 27 '13 18:12

MrUpsidown


People also ask

Can email subjects have special characters?

Special characters are supported in the subject line, preheader, and body of your email. However, you should consider using them sparingly, as too many could land your email in your contacts' junk or spam folders.

Can you use symbols in subject lines?

The answer is: sometimes. There are pros and cons to using special characters in email subject lines. Generally, marketers report higher open rates. Some report better engagement, but many don't.

What is PHP mail () function?

PHP mail() function is used to send email in PHP. You can send text message, html message and attachment with message using PHP mail() function.

Can you use emojis in email preheader?

What about emojis in the preheader? Luckily, preheaders support emojis, even in an email client that does not render them correctly in the subject line. The preheader is part of the message body, which renders HTML like your <table> and <img>.


2 Answers

Try for subject:

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

And then:

mail($to, $sub, $message, $headers);
like image 78
Lkopo Avatar answered Oct 14 '22 06:10

Lkopo


While the accepted answer works fine, it makes it impossible to read the subject when looking at the raw headers. Here's an alternative that keeps the line readable and also shorter if it consists of mostly ascii characters.

$subject = '=?UTF-8?q?' . quoted_printable_encode($subject) . '?=';

Here's the encoded subject line of the accepted answer:

=?UTF-8?B?4piFIFlvdXIgbmV3IGFjY291bnQ=?=

Here's the encoded subject line of my answer:

=?UTF-8?q?=E2=98=85 Your new account?=

Edit:

It turns out quoted_printable_encode() splits long strings into multiple lines of max 75 characters, as required per RFC 2045. This resulted is a string that cannot be used with mail()'s $subject parameter. Here's an updated version to fix this. It will also avoid encoding pure-ascii subjects.

/**
 * Make sure the subject is ASCII-clean
 *
 * @param string $subject Subject to encode
 *
 * @return string Encoded subject
 */
function getEncodedSubject(string $subject): string {
    if (!preg_match('/[^\x20-\x7e]/', $subject)) {
        // ascii-only subject, return as-is
        return $subject;
    }
    // Subject is non-ascii, needs encoding
    $encoded = quoted_printable_encode($subject);
    $prefix = '=?UTF-8?q?';
    $suffix = '?=';
    return $prefix . str_replace("=\r\n", $suffix . "\r\n  " . $prefix, $encoded) . $suffix;
}

Explanation:

$subj = "Lorem ipsuöm dolor sit amet, consectetur adipiscing elit. Praesent mattis molestie purus, non semper lectus dictum eget.";

After quoted_printable_encode

Lorem ipsu=C3=B6m dolor sit amet, consectetur adipiscing elit. Praesent mat=
tis molestie purus, non semper lectus dictum eget.

After str_replace

=?UTF-8?q?Lorem ipsu=C3=B6m dolor sit amet, consectetur adipiscing elit. Praesent mat?=
  =?UTF-8?q?tis molestie purus, non semper lectus dictum eget.?=
like image 9
jlh Avatar answered Oct 14 '22 05:10

jlh