如何使用 phpMailer class 发送 € 或 € 登录电子邮件主题

How to send € or € sign in email subject using phpMailer class

我正在使用 PHPMailer 发送电子邮件。

我想在主题中发送价格,例如 "Bid of Price 460 € has been placed"。

我在主题中使用了 € 和 €,但显示不正确。它显示 &euro 而不是 €

我应该用什么来解决这个问题?

这是我的代码:

$mail = new PHPMailer(true);
$mail->IsMail();
$body = $mailBody;
$mail->CharSet = "text/html; charset=UTF-8;"; 
$mail->IsHTML(true);
$mail -> AddAddress( $email, '' );  

$mail -> SetFrom( 'support@abc.com', 'Support Fretbay.com');
$mail -> Subject = 'Bid of Price 460 € has been placed';
$mail -> AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail -> MsgHTML($body);
$mail -> Send();

主题不能包含 html 个实体,因此 € 将不起作用。您必须使用包含欧元符号的字符集,并设置 $mail->CharSet 属性。当然,您的 字符也必须使用此字符集:

$mail->CharSet = "UTF-8";
$mail->Subject = 'Bid of Price 460 € has been placed';

重要的是欧元符号是 UTF-8 才能正确显示。

一个简单的解决方案是使用 mb_encode_mimeheader,100% 确定。

$subject = "Here is €500!";
$subject = str_replace('€', mb_encode_mimeheader('€', 'UTF-8'), $subject);

适用于 PHPMailer 和 PEAR::Mail