PHP mail special characters in subject field

后端 未结 3 1679
粉色の甜心
粉色の甜心 2020-12-31 03:26

I would like to insert special characters in the subject of HTML e-mails sent with the PHP mail() function.

I want my subject to look like this:

★ Yo

相关标签:
3条回答
  • 2020-12-31 04:19

    Try for subject:

    $sub = '=?UTF-8?B?'.base64_encode($subject).'?=';
    

    And then:

    mail($to, $sub, $message, $headers);
    
    0 讨论(0)
  • 2020-12-31 04:22

    While the accepted answer works fine, it makes it impossible to read the subject when looking at the raw headers. Here's an alternative that keeps the line readable and also shorter if it consists of mostly ascii characters.

    $subject = '=?UTF-8?q?' . quoted_printable_encode($subject) . '?=';
    

    Here's the encoded subject line of the accepted answer:

    =?UTF-8?B?4piFIFlvdXIgbmV3IGFjY291bnQ=?=
    

    Here's the encoded subject line of my answer:

    =?UTF-8?q?=E2=98=85 Your new account?=
    

    Edit:

    It turns out quoted_printable_encode() splits long strings into multiple lines of max 75 characters, as required per RFC 2045. This resulted is a string that cannot be used with mail()'s $subject parameter. Here's an updated version to fix this. It will also avoid encoding pure-ascii subjects.

    /**
     * Make sure the subject is ASCII-clean
     *
     * @param string $subject Subject to encode
     *
     * @return string Encoded subject
     */
    function getEncodedSubject(string $subject): string {
        if (!preg_match('/[^\x20-\x7e]/', $subject)) {
            // ascii-only subject, return as-is
            return $subject;
        }
        // Subject is non-ascii, needs encoding
        $encoded = quoted_printable_encode($subject);
        $prefix = '=?UTF-8?q?';
        $suffix = '?=';
        return $prefix . str_replace("=\r\n", $suffix . "\r\n  " . $prefix, $encoded) . $suffix;
    }
    

    Explanation:

    $subj = "Lorem ipsuöm dolor sit amet, consectetur adipiscing elit. Praesent mattis molestie purus, non semper lectus dictum eget.";
    

    After quoted_printable_encode

    Lorem ipsu=C3=B6m dolor sit amet, consectetur adipiscing elit. Praesent mat=
    tis molestie purus, non semper lectus dictum eget.
    

    After str_replace

    =?UTF-8?q?Lorem ipsu=C3=B6m dolor sit amet, consectetur adipiscing elit. Praesent mat?=
      =?UTF-8?q?tis molestie purus, non semper lectus dictum eget.?=
    
    0 讨论(0)
  • 2020-12-31 04:25
     $headers="";    
     $message = utf8_encode($message);
     $message = wordwrap($message, 70, "\r\n");
     $subject = '=?UTF-8?B?'.base64_encode(utf8_encode($subject)).'?=';
     $to_name = '=?UTF-8?B?'.base64_encode(utf8_encode($to_name)).'?=';
     $from_name = '=?UTF-8?B?'.base64_encode(utf8_encode($from_name)).'?=';
    
     $headers .= "MIME-Version: 1.0" . "\r\n"; 
     $headers .= "Content-type: text/plain; charset=utf-8" . "\r\n"; 
     //$headers .= "Content-Transfer-Encoding: quoted-printable" . "\r\n"; 
     $headers .= "From: $from_name <$from_email>" . "\r\n"; 
     $headers .= "To: $to_name <$to_email>" . "\r\n";
     $headers .= "Subject: $subject" . "\r\n";
     $headers .= "X-Mailer: PHP/" . phpversion();  
    
     mail("", $subject, $message, $headers); 
    
    0 讨论(0)
提交回复
热议问题