PHP:“动态”添加电子邮件附件?

时间:2010-11-25 17:28:52

标签: php email file attachment

我只是让PHP的邮件功能在我的测试环境中正常工作。

我有一个PHP应用程序,输出许多字符串。将这些字符串转换为电子邮件中的附件(*.TXT -files)会非常好,而无需先将它们存储在磁盘上并且必须将其读回。 这可以用PHP吗?

3 个答案:

答案 0 :(得分:2)

是的,这是可能的。您只需使用以下语法将电子邮件设为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,前两行将是标题,其余行将是该邮件的内容。边界应该是一个随机边界,因此在其前面有--的字符串在一个部分的内容中的可能性是不太可能的。

答案 1 :(得分:1)

是的,您可以使用,例如PEAR's Mail_Mine课程。

bool addAttachment ( string $file , string $c_type = 'application/octet-stream' , string $name = '' , boolean $isfile = true , string $encoding = 'base64' )是您要使用的方法,其中$file包含您的字符串,$isfilefalse

答案 2 :(得分:1)

您可以使用Zend_Mail类来实现更轻松的代码 文件名为"smapleFilename",它是createAttachment函数中的最后一个参数 但不要忘记在此之前设置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('test@222222.com', 'Server');
        $mail->addTo('test@hotmail.com');
        $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 = test@111111.com
resources.mail.transport.password = test
;resources.mail.transport.ssl = tls
resources.mail.transport.port = 2525
resources.mail.transport.register = true ; True by default