Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Programming»PHP»Sending Emails using PHP with PHPMailer

    Sending Emails using PHP with PHPMailer

    RahulBy RahulMay 23, 20141 Min ReadUpdated:June 19, 2019

    All the programming languages provide functions for sending emails. PHP also provides mail() function to send emails. But the main point is to increase email delivery to inbox successfully. While sending simple emails the chances of getting email spam increase. Also, a simple PHP mail function has limited options for mail formatting and sending properly. To increase email deliverability and speed we are going to use PHPMailer, which providers lots of option for sending emails. It also provides an option to send email from SMTP servers.

    Step 1 – Setup PHPMailer

    Install the phpmailer module using composer under your application.

    composer require phpmailer/phpmailer
    

    Step 2 – Sending Emails using PHP and PHPMailer

    Now create an simple php script sendMail.php in your web document root and add below content. Below script is using Gmail smtp server for sending mails. You may use any other SMTP server like Amazon SES, Sendgrid, Mailchimp or Mandril App etc.

    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
    <?php
     
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;
    require 'vendor/autoload.php';
     
    $mail = new PHPMailer(true);
    try {
        //Server settings
        $mail->SMTPDebug = 2;
        $mail->isSMTP();
        $mail->Host = 'smtp.gmail.com';
        $mail->SMTPAuth = true;
        $mail->Username = '[email protected]';
        $mail->Password = '_password_';
        $mail->SMTPSecure = 'tls';
        $mail->Port = 587;
     
        $mail->setFrom('[email protected]', 'Admin');
        $mail->addAddress('[email protected]', 'Recipient1');
        $mail->addAddress('[email protected]');
        $mail->addReplyTo('[email protected]', 'noreply');
        $mail->addCC('[email protected]');
        $mail->addBCC('[email protected]');
     
        //Attachments
        $mail->addAttachment('/backup/myfile.tar.gz');
     
        //Content
        $mail->isHTML(true);
        $mail->Subject = 'Test Mail Subject!';
        $mail->Body    = 'This is SMTP Email Test';
     
        $mail->send();
        echo 'Message has been sent';
    } catch (Exception $e) {
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    }

    Now access this application using a browser with your domain name following by script name

      http://example.com/sendMail.php
    
    Email mail PHP phpmailer
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to Setup Node.js with MongoDB on Ubuntu & Debian
    Next Article JavaMail API – Sending Email using Java from Local SMTP

    Related Posts

    How to Install Composer on Ubuntu 22.04

    Updated:June 24, 20223 Mins Read

    How To Install PHP (8.1, 7.4 or 5.6) on Ubuntu 22.04

    Updated:June 19, 20223 Mins Read

    How To Install Linux, Nginx, MySQL, & PHP (LEMP Stack) on Ubuntu 22.04

    Updated:April 7, 20227 Mins Read

    How to Install Apache, MySQL, PHP (LAMP Stack) on Ubuntu 22.04

    Updated:June 28, 20225 Mins Read

    How To Setup Apache, PHP & MongoDB in Ubuntu & Debian

    Updated:October 8, 20213 Mins Read

    How To Install and Use PHP Composer on Debian 11

    Updated:February 16, 20224 Mins Read

    9 Comments

    1. nagaraj on July 5, 2020 3:35 pm

      I am also receiving the same blank screen when i run the sendMail.php. The apache error log entry is
      “GET /sendMail.php HTTP/1.1” 500 185 “-” “Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0”

      Reply
    2. Steve on June 18, 2019 5:53 pm

      Simple instruction with good example code. Sending attachments is now easy. Thanks you!

      Reply
    3. Oliver Russell on December 5, 2018 12:18 pm

      It is also possible to use PHPMailer to send emails from local web server. For this you will have to define following variables

      $mail->From
      $mail->FromName
      $mail->addAddress
      $mail->isHTML(true)
      $mail->Subject
      $mail->Body
      $mail->AltBody

      Source: How to use PHPmailer

      Reply
    4. Adi Kwok on July 19, 2018 2:47 pm

      this line writen in my page when i go to
      http://mydomain/sendmail.php

      ” b0nano 2.5.3xvrootvultr.ubuntusendmail.php ”

      with no mail sent to mailbox i input in the sendmail.php

      so. whats next and thank you. sir

      Reply
    5. Nathaviê on June 1, 2018 10:45 pm

      Why I need this myfile.tar.gz? It’s making and error. I suppose it is because my server is Windows, but I don’t know what to do anyway..

      “Could not access file: /backup/myfile.tar.gz
      A mensagem não pode ser enviada.Mailer Error: Could not access file: /backup/myfile.tar.gz”

      Reply
    6. Amit on June 7, 2016 8:31 am

      Hi Pachak,

      I have setup php 7 and try to use phpmailer 5.2 version. But its provide following error :
      Connection: opening to host(this is my email host):465
      Mailer Error: SMTP connect() failed.

      I have used 465,25 and 587.

      Is phpmailer 5.2 not compatible with PHP 7?

      Looking for your help.

      Best,
      Amit

      Reply
      • Stijn Smits on October 18, 2016 3:28 pm

        Same for me

        Reply
    7. Pachak on December 15, 2015 8:01 am

      I tried with your code, but it returned blank page. It didn’t show “Message sent” nor any errors.

      Reply
      • Rahul on December 15, 2015 12:37 pm

        Hi,

        Is there any error in Apache logs ?

        Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • What is the /etc/aliases file
    • What is the /etc/nsswitch.conf file in Linux
    • How to Install Ionic Framework on Ubuntu 22.04
    • What is the /etc/hosts file in Linux
    • How to Install Angular CLI on Ubuntu 22.04
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.