代码之家  ›  专栏  ›  技术社区  ›  user3865463

文本和HTML电子邮件的MIME

  •  1
  • user3865463  · 技术社区  · 10 年前

    如何修改此代码以添加MIME并包含电子邮件的文本版本?代码正在成功发送HTML格式的电子邮件。我使用的是文本编辑器、PHP、HTML和mySQL数据库。(此代码的目的是向新注册的用户发送一个链接,用于验证注册过程中提供的电子邮件地址。)

    $first_name =$register_data['first_name'];
    $to         =$register_data['email'];
    $from       ='support@mysite.com';
    $subject    = 'Verify email address to activate account';
    $email_code =$register_data['email_code'];
    
    $headers    = "From: $from\r\n";
    $headers    .= "Content-type: text/html\r\n";
    
    $path       ="activate.php?email=" . $to . "&email_code=" . $email_code . "";
    
    $message    = <<<EOF
        <html>
            <head>
                <base href="https://mysite.com">
            </head>
            <header>
                <img src="images/logo.png"><br>
                <img src="images/header.png">
            </header>
            <body>
                <br>
                <table style="width:600px">
                    <tr>
                        <td style="width:10px"></td>
                        <td>Dear $first_name,</td> 
                        <td style="width:10px"></td>
                    </tr>
                    <tr></tr>
                    <tr>
                        <td></td>
                        <td>Thank you for registering!  To activate your account, please verify your email address address by clicking on the verify button below.</td>
                        <td></td>
                    </tr>
                </table>
                <p>
                <table style="width:600px">
                    <tr>
                        <td style="width:10px"></td>
                        <td><center><a href =$path><img src="images/verify_button.png"></a></center></td>
                        <td style="width:10px"></td>
                    </tr>
                </table>
                </p>
                <p>
                <table style="width:600px">
                    <tr>
                        <td style="width:10px"></td>
                        <td>Thanks for registering!</td> 
                        <td style="width:10px"></td>
                    </tr>
                    <tr>
                        <td style="width:10px"></td>
                        <td>-- mysite</td> 
                        <td style="width:10px"></td>
                    </tr>
                </table>
                </p>
            </body>
        </html>
    EOF;
    
    mail ($to, $subject, $message, $headers);
    
    1 回复  |  直到 10 年前
        1
  •  0
  •   Community CDub    3 年前

    MIME语法

    根据RFC 2046(BNF语法,有些简化),这是多部分消息体的结构:

    multipart-body := [preamble CRLF]
                      dash-boundary CRLF
                      body-part *encapsulation
                      close-delimiter
                      [CRLF epilogue]
    
    dash-boundary := "--" boundary
    
    body-part := MIME-part-headers [CRLF *OCTET]
    
    encapsulation := delimiter
                     CRLF body-part
    
    delimiter := CRLF dash-boundary
    
    close-delimiter := delimiter "--"
    

    还要注意消息中的每一行 必须 CRLF -成对(例如。 "\r\n" )以及 不能 超过1000个字符(包括 CRLF公司 ). 这通常不是问题 text/plain text/html 但如果有疑问,请使用 chunk_split (base64_encode ()) body-part 并添加 Content-transfer-encoding: base64 该部分的标题。

    代码

    下面的代码不言自明:

    $first_name =$register_data['first_name'];
    $to         =$register_data['email'];
    $from       ='support@mysite.example';
    $subject    = 'Verify email address to activate account';
    $email_code =$register_data['email_code'];
    
    $boundary = md5(uniqid()); /* Unlikely to match any of body parts. */
    
    $headers = "From: $from\r\n"
             . "MIME-Version: 1.0\r\n"
             . "Content-Type: multipart/alternative; boundary=\"$boundary\"\r\n";
    
    $text_msg = <<<EOF
    Your text message.
    EOF;
    
    $html_msg = <<<EOF
    <html><title>Title</title><p>Your HTML message.
    EOF;
    
    $body = "--$boundary\r\n"         /* dash-boundary */
          . "Content-Type: text/plain\r\n"
          . "\r\n"
          . $text_msg
          . "\r\n--$boundary\r\n"     /* delimiter */
          . "Content-Type: text/html\r\n"
          . "\r\n"
          . $html_msg
          . "\r\n--$boundary--\r\n";  /* close-delimiter */
    
    mail ($to, $subject, $body, $headers);
    

    工具书类

    有关详细信息,请参阅: