How to send email with an attachment in PHP using PHPMailer

Firstly, download the PHPMailer library from the given link :

https://github.com/PHPMailer/PHPMailer

Now extract the zip folder and rename it to ‘PHPMailer’ and place it in your root folder where all the php files are located.

Then after put this code :


require_once('PHPMailer/class.phpmailer.php');
$mail = new PHPMailer();
$mail->From = ''; /* put the email from which the email is to be sent */
$mail->FromName = ''; /* put the name of a person who is sending email */
$mail->Subject = ''; /* give a subject of email if you want */
$message_body = "Hello, sending mail using PHPMailer";
$mail->Body = $MESSAGE_BODY;
$mail->AddAddress( '' ); /* put the email of the recipient */

$mail->AddAttachment('PATHTOFILE'); /* put the path to the file */

if ($mail->Send())
{
echo "Mail Sent";
}
else
{
echo "Could not send mail";
}

Advertisements