Send an Email via Gmail SMTP Server in PHP using PHPMailer

In this tutorial, we going to see how to send an email via Gmail SMTP server in PHP using PHPMailer. We send an email with different mail transfer libraries using PHP. In this, We are going to use PHPMailer. These libraries give excellent features like SMTP authentication and more.
Using Gmail SMTP server sending emails by using PHPMailer. PHPMailer library is famous because of its excellent features are available. Some of those features are below,
1. PHPMailer supports both HTML content and plain text.
2. We can able to use an array of email addresses like to, cc, bcc, reply-to.
3. It also gives Secure/MIME encryption with email validation, SMTP authentication including multiple language support.
Before using Gmail SMTP.
To send an email you can use the mail server of another host but for this, you need first to have authentication. E.g. You need to send an email from Gmail mail server first you require Gmail account.
Gmail SMTP needs the TLS encryption so we set it accordingly. If you want to send mail via SMTP, you want to find the Hostname, Port number, SMTPSecure encryption type if authentication is needed you also want the username and password.
Now need to do some changes in your Gmail account settings. First login and open your Gmail account settings.
1. We want to off the 2-Step Verification before using it.
2. Next, go to less secure apps. The default Gmail less secure apps are in off. So here, you need to keep Allow less secure apps: ON.
Required files are needed:
Now we going to use a PHPMailer library which you can download on GitHub.
In this, we going to send a mail using TextBox and TextArea without saving data in the database. Below you can see the full code.
index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
<!doctype html> <html> <head> <meta charset="UTF-8"> <title>SoftAOX - Send an Email via Gmail SMTP Server in PHP using PHPMailer</title> <link href="style.css" rel="stylesheet"> <link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css"> <script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script> </head> <body> <br/> <div class="inner contact"> <!-- Form Area --> <div class="contact-form"> <h1 align="center">SoftAOX</h1> <h2 align="center">Send an Email via Gmail SMTP Server in PHP using PHPMailer</h2> <hr> <!-- Form --> <form action="mail.php" method="post"> <!-- Left Inputs --> <div class="col-xs-6"> <h3>Enter Your Email ID and Password</h3> <p>Before using your Gmail ID and Password check whether your <a href="https://support.google.com/accounts/answer/6010255?hl=en" target="_blank" rel="noopener noreferrer">Gmail less secure apps</a> is <b>on</b> or <b>not</b>.</p> <input type="text" name="email" required class="form" placeholder="Enter your email ID" /> <input type="password" name="password" required class="form" placeholder="Password" /> </div> <!-- End Left Inputs --> <!-- Right Inputs --> <div class="col-xs-6"> <h3>To Address</h3> <input type="email" name="toid" required class="form" placeholder="To : Email Id " /> <input type="text" name="subject" required class="form" placeholder="Subject" /> <textarea name="message" class="form textarea" placeholder="Message"></textarea> </div> <!-- End Right Inputs --> <!-- Submit --> <button type="submit" id="submit" name="send" class="form-btn semibold">Send Message</button> <!-- End Submit --> <!-- Clear --> <div class="clear"></div> </form> </div> <!-- End Contact Form Area --> </div> </body> </html> |
mail.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
<?php require_once 'PHPMailer/PHPMailerAutoload.php'; if(isset($_POST['send'])) { // Fetching data that is entered by the user $email = $_POST['email']; $password = $_POST['password']; $to_id = $_POST['toid']; $message = $_POST['message']; $subject = $_POST['subject']; // Configuring SMTP server settings $mail = new PHPMailer; $mail->isSMTP(); $mail->Host = 'smtp.gmail.com'; $mail->Port = 587; $mail->SMTPSecure = 'tls'; $mail->SMTPAuth = true; $mail->Username = $email; $mail->Password = $password; $mail->FromName = "SoftAOX - PHP Mailer"; // Email Sending Details $mail->addAddress($to_id); $mail->Subject = $subject; $mail->msgHTML($message); // Success or Failure if (!$mail->send()) { $error = "Mailer Error: " . $mail->ErrorInfo; echo '<p>'.$error.'</p>'; } else { echo '<p>Message sent!</p>'; } } else{ echo '<p>Please enter valid data</p>'; } ?> |
The below code is used to require the PHPMailerAutoload file. This PHPMailerAutoload file is available in PHPMailer library.
1 |
require_once 'mailerClass/PHPMailerAutoload.php'; |
style.css
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
#contact{ padding:10px 0 10px; } .form{ width: 100%; padding: 15px; background:#ff9800; border:1px solid rgba(0, 0, 0, 0.075); margin-bottom:25px; color:#727272 !important; font-size:13px; -webkit-transition: all 0.4s; -moz-transition: all 0.4s; transition: all 0.4s; } .form:hover{ border:1px solid #8BC3A3; } .form:focus{ color: white; outline: none; border:1px solid #8BC3A3; } .textarea{ height: 120px; max-height: 120px; max-width: 100%; } .button{ padding:8px 12px; background:#ff9800; display: block; width:120px; margin:10px 0 0px 0; border-radius:3px; -webkit-transition: all 0.3s; -moz-transition: all 0.3s; transition: all 0.3s; text-align:center; font-size:0.8em; box-shadow: 0px 1px 4px rgba(0,0,0, 0.10); -moz-box-shadow: 0px 1px 4px rgba(0,0,0, 0.10); -webkit-box-shadow: 0px 1px 4px rgba(0,0,0, 0.10); } .button:hover{ background:#8BC3A3; color:white; } .form-btn{ width:180px; display: block; height: auto; padding:15px; color:#fff; background:#ff9800; border:none; border-radius:3px; outline: none; -webkit-transition: all 0.3s; -moz-transition: all 0.3s; transition: all 0.3s; margin:auto; box-shadow: 0px 1px 4px rgba(0,0,0, 0.10); -moz-box-shadow: 0px 1px 4px rgba(0,0,0, 0.10); -webkit-box-shadow: 0px 1px 4px rgba(0,0,0, 0.10); } .form-btn:hover{ background:#111; color: white; border:none; } .form-btn:active{ opacity: 0.9; } |
Download
Conclusion:
I believe in this tutorial you feeling happy with sending an email via Gmail SMTP Server in PHP using PHPMailer. Using other SMTP servers also you can try to send mail.
Mraj
Creative Designer & Developer specialist by the spirit and a loving blogger by thoughts. If you have any questions let me drop an email with the article name to the following email id: [email protected]