Send Email in PHP using Mail Function (SMTP)

By | October 8, 2016

Learn How to Send Email in PHP with localhost servers. This code works on LAMP, WAMP and XAMPP servers. This code makes use of Gmail’s SMTP Server for PHP mailer script. SMTP is an abbreviated form of Simple Mail Transfer Protocol.

Get complete details on using PHP Mail function using gmail SMTP mail server. This is a very simple php mail script that can be modified. You can modify the php email sender function to send attachments . You can add your CSS scripts later on. There are some steps that should be followed for sending email in PHP localhost to your mailbox.

What is SMTP?

Simple Mail Transfer Protocol is an Internet standard for sending electronic mails. SMTP uses TCP Port 25. SMTPS is an extension to SMTP that includes SSL connections.

E-mail servers and other agents use SMTP to send and receive e-mails. For retrieving messages, client applications usually use either IMAP or POP3.

Send email in php using PHP Mail Function using Localhost in Linux and Windows


1. Install SMTP Server
2. Edit SSMTP.CONF File
3. Edit PHP.INI File


index.html code

send_mail.php code

1. Install SMTP Mail Server (Linux)

To install the mail server, execute the following command on Linux Terminal.

Now, you will have to configure your php.ini file and ssmtp.conf files.

2. ssmtp.conf configuration:

File Location: /etc/ssmtp/ssmtp.conf

This code will send php email using Gmail’s SMTP Server. Change AuthUser and AuthPass as per your preference. Gmail IDs work efficiently here.

3. php.ini configuration

File Location: /etc/php/7.0/apache2/php.ini


Configure the PHP Mail Function here. Search for [mail function] and replace the following code. The sendmail_path has been modified here.

Now, try sending an email. If you have any errors in this guide to send email in PHP using PHP Mail function in LAMP, WAMP or XAMPP, let us know about it in the comment section below.

6 thoughts on “Send Email in PHP using Mail Function (SMTP)

  1. Raju Saigal

    Finally, I can now send email in php. I installed it on Linux localhost with LAMP server. Thanks.

  2. Prakash Sawant

    Thanks a lot. Finally, I configured my SMTP Server in Linux Ubuntu and now I can send emails in PHP through my Gmail ID.

  3. ebube

    I still don’t believe it worked. The code is somehow in the sense that there were no conditions given for if it does not deliver


Let's Discuss