Php HTML电子邮件混合(HTML和附件)以纯文本形式出现

Php HTML电子邮件混合(HTML和附件)以纯文本形式出现,php,html,email,base64,Php,Html,Email,Base64,我收到了一封由PHP发送的HTML电子邮件,附带了一个通过base64编码的pdf。这一切都可以工作,除了电子邮件是纯文本,所以你可以看到所有的HTML标记和base64输出-显然不是理想的。这里一定有我遗漏的东西,以确保它被读取为HTML和附件 如果有人能帮忙,那就太好了 我的PHP: <?php $to = $email; $message = 'testing...'; $subject = 'Health Insurance Quote Request'; $headers

我收到了一封由PHP发送的HTML电子邮件,附带了一个通过base64编码的pdf。这一切都可以工作,除了电子邮件是纯文本,所以你可以看到所有的HTML标记和base64输出-显然不是理想的。这里一定有我遗漏的东西,以确保它被读取为HTML和附件

如果有人能帮忙,那就太好了

我的PHP:

<?php $to = $email;

$message = 'testing...';

$subject = 'Health Insurance Quote Request';

$headers = "From: Andy - CEO Health.com.au <" . strip_tags('info@health.com.au') . ">\r\n";
$headers .= "Reply-To: Andy - CEO Health.com.au <". strip_tags('info@health.com.au') . ">\r\n";

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

// headers for attachment 
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 

// multipart boundary 
$email_message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 
$email_message .= "--{$mime_boundary}\n";

//get PDF URL
$data = chunk_split(base64_encode(file_get_contents('http://example.com/doge.pdf')));

$email_message .= "Content-Type: {\"application/pdf\"};\n" . " name=\"$product\"\n" . 
"Content-Disposition: attachment;\n" . " filename=\"$product\"\n" . 
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$email_message .= "--{$mime_boundary}\n";

mail($email, $subject, $email_message, $headers);
在最后一个边界标记的末尾可能没有--

ie$email\u message.=“{$mime\u boundary}\n”

至$email_message.=“{$mime_boundary}-\n”

我不确定multipart/mixed和multipart/alternative之间是否有区别,但这对我来说是可行的

# Setup mime boundary
$mime_boundary = 'Multipart_Boundary_x'.md5(time()).'x';

$headers  = "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/alternative; boundary=\"$mime_boundary\"\n";
$headers .= "Content-Transfer-Encoding: 7bit\n";

$body    = "This is a multi-part message in mime format.\n\n";

# Add in plain text version
$body   .= "--$mime_boundary\n";
$body   .= "Content-Type: text/plain; charset=\"charset=us-ascii\"\n";
$body   .= "Content-Transfer-Encoding: 7bit\n\n";
$body   .= $text_content;
$body   .= "\n\n";

# Add in HTML version
$body   .= "--$mime_boundary\n";
$body   .= "Content-Type: text/html; charset=\"UTF-8\"\n";
$body   .= "Content-Transfer-Encoding: 7bit\n\n";
$body   .= $html_content;
$body   .= "\n\n";

#Attachments
   if ($path!='' && $filename!=''){
    $file_size = filesize($path.$filename);
    $handle = fopen($path.$filename, "r");
    $filecontent = fread($handle, $file_size);
    fclose($handle);
    $filecontent = chunk_split(base64_encode($filecontent));

    $body   .= "--$mime_boundary\n";
    $body .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
    $body .= "Content-Transfer-Encoding: base64\r\n";
    $body .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
    $body .= $filecontent."\r\n\r\n";
    }
# End email
$body   .= "--$mime_boundary--\n"; 

尝试使用SwiftMailer来实现这一点——它可以更优雅地处理附件。
# Setup mime boundary
$mime_boundary = 'Multipart_Boundary_x'.md5(time()).'x';

$headers  = "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/alternative; boundary=\"$mime_boundary\"\n";
$headers .= "Content-Transfer-Encoding: 7bit\n";

$body    = "This is a multi-part message in mime format.\n\n";

# Add in plain text version
$body   .= "--$mime_boundary\n";
$body   .= "Content-Type: text/plain; charset=\"charset=us-ascii\"\n";
$body   .= "Content-Transfer-Encoding: 7bit\n\n";
$body   .= $text_content;
$body   .= "\n\n";

# Add in HTML version
$body   .= "--$mime_boundary\n";
$body   .= "Content-Type: text/html; charset=\"UTF-8\"\n";
$body   .= "Content-Transfer-Encoding: 7bit\n\n";
$body   .= $html_content;
$body   .= "\n\n";

#Attachments
   if ($path!='' && $filename!=''){
    $file_size = filesize($path.$filename);
    $handle = fopen($path.$filename, "r");
    $filecontent = fread($handle, $file_size);
    fclose($handle);
    $filecontent = chunk_split(base64_encode($filecontent));

    $body   .= "--$mime_boundary\n";
    $body .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
    $body .= "Content-Transfer-Encoding: base64\r\n";
    $body .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
    $body .= $filecontent."\r\n\r\n";
    }
# End email
$body   .= "--$mime_boundary--\n";