Spread the love

 

how php send email from

Here in this tutorial, we are going to learn step by step that how php send email from an application using phpmailer, which is one of the most popular and widely used open source mail sending library.

You might be known that There are a lot of ways out there which we use to include php code to send email from contact form of our application like php mail() function, SwiftMailer, PAER, eZ component, and Zend Mail etc. but in this tutorial our primary focus on PHPMailer.

Why PHPMailer?

  • We use PHPMailer, due to its easy simple object-oriented nature.
  • One of The top reason is the PHPMailer is the most widely used library due to which very high technical support are available out there on the internet.
  • It also offers the most advanced features like security (encryption), HTML based emails, file attachments etc.

Why PHPMailer the best choice than mail () function?

There are many ways, php send email from the application, one of them is the use of mail ()   function.

  • But the problem with this function it doesn’t fully support the advanced features like sending HTML based emails and file attachments etc.
  • Another reason is mail() send an email via local mail server using Send mail binary on window or Linux based operating system some time its configuration is a very tedious task.

On the other hand, PHPMailer integrates SMTP standers, allows sending mail without the local mail server.

Note: there are many alternatives to PHPMailer like SwiftMailer, PAER, eZ component and Zend Mail if you like those, you can use it, according to your requirement.

Installation of PHPMailer:

Th PHPMailer up to version-5 offer a special file which we just included to our working file, the name of that file was “PHPMilerAutoLoad.php” which is no longer exist in the latest version-6 of PHPMailer library.

Now, if we want to install the latest version of PHPMailer, we have two choices.

  • The First one is to the use of composer to install it.
  • The second one is to install manually.
Installation of the composer on window operating system:

Following are the simple steps to install composer.

  1. First of all, install XAMPP or WAMP on your computer.
  2. Download composer from site https://getcomposer.org/download/ and installed it according to the instruction and make sure to select the php.exe file otherwise the composer doesn’t work.

Screenshot of the composer installation

php send email from

 3.When the composer successfully installed use command prompt and visit to the directory where you want to install the PHPMailer library just run the command like “composer require phpmailer/phpmailer

4. Now your phpmailer successfully installed and ready for use.

Manually installed phpmailer:

If you don’t want to use composer to install the mailer library then what you have to do you just need to download the source file of phpmailer and include the following file to your working file.

//file of php mailer manually include

Now it is a good experience to using the using directive because phpmailer define all there features under PHPMailer\PHPMailer namespace.

Now open the php code editor and type the following script

//code of the PHPMailer class

2.mail with attachment:

 

Sending email using SMTP:

SMTP stands for “simple mail transfer protocol” which is mostly used by the mail clients to send email sending the request to the mail server. When mail server verifies the email then it sends to the destination mail server.

In the following code of php send email from email using Gmail SMTP .if we want to send email using Gmail SMTP. We have to do some essential setting in Gmail account.

1.step verification off:

php email smtp

2. Allow less secure app on:

php email smtp

Now open  your text editor and type the following code.

 

Conclusion:

In the above tutorials, we used phpmailer to send emails from the local web server, send E-mail with attachments, and also send email using Gmail SMTP servers.