Header Ads

How to send email from localhost using PHP

In this tutorial, we will learn how to send an email from localhost using PHP and Gmail. In the PHP web application, mail() function used to send the mail. But mail() function will not work in the localhost environment. In this tutorial, we will send an email from the localhost system using PHP and Gmail.

In this tutorial, we will use PHPmailer to send email from the localhost using PHP. The PHPmailer library provides the way to send an email from localhost with the SMTP server using PHP. We will use the Gmail account as an SMTP server to sending the email from localhost. Before using the Gmail SMTP server we need to configure the setting in our Gmail account.

  • Login into your google account.
  • Go to the security page.
  • Scroll down to the Less secure app access section. Turn on Less secure app access.

Code for sending email from localhost

Explanation of the above code

  • Include the PHPMailer library and create an instance of this class.
  • Set SMTP credentials (host, username, password, and port).
  • Specify sender name and email ($mail->setFrom('yourgmailid@gmail.com', 'Your_Name')).
  • Set recipient email address ($mail->addAddress($toemail)).
  • Set email subject ($mail->Subject).
  • Set the body content of the email ($mail->Subject =$subject;).
  • Use the mail->send() method of PHPMailer class to send an email.

Here is the Full code with HTML Form and PHP Code


Download Source Code (How to send email from localhost using PHP)
Size: 134 KB
Version: V 1.0
 
Powered by Blogger.