PHP: How to Send a PDF File Attachment via E-mail CodeUnit 08 JUN 2011

I’ve moved to the fantastic PHPMailer PHP class to handle all my e-mail send outs for my projects and have thus far been quite impressed by the ease-of-use and robustness of the class. Today I’ll quickly note how you can send an attachment (like a PDF file for example) with a mail send out.

As you can see from the code snippet below, attaching a file is as simple as passing a valid file path to the file to be included to the AddAttachment() function call and then hitting Send().

//create the mail class and fill in all the required settings
$mail = new PHPMailer();
$mail->Host = "";
$mail->SMTPAuth = true;
$mail->Username = "";
$mail->Password = "password1";
$mail->From = "";
$mail->FromName = "Software Simian";
$mail->AddAddress("", "Target Guy");
$mail->AddReplyTo("", "Software Simian");
$mail->WordWrap = 50;
$mail->Subject = "Subject";
$mail->Body    = "Message";
$mail->AltBody = strip_tags("Message:);

//and now for the actual bit when it comes to adding a file attachment to an e-mail

//and send. Now was that not easy?
    $resultstatus = 'Failed';

Couldn’t be any simpler, could it? :)

Related Link:

Related Posts:

About Craig Lotter

South African software architect and developer at Touchwork. Husband to a cupcake baker and father to two little girls. I don't have time for myself any more.