Send email with attachments in PHP?

How can I send emails with attachments with a PHP script?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

I recommend using PHPMailer.

Solution 2

Use 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 protected]", "[email protected]");

Solution 3

I would recommend that you take a look at some of the PHP PEAR packages designed for sending emails. I know that some PHP programmers like reinventing the wheel, but take a look at the packages. 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

You may want to use the go-pear.php script to install the packages. It will make life a lot easier.

Solution 4

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/

To send files you have to base64 encode the file and use the “header” parameter of the php mail function.

Hope this will help.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply