用于文本和HTML电子邮件的MIME

时间:2014-08-03 15:03:53

标签: php html email mime

如何修改此代码以添加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 个答案:

答案 0 :(得分:0)

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/plaintext/html的问题,但如果有疑问,请在chunk_split (base64_encode ())上使用body-part并为该部分添加Content-transfer-encoding: base64标头。< / p>

代码

以下代码非常明显:

$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);

参考

详情请见: