Send E-Mail in PHP via SMTP Server using PHPMailer

PHP Email Send via SMTP server using PHPMailer Example; In this tutorial you will learn how to send mail in php with contact us form using PHPMailer Library.

We can use mail() function to send email in php but PHPMailer is an alternative to mail() function which is used more often.  PHPMailer provides an object-oriented interface, whereas mail() is not object oriented. There come many scenarios when mail() function is not much flexible.

So, Let’s see how to send email in php via SMTP server using PHPMailer service.

How to Send E-Mail in PHP

Here is the step by step guide for sending email in php using phpmailer.

First at all you need to create a contact form page for sending mail using php.

Create Contact Form

<form action="" method="POST">
        <div class="form-row">
        <div class="form-group col-md-6">
        <input type="text" class="form-control" name="first" id="first" placeholder="First Name">
        <div class="form-group col-md-6">
        <input type="text" class="form-control" name="last" id="last" placeholder="Last Name">
        <div class="form-row">
        <div class="form-group col-md-6">
        <input type="email" class="form-control" name="email" id="email" placeholder="Email">
        <div class="form-group col-md-6">
        <input type="text" class="form-control" name="phone" id="phone" placeholder="Phone">
        <div class="form-group">
        <textarea type="textarea" name="message" class="form-control" id="message" placeholder="Comments / Questions"></textarea>
        <button type="submit" name="submit" class="btn btn-success">Submit</button>

Install PHPMailer

To install PHPmailer in PHP projects, run the below shown composer command::

composer require phpmailer/phpmailer

Send Mail using phpmailer

SMTP is basically a protocol which send email request to a mail server and sends it to the destination mail server after verification. You can send email from local web server by using the code as below:

    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\SMTP;
    use PHPMailer\PHPMailer\Exception;

    require '../vendor/autoload.php';

    if (isset($_POST['submit'])) {
    $mail = new PHPMailer(true);

    $first = $_POST['first'];
    $last = $_POST['last'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $message = $_POST['message'];
    $to = '';

    $mail->SMTPDebug = SMTP::DEBUG_SERVER;
    $mail->Host = '';
    $mail->SMTPAuth = true;
    $mail->Username = 'e23fc963aa6ba3';
    $mail->Password = '39a3bce5cce080';
    $mail->SMTPSecure = 'tls';
    $mail->Port = 2525;

    $mail->setFrom($email, $first.' '.$last);
    $mail->addAddress('', 'Joe User');     //Add a recipient
    // $mail->addAddress('');               //Name is optional
    // $mail->addReplyTo('', 'Information');
    // $mail->addCC('');
    // $mail->addBCC('');

    $mail->isHTML(true);                                  //Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = $message;
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';


Sending an Email with Attachments

Here’s an example of how to send an email with attachments using PHPMailer:

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require_once "vendor/autoload.php";

$mail = new PHPMailer;

$mail->From = "";
$mail->FromName = "Full Name";

$mail->addAddress("", "Recipient Name");

//Provide file path and name of the attachments
$mail->addAttachment("file.txt", "File.txt");        
$mail->addAttachment("images/profile.png"); //Filename is optional


$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";

try {
    echo "Message has been sent successfully";
} catch (Exception $e) {
    echo "Mailer Error: " . $mail->ErrorInfo;

I hope send email in php example work for you.

Leave a Comment