使用PHP发送HTML电子邮件

时间:2011-10-27 18:12:44

标签: php html-email

我有一些代码可以发送带附件的电子邮件。我收到附件的电子邮件,但它显示了html元素。我使用的是Content-Type:multipart / mixed; boundary = ...当我发送带附件的电子邮件时,我会看到电子邮件中的html元素,但是,如果我发送没有附件的电子邮件,则会正确显示电子邮件。 这是代码:

if(is_uploaded_file($_FILES['attachment']['tmp_name'])){
            //die("uploaded");
            copy($_FILES['attachment']['tmp_name'],"attachments/" . $_FILES['attachment']['name']);
            $file = $_FILES['attachment']['tmp_name'];
            $file_type = $_FILES['attachment']['type'];
            $file_name = $_FILES['attachment']['name'];
            $fopen = fopen($file, 'rb');
            $data = fread($fopen,filesize($file));
            fclose($fopen);

            $semi_rand = md5(time());
            $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

            $headers = "From: " .EMAIL_FROM_ADDR. "\r\n";
            $headers .= "Reply-To: ".EMAIL_FROM_ADDR."\r\n";
            $headers .= "CC: susan@example.com\r\n";    
            $headers .= "MIME-Version: 1.0\r\n";
            $headers .= "Content-Type: multipart/mixed; boundary=\"{$mime_boundary}\"";
            $message = "This is a multi-part message in MIME format.\n\n".
                       "--{$mime_boundary}\n".
                       "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . 
                       "Content-Transfer-Encoding: 7bit\n\n".
                       "<table border='1' cellspacing='1' cellpadding='1'>".
                        "<tr><td>Vendor Name:</td><td>".$_POST['vendor_name']."</td></tr>".
                        "<tr><td>Internet Agreement No:</td><td>".$_POST['internet_agreement_no']."</td></tr>".
                        "<tr><td>Vendor Duns No:</td><td>".$_POST['vendor_duns_no']."</td></tr>".
                        "<tr><td>Fee to NEXCOM:</td><td>".$_POST['fee_to_nexcom']."%</td></tr>".
                        "<tr><td>Settlement Worksheet For:</td><td>".$_POST['settlement_worksheet_date']."</td></tr>".
                        "<tr><td colspan='2'>&nbsp;</td></tr>".
                        "<tr><td>Total Gross Sales:</td><td>$".$_POST['total_gross_sales_1']."</td></tr>".
                        "<tr><td>Deductions:</td><td>$".$_POST['deductions']."</td></tr>".
                        "<tr><td>Refunds/Credits(Attach Backup):</td><td>$".$_POST['refunds_credits']."</td></tr>".
                        "<tr><td>Sales Tax:</td><td>$".$_POST['sales_tax']."</td></tr>".
                        "<tr><td>State Tax:</td><td>$".$_POST['state_tax']."</td></tr>".
                        "<tr><td>Other(Explain):".$_POST['explain_1']."</td><td>$".$_POST['other_1']."</td></tr>".
                        "<tr><td>Other(Explain):".$_POST['explain_2']."</td><td>$".$_POST['other_2']."</td></tr>".
                        "<tr><td>Total Deductions:</td><td>$".$_POST['total_deductions']."</td></tr>".
                        "<tr><td>Total Gross Sales:</td><td>$".$_POST['total_gross_sales_2']."</td></tr>".
                        "<tr><td>Less Deductions:</td><td>$".$_POST['less_deductions']."</td></tr>".
                        "<tr><td>Net Sales Subject to Commission:</td><td>$".$_POST['net_sales_subject']."</td></tr>".
                        "<tr><td>Net Commission due to NEXCOM:</td><td>$".$_POST['net_commission_due']."</td></tr>".
                        "<tr><td colspan='2'>Comments:</td></tr>".
                        "<tr><td colspan='2'>".$_POST['comments']."</td></tr>".
                       "</table>\n\n";

            $data = chunk_split(base64_encode($data));
            $message .= "--{$mime_boundary}\n".
                        "Content-Type: {$file_type}; name=\"{$file_name}\"\n".
                        "Content-Disposition: attachment; filename=\"{$file_name}\"\n".
                        "Content-Transfer-Encoding: base64\n\n".
                        $data."\n\n".
                        "--{$mime_boundary}--\n";       
        }
        else{
            //die("not uploaded");
            $headers = "From: " .EMAIL_FROM_ADDR. "\r\n";
            $headers .= "Reply-To: ".EMAIL_FROM_ADDR."\r\n";
            $headers .= "CC: susan@example.com\r\n";
            $headers .= "MIME-Version: 1.0\r\n";
            $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
            $message = "<table border='1' cellspacing='1' cellpadding='1'>".
                        "<tr><td>Vendor Name:</td><td>".$_POST['vendor_name']."</td></tr>".
                        "<tr><td>Internet Agreement No:</td><td>".$_POST['internet_agreement_no']."</td></tr>".
                        "<tr><td>Vendor Duns No:</td><td>".$_POST['vendor_duns_no']."</td></tr>".
                        "<tr><td>Fee to NEXCOM:</td><td>".$_POST['fee_to_nexcom']."%</td></tr>".
                        "<tr><td>Settlement Worksheet For:</td><td>".$_POST['settlement_worksheet_date']."</td></tr>".
                        "<tr><td colspan='2'>&nbsp;</td></tr>".
                        "<tr><td>Total Gross Sales:</td><td>$".$_POST['total_gross_sales_1']."</td></tr>".
                        "<tr><td>Deductions:</td><td>$".$_POST['deductions']."</td></tr>".
                        "<tr><td>Refunds/Credits(Attach Backup):</td><td>$".$_POST['refunds_credits']."</td></tr>".
                        "<tr><td>Sales Tax:</td><td>$".$_POST['sales_tax']."</td></tr>".
                        "<tr><td>State Tax:</td><td>$".$_POST['state_tax']."</td></tr>".
                        "<tr><td>Other(Explain):".$_POST['explain_1']."</td><td>$".$_POST['other_1']."</td></tr>".
                        "<tr><td>Other(Explain):".$_POST['explain_2']."</td><td>$".$_POST['other_2']."</td></tr>".
                        "<tr><td>Total Deductions:</td><td>$".$_POST['total_deductions']."</td></tr>".
                        "<tr><td>Total Gross Sales:</td><td>$".$_POST['total_gross_sales_2']."</td></tr>".
                        "<tr><td>Less Deductions:</td><td>$".$_POST['less_deductions']."</td></tr>".
                        "<tr><td>Net Sales Subject to Commission:</td><td>$".$_POST['net_sales_subject']."</td></tr>".
                        "<tr><td>Net Commission due to NEXCOM:</td><td>$".$_POST['net_commission_due']."</td></tr>".
                        "<tr><td colspan='2'>Comments:</td></tr>".
                        "<tr><td colspan='2'>".$_POST['comments']."</td></tr>".
                       "</table>";
        }           

2 个答案:

答案 0 :(得分:1)

您正在邮件正文中写Content-Type: text/plain;。将此设置为text/html应该可以解决问题。

答案 1 :(得分:0)

您可以使用库为您执行此操作。它们使用得很好,并且有很多错误,它们是免费的。

我看到提到的最多的是

http://swiftmailer.org/

http://phpmailer.worxware.com/

使用两者后我的个人偏好是swiftmailer