How to send attachment mail in PHP using PHPMailer

This article looks at how you can attach a file to an email in PHP using PHPMailer. We have already written a tutorial on send email using PHPMailer. In this tutorial we didn’t go to re-invent the complete code simply add attachment and proceed.

Download the PHPMailer library.

Extract the files to a folder in your application.

Include the class.phpmailer.php file in your code require_once(‘PHPMailer/class.phpmailer.php’);

Implement the code as below:


$mail  = new PHPMailer(); 
$address = "";
$mail->SetFrom('', 'demo');
$mail->AddAddress($address, "Demo");
$mail->Subject    = "Attachment Test";
$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!";
$body  = "Mail body in HTML";

	echo "Mailer Error: " . $mail->ErrorInfo;
	echo "Message sent!";

Leave a Reply

Your email address will not be published. Required fields are marked *