本文介绍了PHP MIME多部分邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一个问题。我正在尝试发送包含html和纯文本的多部分邮件。这也是通过垃圾邮件过滤器并在不支持HTML的情况下允许更多人阅读邮件的途径之一。花了很长时间Google搜索后,我找到了一些例子。我做了我的代码,它发送邮件,但它显示的文本与html标签,代码,字符串等。

 < ?php 
$ boundary = md5(uniqid(rand()));
$ header。=From:My Name< [email protected]> \\\
;
$ header。=回复:[email protected] \\\
;
$ header。='MIME-Version:1.0'。\r\\\
;
$ header。='Content-type:multipart / alternative; boundary = $ boundary'。\\\
;

$ adres [email protected];

$ subject =subject;

$ message =这是使用MIME \\\
的多部分信息;
$ message。= - 。 $边界。 \\\
;
$ message。=Content-type:text / plain; charset = iso-8859-1\\\
;
$ message。=Content-Transfer-Encoding:7bit。 \\\
\\\
;
$ message。=纯文本版本\\\
\\\
;
$ message。= - 。 $边界。 \\\
;
$ message。=Content-type:text / html; charset = iso-8859-1\\\
;
$ message。=Content-Transfer-Encoding:7bit。 \\\
\\\
;
$ message。=< html>
< body>
< center>
< b> HTML文本版本< / b>
< / center>
< / body>
< / html> \\\
\\\
;
$ message。= - 。 $边界。 - ;

if(mail($ adres,$ subject,$ message,$ header))
{
print'message sent';
}
else
{
print'message未发送';
}
?>

这是结果:

 这是使用MIME 
的多部分消息--c071adfa945491cac7759a760ff8baeb
Content-type:text / plain; charset = iso-8859-1
Content-Transfer-Encoding: 7bit

纯文本版本

--c071adfa945491cac7759a760ff8baeb
内容类型:text / html; charset = iso-8859-1
Content-Transfer-编码:7bit

< html>
< body>
< center>
< b> HTML文字版< / b>
< / center>
< / body>
< / html>

--c071adfa945491cac7759a760ff8baeb--

正如您所看到的,它显示了编码而不是单独的信息。我已经尝试了许多解决方案,如:


  • 添加/删除\ r \ n;

  • 将\r\\\
    更改为\ n;

  • 将内容类型从替代方式更改为混合;


  • 我正在学习PHP,而我所知道的全部是迄今为止我所阅读和完成的。如果你能告诉我问题在哪里,我还有很多要学习。

    解决方案

    行:

      $ header。='Content-type:multipart / alternative; boundary = $ boundary'。\\\
    ;

    有错误的引号,所以 $ boundary 将不会扩大。更改为:

      $ header。=内容类型:multipart / alternative; boundary = $ boundary \\\
    ;

    就像我在评论中所说的,在消息标题和内容部分头文件中,您应该使用 \r\\\
    作为换行符,因为这是RFC中定义的内容。大多数MTA只允许 \ n ,但有些会扼杀邮件,一些垃圾邮件过滤器会将每次RFC违规记录为垃圾邮件分数的一个点。



    使用诸如之类的东西是一个更好的选择,因为它格式化所有内容默认情况下,并遵守几乎每一个模糊,乏味的RFC。


    This will be my first question here. I am trying to send a multipart mail that contains both html and plain text. This is also one of the ways to get through spam filters and to allow more people to read the mail in case of not supporting HTML. After spending long hours googling, I have found some examples. I made my code, which sends the mail but it displays the text with the html tags, code, string etc.

    <?php
    $boundary=md5(uniqid(rand()));
    $header .= "From:My Name<[email protected]>\n";
    $header .= "Reply-To: [email protected] \n";
    $header .= 'MIME-Version: 1.0'."\r\n";
    $header .= 'Content-type: multipart/alternative;boundary=$boundary '."\n";
    
    $adres = "[email protected]";
    
    $subject = "subject";
    
    $message = "This is multipart message using MIME\n";
    $message .= "--" . $boundary . "\n";
    $message .= "Content-type: text/plain;charset=iso-8859-1\n";
    $message .= "Content-Transfer-Encoding: 7bit". "\n\n";
    $message .= "Plain text version\n\n";
    $message .="--" . $boundary . "\n";
    $message .="Content-type: text/html;charset=iso-8859-1\n";
    $message .= "Content-Transfer-Encoding: 7bit". "\n\n";
    $message .="<html>
    <body>
    <center>
    <b>HTML text version</b>
    </center>
    </body>
    </html>\n\n";
    $message .= "--" . $boundary . "--";
    
    if(mail($adres, $subject, $message, $header))
    {
    print'message sent';
    }
    else
    {
    print'message was not sent';
    }
    ?>
    

    This is the result:

        This is multipart message using MIME
        --c071adfa945491cac7759a760ff8baeb
        Content-type: text/plain;charset=iso-8859-1
        Content-Transfer-Encoding: 7bit
    
        Plain text version
    
        --c071adfa945491cac7759a760ff8baeb
        Content-type: text/html;charset=iso-8859-1
        Content-Transfer-Encoding: 7bit
    
        <html>
        <body>
        <center>
        <b>HTML text version</b>
        </center>
        </body>
        </html>
    
        --c071adfa945491cac7759a760ff8baeb--
    

    As you can see it displays the coding instead of the message alone. I have tried many solutions posted like:

    • adding/removing \r\n;
    • changing \r\n to \n;
    • changing content type from alternative to mixed;

    I am learning PHP and all I know is all I have read and done so far. I have still much to learn so please if you could tell me where is the problem. I would be very thankful.Best regards.

    解决方案

    The line:

    $header .= 'Content-type: multipart/alternative;boundary=$boundary '."\n";
    

    Has the wrong quotes, so $boundary won't be expanded. Change to:

    $header .= "Content-type: multipart/alternative;boundary=$boundary\n";
    

    And like I said in the comments, in the message headers and the content section headers you should be using \r\n as the line break since that's what is defined in the RFC. Most MTAs will allow simply \n, but some will choke on the message, and some spam filters will count every RFC violation as a point towards your spam score.

    Using something like PHPMailer is a much better option because it formats everything perfectly by default, and abides by just about every single obscure, boring RFC.

    这篇关于PHP MIME多部分邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 00:28