Skip to main content

How to Send Mail To Gmail Using PHPMailer

Screenshot
Body

In this tutorial we will create a Sending Email Using PHPMailer. PHP is a server-side scripting language designed primarily for web development. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. It is mostly used by a newly coders for its user-friendly environment.

So Let's do the coding...

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html.

And, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Installing the PHPMailer

Using Composer

If you dont' have a composer installed on your machine you can get it here, https://getcomposer.org/download/

First, Create a file that will receive the phpmailer file, write these code inside the text editor and save it as composer.json.

  1. {
  2. "phpmailer/phpmailer": "~6.0"
  3. }

Then, open your command prompt and cd to your working directory. After that, type this command and hit enter afterward.

  1. composer require phpmailer/phpmailer

After installation you will see a different directory inside your working directory, namely vendor, etc.

Without Composer

To manually install the phpmailer to your app, first download the file here https://github.com/PHPMailer/PHPMailer

After you finish downloading the file, move the file inside the directory that you are working on. Now you're ready to use the phpmailer.

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php

.
  1. <?php session_start()?>
  2. <!doctype html>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  6. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  7. </head>
  8. <body>
  9. <nav class="navbar navbar-default">
  10. <div class="container-fluid">
  11. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  12. </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Send Mail To Gmail Using PHPMailer</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <div class="col-md-3"></div>
  19. <div class="col-md-6">
  20. <form method="POST" action="send_email.php">
  21. <div class="form-group">
  22. <label>Email:</label>
  23. <input type="email" class="form-control" name="email" required="required"/>
  24. </div>
  25. <div class="form-group">
  26. <label>Subject</label>
  27. <input type="text" class="form-control" name="subject" required="required"/>
  28. </div>
  29. <div class="form-group">
  30. <label>Message</label>
  31. <input type="text" class="form-control" name="message" required="required"/>
  32. </div>
  33. <center><button name="send" class="btn btn-primary"><span class="glyphicon glyphicon-envelope"></span> Send</button></center>
  34. </form>
  35. <br />
  36. <?php
  37. if(ISSET($_SESSION['status'])){
  38. if($_SESSION['status'] == "ok"){
  39. ?>
  40. <div class="alert alert-info"><?php echo $_SESSION['result']?></div>
  41. <?php
  42. }else{
  43. ?>
  44. <div class="alert alert-danger"><?php echo $_SESSION['result']?></div>
  45. <?php
  46. }
  47.  
  48. unset($_SESSION['result']);
  49. unset($_SESSION['status']);
  50. }
  51. ?>
  52. </div>
  53. </div>
  54. </body>
  55. </html>

Creating the Main Function

This is the main function of the application. This code will send requests via phpmailer to be able to send mail to google SMTP. To do that copy and write these inside the text editor, then save it as send_email.php

. Note:

To make this script work without composer, make sure you change this code:

  1. require 'vendor/autoload.php';
Into this:
  1. require 'path/to/PHPMailer/src/Exception.php';
  2. require 'path/to/PHPMailer/src/PHPMailer.php';
  3. require 'path/to/PHPMailer/src/SMTP.php';

So that it will recognize the phpmailer class when you call it in the code.

  1. <?php
  2. use PHPMailer\PHPMailer\PHPMailer;
  3. use PHPMailer\PHPMailer\Exception;
  4.  
  5.  
  6. if(isset($_POST['send'])){
  7.  
  8. $email = $_POST['email'];
  9. $subject = $_POST['subject'];
  10. $message = $_POST['message'];
  11.  
  12.  
  13. //Load composer's autoloader
  14. require 'vendor/autoload.php';
  15.  
  16. $mail = new PHPMailer(true);
  17. try {
  18. //Server settings
  19. $mail->isSMTP();
  20. $mail->Host = 'smtp.gmail.com';
  21. $mail->SMTPAuth = true;
  22. $mail->Username = '[email protected]';
  23. $mail->Password = 'mypassword';
  24. $mail->SMTPOptions = array(
  25. 'ssl' => array(
  26. 'verify_peer' => false,
  27. 'verify_peer_name' => false,
  28. 'allow_self_signed' => true
  29. )
  30. );
  31. $mail->SMTPSecure = 'ssl';
  32. $mail->Port = 465;
  33.  
  34. //Send Email
  35. $mail->setFrom('[email protected]');
  36.  
  37. //Recipients
  38. $mail->addAddress($email);
  39. $mail->addReplyTo('[email protected]');
  40.  
  41. //Content
  42. $mail->isHTML(true);
  43. $mail->Subject = $subject;
  44. $mail->Body = $message;
  45.  
  46. $mail->send();
  47.  
  48. $_SESSION['result'] = 'Message has been sent';
  49. $_SESSION['status'] = 'ok';
  50. } catch (Exception $e) {
  51. $_SESSION['result'] = 'Message could not be sent. Mailer Error: '.$mail->ErrorInfo;
  52. $_SESSION['status'] = 'error';
  53. }
  54.  
  55. header("location: index.php");
  56.  
  57. }
  58. ?>

Note: To be able to send a mail to Gmail, make sure you provide your own Gmail username and password or much better is to make a dummy account to use.

Inside your Google account make sure you allow the less secure apps so that you can have permission to send the email.

tut1DEMO

There you have it we successfully created a Send Mail To Gmail Using PHPMailer. I hope that this simple tutorial helps you with your projects. For more updates and tutorials just kindly visit this site.

Enjoy Coding!

Add new comment