【发布时间】:2017-10-14 01:45:01
【问题描述】:
我正在尝试使用 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";
我得到了第一个附件,名为“smalllogo.png”,但我没有得到另一个。而且我不确定我是否收到了正确的纯文本消息。
提前致谢, J. Doe ;)
【问题讨论】:
标签: php email email-attachments email-headers