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>
        <div class="form-group col-md-6">
        <input type="text" class="form-control" name="last" id="last" placeholder="Last Name">
        </div>
        </div>
        <div class="form-row">
        <div class="form-group col-md-6">
        <input type="email" class="form-control" name="email" id="email" placeholder="Email">
        </div>
        <div class="form-group col-md-6">
        <input type="text" class="form-control" name="phone" id="phone" placeholder="Phone">
        </div>
        </div>
        <div class="form-group">
        <textarea type="textarea" name="message" class="form-control" id="message" placeholder="Comments / Questions"></textarea>
        </div>
        <button type="submit" name="submit" class="btn btn-success">Submit</button>
 </form>

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:

<?php
    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 = 'signatureofficestaff@yahoo.com';

    $mail->SMTPDebug = SMTP::DEBUG_SERVER;
    $mail->isSMTP();
    $mail->Host = 'smtp.mailtrap.io';
    $mail->SMTPAuth = true;
    $mail->Username = 'e23fc963aa6ba3';
    $mail->Password = '39a3bce5cce080';
    $mail->SMTPSecure = 'tls';
    $mail->Port = 2525;

    //Recipients
    $mail->setFrom($email, $first.' '.$last);
    $mail->addAddress('joe@example.net', 'Joe User');     //Add a recipient
    // $mail->addAddress('ellen@example.com');               //Name is optional
    // $mail->addReplyTo('info@example.com', 'Information');
    // $mail->addCC('cc@example.com');
    // $mail->addBCC('bcc@example.com');

    //Content
    $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';

    $mail->send();
    header('location:contact-response.php');
    }
?>

Sending an Email with Attachments

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

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require_once "vendor/autoload.php";

$mail = new PHPMailer;

$mail->From = "from@yourdomain.com";
$mail->FromName = "Full Name";

$mail->addAddress("recipient1@example.com", "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->isHTML(true);

$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 {
    $mail->send();
    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