Send an email with PHP

Yashod Perera
3 min readMay 7, 2020

In this post I am going to describe how to send an email to someone using smtp server in PHP. Let’s get started.

There are 2 sections in this post as follows.

  1. Make the HTML template.
  2. Embed the PHP code and send email.

Project file structure

The project file structure as follows. In this tutorial I have user PHPMailer 5.2-stable.

Project
|
-- lib
| |
| -- PHPMailer // The PHPMailer dependency.
|
-- index.php

Make the HTML template

Let’s make a simple form using html and bootstrap(you can find the full code here) .

<form action="index.php" method="POST" style="margin:20px;padding:20px;">
<h4>Send Email</h4>
<div class="form-group">
<label for="name">Name :</label>
<input name="userName" type="text" class="form-control" placeholder="Enter Name" id="name">
</div>
<div class="form-group">
<label for="email">Email address :</label>
<input name="userEmail" type="email" class="form-control" placeholder="Enter email" id="email">
</div>
<div class="form-group">
<label for="subject">Subject :</label>
<input name="subject" type="text" class="form-control" placeholder="Enter Subject" id="subject">
</div>
<div class="form-group">
<label for="message">Message :</label>
<input name="message" type="text" class="form-control" placeholder="Enter Message" id="message">
</div>
<button name="submit" type="submit" class="btn btn-primary">Submit</button>
</form>

when the form is submitting it will reach the index.php file using the POST method. Then all the fields can be accessible using the name parameter of each field as $_POST["name"] .

Embed the PHP code and send email.

Let’s embed the php code for send the email. First let’s get the form submitted values as follows.(In this post I am not going to validate).

<?php           
if(isset($_POST["submit"])){
$name = $_POST["userName"]; // Get the name.
$email = $_POST["userEmail"]; // Get the email address.
$subject = $_POST["subject"]; // Get the subject.
$message = $_POST["message"]; // Get the message.
}
?>

Then let’s add the PHPMailer to the code and send an email.

<?php
// Added the dependency
require_once('lib/PHPMailer/PHPMailerAutoload.php');
if(isset($_POST["submit"])){
$name = $_POST["userName"];
$email = $_POST["userEmail"];
$subject = $_POST["subject"];
$message = $_POST["message"];
// Use PHPMailer class.
$mail = new PHPMailer();

$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
// SMTP host
$mail->Host = '<smtp-host>';

// SMTP port
$mail->Port = '<smtp-port>';
$mail->isHTML();
// Add email of the SMTP account.
$mail->Username = '<email>';

// Add password of the SMTP account
$mail->Password = '<password>';
$mail->SetFrom('<email>','<Name>');

// Add subject.
$mail->Subject = "$subject";
// Add message.
$mail->Body = "$message";
// Add senders' address.
$mail->AddAddress("$email");
$result = $mail->Send();
if($result == 1){
header("Location: index.php");
} else {
echo "Sorry. Failure Message";
}
}
?>

You have to fill the smtp host, port, account email, password and the name.

Enable debug output in SMTP

Always we have to debug the code to find if there are errors. In this package there are several options to enable debugging as follows.

$mail->SMTPDebug = SMTP::DEBUG_SERVER;

The full document can be found here.

Get the Error

You can directly get the error information using the ErrorInfo variable as follows.

if($result != 1)
echo "Mailer Error: " . $mail->ErrorInfo;
}

Quick tip: Use Google SMTP service

  • Go to your gmail and account settings -> security and turn off two factor authentication.
  • Turn on Less secure app access.
  • Then change the host and the port as follows and you can user your gmail and password as the smtp account and the password.
$mail->Host = 'smtp.gmail.com';
$mail->Port = '465';

You can find the full code in the following repository.

Hope you get a better understanding about sending email in PHP.

If you have found this helpful please hit that 👏 and share it on social media :)

--

--

Yashod Perera

Technical Writer | Tech Enthusiast | Open source contributor