2

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user93796
  • 18,749
  • 31
  • 94
  • 150

4 Answers4

6

I recommend using PHPMailer.

chaos
  • 122,029
  • 33
  • 303
  • 309
3

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@host", "myemail@host");
Zed
  • 57,028
  • 9
  • 76
  • 100
0

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.

VinkoCM
  • 337
  • 2
  • 6
  • 15
-1

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.

Pons
  • 1,747
  • 1
  • 13
  • 19