【问题标题】:Sending HTML email from PHP从 PHP 发送 HTML 电子邮件
【发布时间】:2011-03-04 18:53:03
【问题描述】:

我正在尝试从 PHP 发送一封简单的 HTML 电子邮件。下面的代码只会在 GMail 中生成一个空白电子邮件。它还有一个名为“noname”的空附件,这根本不是我想要的;虽然这可能只是它不起作用的症状。

我使用的代码是:

<?php
//define the receiver of the email
$to = 'morrillkevin@gmail.com';
//define the subject of the email
$subject = 'Test HTML email';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-alt-<?php echo $random_hash; ?> 
MIME-Version: 1.0
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Hello World!!! 
This is simple text email message. 

--PHP-alt-<?php echo $random_hash; ?> 
MIME-Version: 1.0
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<h2>Hello World!</h2>
<p>This is something with <b>HTML</b>formatting.</p>

--PHP-alt-<?php echo $random_hash; ?>--
<?
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed";

【问题讨论】:

    标签: php html email


    【解决方案1】:

    以下代码可以帮助我删除邮件中的 HTML 标签。

    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";      
    $headers .= "Content-Transfer-Encoding: base64\r\n\r\n"; 
    

    【讨论】:

      【解决方案2】:

      这里有一些关于字符集的重要信息:

      "Content-Type: text/plain; charset = \"UTF-8\";\n"

      是对的,不只是

      "Content-Type: text/plain; charset=UTF-8\n"

      希望我能帮助其他像我一样在寻找同样错误时感到沮丧的人。

      请记住,只为 windows 而不是为 Linux 服务器编写 \r。 并且在标题的末尾应该是一个额外的空行:

      $headers .= "Content-Type: text/plain; charset = \"UTF-8\";\n";
      $headers .= "Content-Transfer-Encoding: 8bit\n";
      $headers .= "\n";
      

      【讨论】:

        【解决方案3】:

        原来关键是编码类型。而不是:

        Content-Type: text/plain; charset="iso-8859-1"
        

        我需要使用:

        Content-Type: text/plain; charset=us-ascii
        

        这可能取决于您如何在自己的文本编辑器中保存 PHP 文件等详细信息。我还没有研究过,但是 PHP 中的 iconv 函数可能也给我带来了一些乐趣。所以我觉得这部分真的很敏感。

        这是一个更好的示例代码 sn-p,它显示了整个事情的端到端:

        $notice_text = "This is a multi-part message in MIME format.";
        $plain_text = "This is a plain text email.\r\nIt is very cool.";
        $html_text = "<html><body>This is an <b style='color:purple'>HTML</b> text email.\r\nIt is very cool.</body></html>";
        
        $semi_rand = md5(time());
        $mime_boundary = "==MULTIPART_BOUNDARY_$semi_rand";
        $mime_boundary_header = chr(34) . $mime_boundary . chr(34);
        
        $to = "Me <foo@gmail.com>";
        $from = "Me.com <me@me.com>";
        $subject = "My Email";
        
        $body = "$notice_text
        
        --$mime_boundary
        Content-Type: text/plain; charset=us-ascii
        Content-Transfer-Encoding: 7bit
        
        $plain_text
        
        --$mime_boundary
        Content-Type: text/html; charset=us-ascii
        Content-Transfer-Encoding: 7bit
        
        $html_text
        
        --$mime_boundary--";
        
        if (@mail($to, $subject, $body,
            "From: " . $from . "\n" .
            "MIME-Version: 1.0\n" .
            "Content-Type: multipart/alternative;\n" .
            "     boundary=" . $mime_boundary_header))
            echo "Email sent successfully.";
        else
            echo "Email NOT sent successfully!";
        
        exit;
        

        -凯文

        【讨论】:

          【解决方案4】:

          如果使用库不是问题,只需使用Swift Mailer

          【讨论】:

          • 我试过 Pear mail 无济于事。我一般会尽量减少依赖关系并真正了解它是如何工作的。
          • Swift Mailer 文档中有多个示例。不像 PEAR Mail 它没有依赖项
          【解决方案5】:

          如果可能,请使用 PHPMailer 类。它将大大简化您的工作。

          【讨论】:

            【解决方案6】:

            希望这对你的 php 有所帮助

            $fname=$_POST['FirstName'];
            $lname=$_POST['LastName'];
            $dob=$_POST['DOB'];
            $name=$fname.'  '.$lname;
            
            
            $HTML="
            <style>
            <!--
            .form_tbl{ border:1px solid #bbbbbb; background-color:#eeeeee;}
            .form_tbl td{
                font-size:12px;
                color:#555555;
                padding:5px 5px;
            }
            h3{ 
                font-family:Georgia, Times New Roman, Times, serif;
                font-size:12px;
                font-weight:bold;
                color:#0099CC;
                margin:0;
                padding:0;
            }
            .form_tbl td select{ margin:0; padding:0;}
            .tbl_brdr{ border-top:1px solid #bbbbbb; border-left:1px solid #bbbbbb;}
            .tbl_brdr td{ border-bottom:1px solid #bbbbbb;  border-right:1px solid #bbbbbb; padding:5px 5px 5px 10px; }
            -->
            </style>
            
            <table class='form_tbl' width='650' border='0' cellspacing='0' cellpadding='3'>
            
                <tr>
                  <td colspan='2'><strong>PERSONAL DETAILS:</strong></td>
                </tr>
                <tr>
                  <td width='250' > Name:</td>
                  <td >".$name."</td>
                </tr>
                <tr>
                  <td  valign='middle'>Date Of Birth:</td>
                  <td  valign='top'>".$dob."</td>
                </tr>
              </table>
            ";
            
            $from=$name;
            $subject='Student Registration Form';
            $to= 'yourmail@mail.com';
            //$to='development@ezone.com.np';
            function sendHTMLemail($HTML,$from,$to,$subject)
            {
            // First we have to build our email headers
            // Set out "from" address
            
                $headers = "From: $from\r\n"; 
            
            // Now we specify our MIME version
            
                $headers .= "MIME-Version: 1.0\r\n"; 
            
            // Now we attach the HTML version
            
                $headers .= //"--$boundary\r\n".
                            "Content-Type: text/html; charset=ISO-8859-1\r\n";
                            //"Content-Transfer-Encoding: base64\r\n\r\n"; 
            // And then send the email ....
            
                if (mail($to,$subject,$HTML,$headers)) 
            {
            
              $_SESSION['msg']="Details successfully sent!";
            
            } 
            else 
            {
              $_SESSION['msg']="Sending Failed! Please try again later.";
            
            }
            }
            sendHTMLemail($HTML,$from,$to,$subject);
            ?>
            

            【讨论】:

            • 这不包括电子邮件的文本部分,所以它不能满足我的需要。
            【解决方案7】:

            您必须在邮件函数的 headers-parameter 中指定 mime-type。添加这个:

            $header  .= 'MIME-Version: 1.0' . "\r\n";
            $header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
            

            它也显示在PHP-Documentation for the mail-function 中。见示例 4

            【讨论】:

            • 这行不通,因为我需要包含测试和 HTML 的多部分消息。我尝试将 MIME 添加到两者中,但这无济于事,尽管我认为您是对的,它是必需的。所以我必须有一个额外的问题。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2013-04-21
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多