发送带附件的 PHP HTML 邮件 [英] Send PHP HTML mail with attachments
问题描述
我遇到了一个问题:直到今天,我使用包含
I got a problem: Until today, I sent HTML mails with PHP using a header which contains
Content-type: text/html;
现在,我添加了添加附件的功能.为此,我不得不将此行更改为
Now, I added functionality to add attachments. For this, I had to change this line to
Content-Type: multipart/mixed;
现在,使用multipart/mixed
,邮件的其余部分,即普通文本,显示为文本/纯文本.我如何才能意识到附件有效而邮件文本仍然是 HTML?
Now, with multipart/mixed
, the rest of the mail, so the normal text, gets shown just as text/plain. How can I realize that attachments work and the mailtext is still HTML?
推荐答案
要发送带有附件的电子邮件,我们需要使用 multipart/mixed MIME 类型,该类型指定将混合类型包含在电子邮件中.此外,我们想使用 multipart/alternative MIME 类型来发送电子邮件的纯文本和 HTML 版本.请看示例:
To send an email with attachment we need to use the multipart/mixed MIME type that specifies that mixed types will be included in the email. Moreover, we want to use multipart/alternative MIME type to send both plain-text and HTML version of the email.Have a look at the example:
<?php
//define the receiver of the email
$to = 'youraddress@example.com';
//define the subject of the email
$subject = 'Test email with attachment';
//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
$headers = "From: webmaster@example.com
Reply-To: webmaster@example.com";
//add boundary string and mime type specification
$headers .= "
Content-Type: multipart/mixed; boundary="PHP-mixed-".$random_hash.""";
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents('attachment.zip')));
//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
Hello World!!!
This is simple text email message.
--PHP-alt-<?php echo $random_hash; ?>
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; ?>--
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: application/zip; name="attachment.zip"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
<?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";
?>
如您所见,发送带附件的电子邮件很容易完成.在前面的示例中,我们有 multipart/mixed MIME 类型,在其中我们有 multipart/alternative MIME 类型,它指定了电子邮件的两个版本.为了在我们的消息中包含附件,我们将指定文件中的数据读取为一个字符串,使用 base64 对其进行编码,将其拆分为较小的块以确保它符合 MIME 规范,然后将其作为附件包含在内.
As you can see, sending an email with attachment is easy to accomplish. In the preceding example we have multipart/mixed MIME type, and inside it we have multipart/alternative MIME type that specifies two versions of the email. To include an attachment to our message, we read the data from the specified file into a string, encode it with base64, split it in smaller chunks to make sure that it matches the MIME specifications and then include it as an attachment.
这篇关于发送带附件的 PHP HTML 邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!