当前位置: 首页 > 面试题库 >

发送带有附件的PHP HTML邮件

景凌
2023-03-14
问题内容

我遇到了一个问题:直到今天,我使用包含以下内容的标头使用PHP发送HTML邮件

Content-type: text/html;

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

Content-Type: multipart/mixed;

现在,使用multipart/mixed,邮件的其余部分(即普通文本)将显示为文本/纯文本。我怎么能知道附件有效并且邮件文本仍然是HTML?


问题答案:

要发送带有附件的电子邮件,我们需要使用multipart / mixed
MIME类型,该类型指定混合类型将包含在电子邮件中。此外,我们想使用多部分/替代MIME类型来发送电子邮件的纯文本和HTML版本,请看示例:

<?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 \r\n 
$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com"; 
//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"; 
?>

如您所见,发送带有附件的电子邮件很容易完成。在前面的示例中,我们具有multipart / mixed MIME类型,在其中,我们具有multipart /
alternative
MIME类型,用于指定电子邮件的两个版本。为了包括消息的附件,我们将指定文件中的数据读取为字符串,使用base64对其进行编码,将其拆分成较小的块以确保其与MIME规范匹配,然后将其作为附件包含在内。



 类似资料:
  • 问题内容: 是否有用于NodeJS的库,用于发送带有附件的邮件? 问题答案: 是的,这非常简单,我使用nodemailer: 然后阅读文件并发送电子邮件:

  • 问题内容: 我似乎找不到我编写的应该发送带有附件的电子邮件的php函数的问题。我已经为此苦苦挣扎了一段时间了。 编辑 问题是邮件的消息与文件混合在一起并作为附件发送。 问题答案: Artefacto让我更加关注输出,并且找到了解决方法:

  • 问题内容: 我正在尝试在带有附件的codeigniter上发送电子邮件。 我总是成功收到电子邮件。但是,我从未收到带有附件的文件。以下是代码,非常感谢所有评论。 问题答案: $ this- >电子邮件-> attach() 使您可以发送附件。将文件路径/名称放在第一个参数中。注意:使用文件路径,而不是URL。对于多个附件,请多次使用该功能。例如:

  • 我正在尝试使用sendTransacEmail()方法将附件发送到sendinblue(版本3),下面是我发送事务性电子邮件的代码, 这行给我错误请求, 在给定的API中https://developers.sendinblue.com/reference#sendtransacemail附件是一个对象数组,其中URL、名称 如何用附件发送电子邮件,1.使用字节数组内容?2.使用网址?

  • 尝试发送带有pdf附件的电子邮件,尝试使用swickmailer,但没有成功,此代码使用zip但不使用PDF:( 邮件被发送罚款,我得到的邮件:但附件是不存在的,在meial有所有的bas64编码在电子邮件像: onatatent-Type: Application/octet-stream; name="media.pdf"Content-transver-Encode: base 64 Con

  • 本文向大家介绍Codeigniter实现发送带附件的邮件,包括了Codeigniter实现发送带附件的邮件的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了Codeigniter实现发送带附件的邮件的方法。分享给大家供大家参考。具体分析如下: attach() 方法允许你的发邮件时带上附件,下面是演示代码 希望本文所述对大家基于Codeigniter的php程序设计有所帮助。