【问题标题】:PHP Send UTF-8 mail without PEAR::Mail PEAR::Mail_MimePHP 发送没有 PEAR::Mail PEAR::Mail_Mime 的 UTF-8 邮件
【发布时间】:2010-12-02 19:25:27
【问题描述】:

我希望能够使用包含 8 位字符 åäö 的 PHP mail() 发送电子邮件。它们将用于主题、消息和“发件人:”标题中。如何在不使用 PEAR 包的情况下做到这一点?

【问题讨论】:

    标签: php email pear


    【解决方案1】:

    如果您不介意对不需要的单词进行编码,最简单的解决方案是将所有内容放在 base64 RFC 2047 编码字中:

    $subject= "=?utf-8?b?".base64_encode($subject)."?=";
    $body= "blah blah $utf8text blah";
    $headers= "MIME-Version: 1.0\r\n";
    $headers.= "From: =?utf-8?b?".base64_encode($fromname)."?= <$fromaddress>\r\n";
    $headers.= "Content-Type: text/plain;charset=utf-8";
    
    mail($toaddress, $subject, $body, $headers);
    

    【讨论】:

    • 谢谢,但这个答案相当不完整。我需要在消息中使用 åäö、“发件人:”标题和主题。我怎样才能做到这一点?感谢您的宝贵时间。
    • 添加了更多上下文。 From 标头与 Subject 标头的编码方法相同。邮件正文编码由 Content-Type 控制。我认为您已经将 åäö 字符编码为 UTF-8 字节字符串 '\xc3\xa5\xc3\xa4\xc3\xb6'。
    【解决方案2】:

    使用 swiftmailer 库。 http://swiftmailer.org/

    【讨论】:

      【解决方案3】:
      $headers = array('From' => $from,
          'To' => "<$to>",
          'Subject' => $subject);
      
      if ($is_html) {
          $headers['Content-type'] = "text/html; charset=UTF-8";
      } else {
          $headers['Content-type'] = "text/plain; charset=UTF-8";
      }
      

      这对我有用

      【讨论】:

        猜你喜欢
        • 2014-09-04
        • 2011-06-24
        • 1970-01-01
        • 2010-12-05
        • 2018-06-10
        • 2013-01-23
        • 1970-01-01
        • 2015-07-08
        • 2017-10-26
        相关资源
        最近更新 更多