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

Steps:

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

index.html code

<!DOCTYPE HTML>
<html>	
<head></head>
<body>
	<form method="post" action="send_mail.php">
		Name: <input type="name" class="form-control" name="sender_name" placeholder="Name">
		<input type="email" name="email" placeholder="Email">
		<textarea name="message" rows="5" placeholder="Message"></textarea>
		<input type="submit" value="Send Message">
	</form>
</body>
</html>

send_mail.php code

<?php
$recepient = "tushar.soni@outlook.com";
$subject = "CodingAlpha";
$message = $_POST['message'];
$email = $_POST['email'];
mail($recepient, $subject, $message, $email);
 echo '<center><h4 style="background-color: blue;">Your mail is sent. You will be redirected in 3 seconds.</h4></center>';
?>

1. Install SMTP Mail Server (Linux)

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

 
sudo apt-get install ssmtp

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.

mailhub=smtp.gmail.com:587
AuthUser=tushar.s1994@gmail.com
AuthPass=email_password
UseSTARTTLS=YES

# You can only do this if you've verified your domain with Gmail.
# If you haven't, delete, or add a # before this
#hostname=yourwebsite.com

FromLineOverride=YES

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.

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25

; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = me@example.com

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
 sendmail_path = /usr/sbin/ssmtp -t

; Force the addition of the specified parameters to be passed as extra parameters
; to the sendmail binary. These parameters will always replace the value of
; the 5th parameter to mail().
;mail.force_extra_parameters =

; Add X-PHP-Originating-Script: that will include uid of the script followed by the filename
mail.add_x_header = On

; The path to a log file that will log all mail() calls. Log entries include
; the full path of the script, line number, To address and headers.
;mail.log =
; Log mail to syslog (Event Log on Windows).
;mail.log = syslog

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.

    Reply
  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.

    Reply
  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

    Reply

Let's Discuss