• We just launched and are currently in beta. Join us as we build and grow the community.

How to Send Mail To Gmail Using PHPMailer

Hamishe.tanha

Error Handling Specialist
H Rep
0
0
0
Rep
0
H Vouches
0
0
0
Vouches
0
Posts
39
Likes
183
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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. unset

    (

    $_SESSION

    [

    'result'

    ]

    )

    ;
  48. unset

    (

    $_SESSION

    [

    'status'

    ]

    )

    ;
  49. }
  50. ?>
  51. </div>
  52. </div>
  53. </body>
  54. </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. session_start

    (

    )

    ;

  5. if

    (

    isset

    (

    $_POST

    [

    'send'

    ]

    )

    )

    {

  6. $email

    =

    $_POST

    [

    'email'

    ]

    ;
  7. $subject

    =

    $_POST

    [

    'subject'

    ]

    ;
  8. $message

    =

    $_POST

    [

    'message'

    ]

    ;


  9. //Load composer's autoloader
  10. require

    'vendor/autoload.php'

    ;

  11. $mail

    =

    new

    PHPMailer(

    true

    )

    ;
  12. try {
  13. //Server settings
  14. $mail

    ->

    isSMTP

    (

    )

    ;
  15. $mail

    ->

    Host

    =

    'smtp.gmail.com'

    ;
  16. $mail

    ->

    SMTPAuth

    =

    true

    ;
  17. $mail

    ->

    Username

    =

    '[email protected]'

    ;
  18. $mail

    ->

    Password

    =

    'mypassword'

    ;
  19. $mail

    ->

    SMTPOptions

    =

    array

    (
  20. 'ssl'

    =>

    array

    (
  21. 'verify_peer'

    =>

    false

    ,
  22. 'verify_peer_name'

    =>

    false

    ,
  23. 'allow_self_signed'

    =>

    true
  24. )
  25. )

    ;
  26. $mail

    ->

    SMTPSecure

    =

    'ssl'

    ;
  27. $mail

    ->

    Port

    =

    465

    ;

  28. //Send Email
  29. $mail

    ->

    setFrom

    (

    '[email protected]'

    )

    ;

  30. //Recipients
  31. $mail

    ->

    addAddress

    (

    $email

    )

    ;
  32. $mail

    ->

    addReplyTo

    (

    '[email protected]'

    )

    ;

  33. //Content
  34. $mail

    ->

    isHTML

    (

    true

    )

    ;
  35. $mail

    ->

    Subject

    =

    $subject

    ;
  36. $mail

    ->

    Body

    =

    $message

    ;

  37. $mail

    ->

    send

    (

    )

    ;

  38. $_SESSION

    [

    'result'

    ]

    =

    'Message has been sent'

    ;
  39. $_SESSION

    [

    'status'

    ]

    =

    'ok'

    ;
  40. }

    catch (

    Exception $e

    )

    {
  41. $_SESSION

    [

    'result'

    ]

    =

    'Message could not be sent. Mailer Error: '

    .

    $mail

    ->

    ErrorInfo

    ;
  42. $_SESSION

    [

    'status'

    ]

    =

    'error'

    ;
  43. }

  44. header

    (

    "location: index.php"

    )

    ;

  45. }
  46. ?>

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.

2018-09-01_14_34_13-less_secure_app_access.png

DEMO

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!


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,526

323,535

Top