PhpMailer is one of the most popular opensource library to send email from your application. PHPMailer is an alternate of mail() function. PHPMailer have lots of function which really very helpful. In most of case we can use PHP mail() method to send email but in some case like sending attachment, send html formatted email is not easy task to accomplish with php mail() if you dont have good experience in PHP it was headache for you, so PHPMailer provide all this in very easy way and you just need to  call its method and done. To configure PHPMailer follow simple steps.

Send Email using PHPMailer with attachment

Step 1. Installing PHPMailer

You can install PHPMailer by two ways first using composer:

And second one you can manually download library from Git using this link

https://github.com/PHPMailer/PHPMailer

Step 2. After download you can extract zip file you can place this folder inside your project.

Step 3. Now create new file mail.php  in which we can include PHPMailer library. Now we can call PHPMailer function to send email.

Simple email with text and HTML

In above code we can first include library and after that create instance of PHPMailer class and set SMTP info i just put my domain name, after that we can call method to set emails, message, subject etc. It is easy to understand every method by its name. To enable HTML email you need to call IsHTML method

Send Email with Attachment

to attach image or file with email we call addAttachment  method and pass our file location

Hope this post will help you to build better email system in you application 🙂

Posted in: php