Pear Mail, how to send plain/text + text/html in UTF-8

前端 未结 1 1700
一整个雨季
一整个雨季 2021-02-05 15:29

I\'m trying to send an email in both text and html, but I can\'t correctly send the right headers. In particular, I\'d like to set the Content-Type header, but I can\'t find how

相关标签:
1条回答
  • 2021-02-05 16:06

    I discovered that the headers are supposed to be written differently. In particular, some of them are parameters for the mime object, and not email headers. Then the mime_params array should be passed to the get() function.

    This is the correct way to set the headers:

    $headers = array(
      'From'          => 'info@mydomain.com',
      'Return-Path'   => 'info@mydomain.com',
      'Subject'       => 'mysubject',
      'Content-Type'  => 'text/html; charset=UTF-8'
    );
    
    $mime_params = array(
      'text_encoding' => '7bit',
      'text_charset'  => 'UTF-8',
      'html_charset'  => 'UTF-8',
      'head_charset'  => 'UTF-8'
    );
    
    $mime = new Mail_mime();
    
    $html = '<html><body><b>my body</b></body></html>';
    $text = 'my body';
    
    $mime->setTXTBody($text);
    $mime->setHTMLBody($html);
    
    $body = $mime->get($mime_params);
    $headers = $mime->headers($headers);
    $mail_object =& Mail::factory('smtp', $GLOBALS['pear_mail_config']);
    $mail_object->send('test@mydomain.com', $headers, $body);
    
    0 讨论(0)
提交回复
热议问题