简体   繁体   English

用PHP发送带附件的电子邮件?

[英]Send email with attachments in PHP?

如何使用PHP脚本发送带附件的电子邮件?

我建议使用PHPMailer

Use SwiftMailer . 使用SwiftMailer

$message = new Swift_Message("My subject");

$message->attach(new Swift_Message_Part("Attached txt file"));
$message->attach(new Swift_Message_Attachment(new Swift_File("filename.txt"), "filename.txt", "text/txt"));

$swift->send($message, "email@host", "myemail@host");

I would recommend that you take a look at some of the PHP PEAR packages designed for sending emails. 我建议您查看一些用于发送电子邮件的PHP PEAR包。 I know that some PHP programmers like reinventing the wheel, but take a look at the packages. 我知道有些PHP程序员喜欢重新发明轮子,但要看一下这些包。 They are easy to use and implement and you should have no problem finding help. 它们易于使用和实施,您应该没有问题找到帮助。

Here is a link: http://pear.php.net/package/Mail 这是一个链接: http//pear.php.net/package/Mail

You may want to use the go-pear.php script to install the packages. 您可能希望使用go-pear.php脚本来安装软件包。 It will make life a lot easier. 它会让生活变得更轻松。

If you simply want a php function, you can use this mail_file function: http://www.barattalo.it/2010/01/10/sending-emails-with-attachment-and-html-with-php/ 如果你只是想要一个php函数,你可以使用这个mail_file函数: http//www.barattalo.it/2010/01/10/sending-emails-with-attachment-and-html-with-php/

To send files you have to base64 encode the file and use the "header" parameter of the php mail function. 要发送文件,您必须对base64进行编码,并使用php mail函数的“header”参数。

Hope this will help. 希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM