【问题标题】:PHPMailer and specific characters in subject (UTF-8?)PHPMailer 和主题中的特定字符(UTF-8?)
【发布时间】:2020-07-08 00:58:53
【问题描述】:

我正在尝试使用 PHPMailer 发送电子邮件。除了主题中的 UTF-8 编码外,一切都有效。 我仍然在我的邮件客户端中获得 html 代码(commancé 并且必须是“commancé”)。

$mail = new PHPMailer(true);
$mail->CharSet = 'UTF-8';
$mail->Encoding = 'base64';

try {
    //Server settings

    //Recipients
    $mail->setFrom('kevin@expl.com', 'Kevin de Exple');
    $mail->addAddress($email, $name);     // Add a recipient
    
    // Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $subject = 'RE: La plantation de votre arbre a commancé';
    $sub = '=?UTF-8?B?'.base64_encode($subject).'?=';
    $mail->Subject = $sub;

...

你能帮帮我吗?我在网上尝试了所有内容:)

祝你有美好的一天!

【问题讨论】:

    标签: phpmailer


    【解决方案1】:

    我不知道你为什么要努力做事!第一步是将CharSet 属性设置为UTF-8,您已经完成了。对于这个主题,你有这个:

    $subject = 'RE: La plantation de votre arbre a commancé';
    $sub = '=?UTF-8?B?'.base64_encode($subject).'?=';
    $mail->Subject = $sub;
    

    里面有很多不必要的东西。您需要做的就是:

    $mail->Subject = 'RE: La plantation de votre arbre a commancé';
    

    PHPMailer 为您处理所有编码。这里唯一需要注意的是确保您实际上在编辑器中使用 UTF-8。如果您使用的是 ISO-8859-1 或类似标准,它将无法正常工作——尽管它在您的代码中看起来是一样的。

    至于拼写错误,我将不得不把它们留给你......

    【讨论】:

      【解决方案2】:

      这样做

          function correct_encoding($text) {
            $current_encoding = mb_detect_encoding($text, 'auto');
            $text = iconv($current_encoding, 'UTF-8', $text);
            return $text;
          }
          $mail = new PHPMailer(true);
          $mail->CharSet = 'utf-8';///<-- use lowercase
          //$mail->Encoding = 'base64';//usually it is not necessary
      
      try {
          //Server settings
      
          //Recipients
          $mail->setFrom('kevin@expl.com', 'Kevin de Exple');
          $mail->addAddress($email, $name);     // Add a recipient
          
          // Content
          $mail->isHTML(true);                                  // Set email format to HTML
          $subject = 'RE: La plantation de votre arbre a commanc&eacute;';
          //$sub = '=?UTF-8?B?'.base64_encode($subject).'?=';
          $mail->Subject = correct_encoding($subject);
      

      【讨论】:

      • 您好!谢谢,但它不起作用。我还有话要说:)
      猜你喜欢
      • 2012-12-25
      • 2011-10-27
      • 1970-01-01
      • 1970-01-01
      • 2012-10-05
      • 1970-01-01
      • 1970-01-01
      • 2011-12-21
      相关资源
      最近更新 更多