How to send email with PHPMailer?

Send mail with PHPmailer

Though php has an inbuilt mail() to send emails but it’s not flexible enough for sending mails. In some cases, emails send via mail() are not received by recipients although it was send succesfully without any error. Also the formatting of the cotent and mail header doesn’t come properly.To overcome some of these issues and to save extra bit of coding efforts PHPMailer is widely used.

So what is PHPMailer?

PHPMailer code library is one of the most popular open source email code library for php which was released in 2001.

Sending emails with PHPMailer

To send emails with the help of PHPMailer you first have to install it. To install it simply go to google and search for phpmailer

open the site appearing at top the search results.It is the official site for downloading phpmailer.

From the branch section select the stable version instead of master.

and hit the clone or download button and download the zip file.

after downloading the zip file extract the php mailer file

now we will create a form as form.php

source code for simple form

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Form to Send PHP Mail</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
<style>
.frm{
border:1px solid grey;
padding:15px;
}
</style>
</head>
<body>
 
<div class="container">
    <div class="row">
    <div class="col-md-4 frm">
  <h2>Form to Send PHP Mail</h2>
  <form action="mail.php" method="post" >
    <div class="form-group">
      <label for="name">Name:</label>
      <input type="text" class="form-control" id="name" placeholder="Enter name" name="name">
    </div>
    <div class="form-group">
      <label for="mail">Email:</label>
      <input type="email" class="form-control" id="mail" placeholder="Enter Email" name="email">
    </div>
    <button type="submit" name ="submit" class="btn btn-default">Submit</button>
  </form>
</div></div></div>
 
</body>
</html>

source code for sending mail using phpmailer

note: change the require file path

<?php
$name= $_POST['name'];
$email = $_POST['email'];
require 'PHPMailer/PHPMailerAutoload.php';

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'abc@domain.com';                 // SMTP username
$mail->Password = 'password';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->setFrom('abc@example', 'Mailer');
$mail->addAddress('abc@mail.com', 'Joe User');     // Add a recipient

$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Test mail';
$mail->Body    = 'this is a test mail';


if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

now to configure SMTP you have to login to your hosting cpanel

scroll down to email section of your cpanel account

on clicking email accounts you will see list of existing email from there goto connect devices.

after clicking connect devices scroll down to mail settings here you can find the details such as username,incoming server, outgoing server and port number.

now you got all necessary details which are required for configuring SMTP .To configure SMTP go to your code and enter username in username field ,password of you mail account in password field server detail in host field and port number in port field.

Note: To prevent resending of data on moving back and forth or on page refresh send email on button click.

Leave a Reply

Your email address will not be published. Required fields are marked *