PHP:将附件添加到“即时”电子邮件?
问题描述:
我刚刚在我的测试环境中获得了PHP的邮件功能。PHP:将附件添加到“即时”电子邮件?
我有一个输出一些字符串的PHP应用程序。将这些字符串转换为电子邮件中的附件(*.TXT
-files)将非常好,而不必先将它们存储在磁盘上,并且必须将其读回。 这将成为可能的PHP?
答
是的,这是可能的。你只需要让你的电子邮件消息的multipart message语法如下:
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.
每个部分都那么像任何其他信息来描述。但是你应该使用一个为你做这个的库。
现在,如果您使用的是PHP’s mail
function,则前两行应为标题,其余为该邮件的内容。边界应该是一个随机边界,以便在该部分的内容中存在该字符串前面的字符串的可能性非常小。
答
是的,您可以使用例如PEAR's Mail_Mine它的类。
bool addAttachment (string $file , string $c_type = 'application/octet-stream' , string $name = '' , boolean $isfile = true , string $encoding = 'base64')
是您想要使用的方法,$file
包含您的字符串,而$isfile
是false
。
答
,你可以使用的Zend_Mail类的容易得多代码 的文件名会"smapleFilename"
及其createAttachment
功能 最后一个参数,但不foget设置你的transport
是 样品之前:
$mail = new Zend_Mail();
$mail->setBodyText("body")
->createAttachment("your wanted text " , Zend_Mime::TYPE_TEXT,
Zend_Mime::DISPOSITION_ATTACHMENT , Zend_Mime::ENCODING_BASE64, "smapleFilename.txt");
$mail->setFrom('[email protected]', 'Server');
$mail->addTo('[email protected]');
$mail->setSubject("subject");
$mail->send();
在Zend框架项目中你会这样做:
resources.mail.transport.type = smtp
resources.mail.transport.host = "mail.111111.com"
resources.mail.transport.auth = login
resources.mail.transport.username = [email protected]
resources.mail.transport.password = test
;resources.mail.transport.ssl = tls
resources.mail.transport.port = 2525
resources.mail.transport.register = true ; True by default
感谢您的回答。花了一些时间让它运行,但它的工作!谢谢 – Industrial 2010-11-25 19:38:20