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.
1. Install SMTP Server
2. Edit SSMTP.CONF File
3. Edit PHP.INI File
<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">
$recepient = "email@example.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.
# You can only do this if you've verified your domain with Gmail.
# If you haven't, delete, or add a # before this
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.
; For Win32 only.
SMTP = localhost
smtp_port = 25
; For Win32 only.
;sendmail_from = firstname.lastname@example.org
; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
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().
; 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.
; 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.