Send Email in PHP using Mail Function (SMTP)

Send Email in PHP using Mail Function – For Linux and Windows

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.

Tushar Soni

I am Tushar Soni, Co - Founder of CodingAlpha. I am a computer science student from India and passionate about Web Development and Programming. Connect with me on Facebook | LinkedIn | Google Plus

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

  • October 8, 2016 at 8:20 pm

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

  • October 14, 2016 at 10:11 pm

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


Join The Discussion