Sending Email With Attachment Using PHPMailer In PHP | CampCodes

Sending Email with Attachment using PHPMailer in PHP

May 30, 2020

Getting Started

I’ve used CDN for Bootstrap in this tutorial, so you need an internet connection for it to work. If ever you want full documentation of this plugin, you can visit Github.

Installing the PHPMailer using Composer

If you don’t have Composer yet, you may use this link to download the latest version.

1. Create a folder of your app, in my case I’ve created my app inside htdocs folder of my xampp and name it phpmailer.

2. Create a file named Composer, JSON inside your app folder and put the ff. Line inside it. This will tell Composer to install phpmailer to our app.

     "phpmailer/phpmailer": "~6.0"

3. Open command prompt, navigate to your app and type the ff.

composer require phpmailer/phpmailer

This will install PHPMailer for our app.

4. After installation, you should be able to see the vendor folder.

Creating a Send Email Form

Next, we’re going to create a form to send email using PHPMailer, and this will be our index.php.

<!DOCTYPE html>
    <meta charset="utf-8">
    <title>Sending Email using PHPMailer</title>
    <link href="" rel="stylesheet">
<div class="container">
    <h1 class="page-header text-center">Sending Email using PHPMailer</h1>
    <div class="row">
        <div class="col-sm-4 col-sm-offset-4">
                    <div class="alert alert-info text-center">
                        <?php echo $_SESSION['message']; ?>
            <form method="POST" action="send.php" enctype="multipart/form-data">
                <div class="form-group">
                    <input type="email" class="form-control" name="email" required>
                <div class="form-group">
                    <input type="text" class="form-control" name="subject" required>
                <div class="form-group">
                    <textarea class="form-control" name="message" required></textarea>
                <div class="form-group">
                    <label>Add Attachment:</label>
                    <input type="file" name="attachment">
                <button type="submit" name="send" class="btn btn-primary">Send</button>

Creating a Send Email Code

This contains our code that sends email using PHPMailer, and we’re going to name it as send.php.

// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
    $email = $_POST['email'];
    $subject = $_POST['subject'];
    $message = $_POST['message'];
    $filename = $_FILES['attachment']['name'];
    $location = 'attachment/' . $filename;
    move_uploaded_file($_FILES['attachment']['tmp_name'], $location);
    //Load composer's autoloader
    require 'vendor/autoload.php';
    $mail = new PHPMailer(true);                              // Passing `true` enables exceptions
    try {
        //Server settings
        $mail->isSMTP();                                      // Set mailer to use SMTP
        $mail->Host = '';                       // Specify main and backup SMTP servers
        $mail->SMTPAuth = true;                               // Enable SMTP authentication
        $mail->Username = '';     // Your Email/ Server Email
        $mail->Password = 'mysourcepass';                     // Your Password
        $mail->SMTPOptions = array(
            'ssl' => array(
            'verify_peer' => false,
            'verify_peer_name' => false,
            'allow_self_signed' => true
        $mail->SMTPSecure = 'ssl';                           
        $mail->Port = 465;                                   
        //Send Email
            $mail->addAttachment($location, $filename); 
        $mail->Subject = $subject;
        $mail->Body    = $message;
        $_SESSION['message'] = 'Message has been sent';
    } catch (Exception $e) {
        $_SESSION['message'] = 'Message could not be sent. Mailer Error: '.$mail->ErrorInfo;
    $_SESSION['message'] = 'Please fill up the form first';

Allowing Less Secure Apps

Lastly, in our server email, we need to allow less secure apps by logging in your server email and use this link.

P.S. Don’t forget to create an attachment folder in our app folder. This will be the container for our uploaded attachments. Also, take note of Gmails restrictions in sending emails with attachments.

That ends this tutorial, Happy Coding!

Download Here

Leave a Reply

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