PHP:在电子邮件中“附加”附加内容“ [英] PHP: Add attachments to emails "on the fly"?
问题描述
我有一个PHP应用程序输出一些字符串。将这些字符串转换为电子邮件中的附件( *。TXT
-files)将是非常好的,而无需先将其存储在磁盘上,并且必须将其读回。 这是否可以在PHP中?
是的,这是可能的。您只需要使用以下语法将您的电子邮件发送到多部分消息:
MIME版本:1.0
内容类型:multipart / mixed; boundary = random-boundary
这是多部分/混合消息的可选前导码。
--random-boundary
内容类型:text / plain
这是主消息体。
--random-boundary
内容类型:text / plain
Content-Disposition:attachment; filename = file.txt
这是附件文件的内容。
--random-boundary--
这是多部分/混合消息的可选结尾。
然后可以像任何其他消息一样描述每个部分。但是你应该可以使用一个这样做的库。
现在,如果你使用 PHP的邮件
函数,前两行将是标题,其余的将是该邮件的内容。边界应该是一个随机边界,所以在它的前面有 -
的字符串的可能性是不可能的。
I just got PHP's mail function to work properly in my test environment.
I have a PHP app that outputs a number of strings. It would be really nice to convert these strings to attachments (*.TXT
-files) in an email, without first storing them on disk and having to read them back. Would this be possible in PHP?
Yes, this is possible. You just need to make your email message a multipart message with the following syntax:
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=random-boundary
This is the optional preamble of a multipart/mixed message.
--random-boundary
Content-Type: text/plain
This is the main message body.
--random-boundary
Content-Type: text/plain
Content-Disposition: attachment; filename=file.txt
This is the content of the attached file.
--random-boundary--
This is the optional epilogue of a multipart/mixed message.
Each part can then be described like any other message. But you should probably use a library that does this for you.
Now if you’re using PHP’s mail
function, the first two line would be the header and the rest would be the contents of that mail message. The boundary should be a random boundary so that the possibility of having that string with --
in front of it being in the contents of one part is very unlikely.
这篇关于PHP:在电子邮件中“附加”附加内容“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!