本文介绍了发送带有附件的PHP HTML邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题:直到今天,我使用包含

 内容类型的标题发送HTML邮件:为text / html; 

现在,我添加了添加附件的功能。为此,我不得不将此行更改为

  Content-Type:multipart / mixed; 

现在,使用 multipart / mixed 其余的邮件,所以正常的文字,显示为文本/平原。我如何才能意识到这个附件是有效的,而mailtext仍然是HTML?

解决方案

要发送带有附件的电子邮件,我们需要使用指定混合类型将包含在电子邮件中的multipart / mixed MIME类型。此外,我们想使用multipart / alternative MIME类型发送电子邮件的纯文本和HTML版本。看一下例子:

 <?php 
//定义电子邮件的接收者
$ to ='[email protected]';
//定义电子邮件的主题
$ subject ='使用附件测试电子邮件;
//创建边界字符串。它必须是唯一的
//所以我们使用MD5算法来生成随机哈希
$ random_hash = md5(date('r',time()));
//定义要传递的标题。请注意,它们与\\\

$ headers =From:[email protected]\r\\\
Reply-To:[email protected]分开;
//添加边界字符串和MIME类型规范
$ headers。=\r\\\
Content-Type:multipart / mixed; boundary = \PHP混合 - $ random_hash。 \ ;
//将文件内容读入一个字符串,
//用MIME base64,
//编码,并将其拆分成较小的块
$ attachment = chunk_split(base64_encode(的file_get_contents( 'attachment.zip')));
//定义消息的正文。
ob_start(); //打开输出缓冲
?>
--PHP-mixed-<?php echo $ random_hash; ?>
内容类型: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!
这是简单的文本电子邮件。

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

< h2> Hello World!< / h2>
< p>这是< b> HTML< / b> 。格式化及LT; / p为H.

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

--PHP-mixed-<?php echo $ random_hash; ?>
内容类型:application / zip; name =attachment.zip
内容传输编码:base64
内容处理:附件

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

<?php
//将当前缓冲区内容复制到$ message变量中,并删除当前输出缓冲区
$ message = ob_get_clean();
//发送电子邮件
$ mail_sent = @mail($ to,$ subject,$ message,$ headers);
//如果邮件发送成功打印邮件发送。否则打印邮件失败
echo $ mail_sent? 邮件发送:邮件失败;
?>

如您所见,发送带附件的电子邮件很容易完成。在前面的例子中,我们使用了multipart / mixed MIME类型,其中包含多个/替代的MIME类型,它指定了两个版本的电子邮件。要包括我们的消息的附件,我们从指定的文件中读取数据到一个字符串,用base64进行编码,将其拆分成更小的块,以确保它符合MIME规范,然后将其作为附件包含。 >

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;

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?

解决方案

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 = '[email protected]';
//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 \r\n
$headers = "From: [email protected]\r\nReply-To: [email protected]";
//add boundary string and mime type specification
$headers .= "\r\nContent-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";
?>

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邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 06:12