PHP Email Header多个附件

时间:2017-05-15 10:05:42

标签: php email email-attachments email-headers

我试图用PHP的mail()函数发送电子邮件,但我没知道如何在这封邮件中发送两个附件。我已经可以在我的邮件中发送纯文本,Html文本和一个附件,但它很难发送给另一个。

我确定我的邮件头中的任何内容都是错误的,但我不知道是什么。 这是我的PHP代码,它生成电子邮件:

$attachment = chunk_split(base64_encode(file_get_contents('/var/www/html/style/img/smalllogo.png')));
$attachment2 = chunk_split(base64_encode(file_get_contents('/var/www/html/style/img/header.png')));
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"

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

<?php echo $txtMailPlain;?>

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

<?php echo $txtMailHtml;?>

--PHP-alt-<?php echo $random_hash; ?>--

--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: image/png; name="smalllogo.png" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 
X-Attackment-Id: 1

<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--

--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: image/png; name="smalsllogo.png" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 
X-Attackment-Id: 2

<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--


<?php
//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"; 

我得到了第一个附件,名为&#34; smalllogo.png&#34;但我没有得到另一个。而且我不确定我是否收到正确的明文消息。

提前致谢, J. Doe;)

1 个答案:

答案 0 :(得分:0)

你已经写过两个地方

<?php echo $attachment; ?>

你必须写一个地方是<?php echo $attachment2; ?>