PHP E-Mail Encoding?

后端 未结 7 1523
甜味超标
甜味超标 2020-11-29 08:26

I am having some trouble with foreign characters when sending an e-mail. Could someone advise me on what to do? I suspect the problem could be one of three things.

相关标签:
7条回答
  • 2020-11-29 08:29

    You didn’t specify the type and encoding of your content. Try this:

    $headerFields = array(
        "From: {$visitor_email}",
        "MIME-Version: 1.0",
        "Content-Type: text/html;charset=utf-8"
    );
    $mail_sent = mail($client_email, $title, $message, implode("\r\n", $headerFields));
    
    0 讨论(0)
  • 2020-11-29 08:30

    Use a PHP mail wrapper, it will save your sanity (speaking from experience here). For example, PHPMailer allows you to set outgoing e-mail encoding and builds the message for you.

    Also, use UTF-8; it's almost universally supported nowadays and covers all the characters you would ever need.

    0 讨论(0)
  • 2020-11-29 08:31

    As far as I know PHP does not support UTF-8 as default encoding for its strings. You need to use the relevant encoding/handling functions for the encoding you would prefer.

    Also add a Content-Type:text/html;charset=utf-8 to your email headers so the email clients will display the characters correctly (or replace with your encoding of choice).

    0 讨论(0)
  • 2020-11-29 08:32

    I use this code to solve the problem with greek encode in old Horde mail.

    $headers = 'From: ' . $youremail . "\r\n";
    $headers .= 'To: ' . $to . "\r\n";    
    $headers .= 'Return-Path: ' . $youremail . "\r\n";
    $headers .= 'MIME-Version: 1.0' ."\r\n";
    $headers .= 'Content-Type: text/HTML; charset=utf-8' . "\r\n";
    $headers .= 'Content-Transfer-Encoding: 8bit'. "\n\r\n";
    $headers .= $message . "\r\n";
    
    mail('', 'Request from Site yoursite', '', $headers);
    
    0 讨论(0)
  • 2020-11-29 08:35

    In addition to what was said earlier, it is not mandatory to send the mail in HTML to be able to use UTF-8, you may also format it as simple text in the headers:

    'Content-Type: text/plain;charset=utf-8'
    
    0 讨论(0)
  • 2020-11-29 08:40

    Use this code

    function mail_send($arr)
    {
        if (!isset($arr['to_email'], $arr['from_email'], $arr['subject'], $arr['message'])) {
            throw new HelperException('mail(); not all parameters provided.');
        }
    
        $to      = empty($arr['to_name']) ? $arr['to_email'] : '"' . mb_encode_mimeheader($arr['to_name']) . '" <' . $arr['to_email'] . '>';
        $from    = empty($arr['from_name']) ? $arr['from_email'] : '"' . mb_encode_mimeheader($arr['from_name']) . '" <' . $arr['from_email'] . '>';
    
        $headers = array
        (
            'MIME-Version: 1.0',
            'Content-Type: text/html; charset="UTF-8";',
            'Content-Transfer-Encoding: 7bit',
            'Date: ' . date('r', $_SERVER['REQUEST_TIME']),
            'Message-ID: <' . $_SERVER['REQUEST_TIME'] . md5($_SERVER['REQUEST_TIME']) . '@' . $_SERVER['SERVER_NAME'] . '>',
            'From: ' . $from,
            'Reply-To: ' . $from,
            'Return-Path: ' . $from,
            'X-Mailer: PHP v' . phpversion(),
            'X-Originating-IP: ' . $_SERVER['SERVER_ADDR'],
        );
    
        mail($to, '=?UTF-8?B?' . base64_encode($arr['subject']) . '?=', $arr['message'], implode("\n", $headers));
    }
    

    from:http://php.net/manual/en/function.mail.php

    0 讨论(0)
提交回复
热议问题