PHP mail special characters in subject field

2019-02-07 03:25发布

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 = 'me@example.com';
$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 <me@example.com>' . "\r\n";

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

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

2条回答
姐就是有狂的资本
2楼-- · 2019-02-07 03:50

Try for subject:

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

And then:

mail($to, $sub, $message, $headers);
查看更多
太酷不给撩
3楼-- · 2019-02-07 03:53
 $headers="";    
 $message = utf8_encode($message);
 $message = wordwrap($message, 70, "\r\n");
 $subject = '=?UTF-8?B?'.base64_encode(utf8_encode($subject)).'?=';
 $to_name = '=?UTF-8?B?'.base64_encode(utf8_encode($to_name)).'?=';
 $from_name = '=?UTF-8?B?'.base64_encode(utf8_encode($from_name)).'?=';

 $headers .= "MIME-Version: 1.0" . "\r\n"; 
 $headers .= "Content-type: text/plain; charset=utf-8" . "\r\n"; 
 //$headers .= "Content-Transfer-Encoding: quoted-printable" . "\r\n"; 
 $headers .= "From: $from_name <$from_email>" . "\r\n"; 
 $headers .= "To: $to_name <$to_email>" . "\r\n";
 $headers .= "Subject: $subject" . "\r\n";
 $headers .= "X-Mailer: PHP/" . phpversion();  

 mail("", $subject, $message, $headers); 
查看更多
登录 后发表回答