本文介绍了PHP库生成EML电子邮件文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从PHP生成EML文件。有什么图书馆能让我轻松创作吗?我可以在互联网上找到一些ActiveX组件,但宁可使用更便携的东西。

解决方案

我最终自己构建了MIME消息使用这种模板,每个字段被替换为 TEMPLATE_< name> 变量:

  From:TEMPLATE_FROM_ADDRESS 
MIME版本:1.0
至:TEMPLATE_TO_ADDRESS
主题:TEMPLATE_SUBJECT
Content-Type:multipart / mixed; boundary =080107000800000609090108

这是一个包含MIME格式多个部分的消息。
--080107000800000609090108
内容类型:text / plain

TEMPLATE_BODY
--080107000800000609090108
内容类型:application / octet-stream; name = TEMPLATE_ATTACH_FILENAME
Content-Transfer-Encoding:base64
Content-Disposition:attachment; filename =TEMPLATE_ATTACH_FILENAME

TEMPLATE_ATTACH_CONTENT
--080107000800000609090108

然后使用 str_replace 创建最终消息是非常简单的: p>

  $ content = file_get_contents(Template.eml); 
$ content = str_replace(TEMPLATE_FROM_ADDRESS,$ fromEmail,$ content);
$ content = str_replace(TEMPLATE_TO_ADDRESS,$ toEmail,$ content);
//等为每个模板参数
//也不要忘记base64_encode附件内容;
$ content = str_replace(TEMPLATE_ATTACH_CONTENT,base64_encode($ attachContent),$ content);

这篇文章中有关文件附件的附加信息:


I'm trying to generate EML files from PHP. Is there any library that will allow me to easily create them? I could find some ActiveX component on the internet but would rather use something more portable.

解决方案

I ended up building the MIME message myself using this kind of template, where each field is replaced by a TEMPLATE_<name> variable:

From: TEMPLATE_FROM_ADDRESS
MIME-Version: 1.0
To: TEMPLATE_TO_ADDRESS
Subject: TEMPLATE_SUBJECT
Content-Type: multipart/mixed; boundary="080107000800000609090108"

This is a message with multiple parts in MIME format.
--080107000800000609090108
Content-Type: text/plain

TEMPLATE_BODY
--080107000800000609090108
Content-Type: application/octet-stream;name="TEMPLATE_ATTACH_FILENAME"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;filename="TEMPLATE_ATTACH_FILENAME"

TEMPLATE_ATTACH_CONTENT
--080107000800000609090108

Then creating the final message is quite simple using str_replace:

$content = file_get_contents("Template.eml");
$content = str_replace("TEMPLATE_FROM_ADDRESS", $fromEmail, $content);
$content = str_replace("TEMPLATE_TO_ADDRESS", $toEmail, $content);
// etc. for each template parameter
// Also don't forget to base64_encode the attachment content;
$content = str_replace("TEMPLATE_ATTACH_CONTENT", base64_encode($attachContent), $content);

Additional info about file attachment in this post: Attachment name and file extension not working in email *.eml

这篇关于PHP库生成EML电子邮件文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 02:06