DEV Community

Cover image for How to send an email to 2 people easily with PHP
Yahaya Oyinkansola
Yahaya Oyinkansola

Posted on

How to send an email to 2 people easily with PHP

Creating a web form that sends an email to someone is a very common task for developers, but from most tutorials i have seen online, they only show you how you can send an email to one person (i.e the person who fills a form), but not to 2 people at the same time. In this article, i want to show you how i have learnt to send an email to 2 people using PHP.

Intro

To demonstrate how this would work, we will build a Contact us form that sends an email to the person who fills the form, and another one to an email address we specify that contains the persons details and message.

Let's create a new project and install PHPMailer using Composer as we would use PHPMailer to send the emails

composer require PHPMailer/PHPMailer
Enter fullscreen mode Exit fullscreen mode

Now that we have installed PHPMailer, let's create the contact us form. Create a file called contact.html and put this code in it

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>PHP Contact Form</title>
    <link rel="stylesheet" href="contact.css" />
  </head>

  <body>
    <form action="process.php" method="POST">
      <div class="form-container">
        <div class="form-item">
          <label for="name">Full Name</label>
          <input type="text" name="name" id="name" />
        </div>

        <div class="form-item">
          <label for="email">Email</label>
          <input type="text" name="email" id="email" />
        </div>

        <div class="form-item">
          <label for="message">Message</label>
          <textarea name="message" id="message" name="message" rows="5"></textarea>
        </div>

        <button class="btn-submit" name="submit">Send Message</button>
      </div>
    </form>
  </body>
</html>
Enter fullscreen mode Exit fullscreen mode

Then create a contact.css file

body {
  font-family: Verdana, sans-serif;
}

.form-container {
  width: 600px;
  padding: 30px;
}

.form-item:not(:first-child) {
  margin-top: 20px;
}

.form-item label {
  display: inline-block;
  font-weight: bold;
  margin-bottom: 10px;
}

.form-item input[type="text"],
.form-item textarea {
  display: block;
  width: 100%;
  padding: 0.375rem 0.75rem;
  font-size: 1rem;
  line-height: 1.5;
  color: #495057;
  background-color: #fff;
  background-clip: padding-box;
  border: 1px solid #ced4da;
  border-radius: 0.25rem;
}

.form-item textarea {
  resize: none;
}

.btn-submit {
  display: inline-block;
  font-weight: 400;
  text-align: center;
  border: 1px solid transparent;
  padding: 0.375rem 0.75rem;
  font-size: 1rem;
  line-height: 1.5;
  border-radius: 0.25rem;
  background-color: #f23a2e;
  color: #fff;
  margin-top: 15px;
}
Enter fullscreen mode Exit fullscreen mode

(This is how the form looks)
Contact us form

Now that the form is created, let's build the logic. When the form gets submitted, the form data is sent to process.php. Here is the code that handles sending the emails to their respective destinations

<?php
  require_once "vendor/autoload.php";

  use PHPMailer\PHPMailer\PHPMailer as PHPMailer;

  if ($_SERVER["REQUEST_METHOD"] == "POST) {
    $name = $_POST["name"];
    $email = $_POST["email"];
    $message = $_POST["message"];

    $mail = new PHPMailer();
    $mail->isSMTP();
    $mail->SMTPDebug = 0;
    $mail->SMTPAuth = true;
    $mail->SMTPSecure = "tls"; // or ssl
    $mail->Port = 587; // or 465
    $mail->Host = "smtp.gmail.com"; // Your Host Server
    $mail->Username = "yahayaoyinkansola@gmail.com"; // Your Gmail address
    $mail->Password = "password123"; // Your Gmail password

    // WHERE THE EMAIL IS COMING FROM
    $mail->setFrom("yahayaoyinkansola@gmail.com");

    // WHO THE EMAIL IS GOING TO
    $mail->addAddress("$email", "$name");

    // SUBJECT OF THE MESSAGE
    $mail->Subject = "Hi $name, we have received your message";

    // THE MESSAGE BODY
    $mail->Body = "Thank you for contacting us, we will get back to you shortly";
    $mail->send();

    $autoRespond = new PHPMailer();
    $autoRespond->isSMTP();
    $autoRespond->SMTPDebug = 0;
    $autoRespond->SMTPAuth = true;
    $autoRespond->SMTPSecure = "tls";
    $autoRespond->Port = 587;
    $autoRespond->Host = "smtp.gmail.com";
    $autoRespond->Username = "yahayaoyinkansola@gmail.com";
    $autoRespond->Password = "password123";
    $autoRespond->setFrom("yahayaoyinkansola@gmail.com");
    $autoRespond->addAddress("Kansoldev@gmail.com");
    $autoRespond->Subject = "You have received a message from $name";
    $autoRespond->Body = "Name: $name\n Email: $email\n Message: $message";
    $autoRespond->send();
  }
Enter fullscreen mode Exit fullscreen mode

So what exactly is happening here?, let me explain

  1. We first check for the request method being sent to ensure we process the data with the right parameters
  2. We assign each POST value to specific variables (In a real world scenario, this POST values would be validated)
  3. We then instantiate PHPMailer to send an email to the user that filled the form, to inform them their message has been sent. If you don't know how to configure your gmail account to send emails with PHPMailer, you can learn how to do it from here
  4. We then create another instance of PHPMailer to send an email to another email address (i.e Kansoldev@gmail.com), that contains the details of the user.

Conclusion

You can do this for multiple email addresses and send each of them an email. If you have any suggestion or better ways of implementing this, let me know in the comments. Thanks for reading.

Top comments (0)