How to send Email using PHPMailer library in PHP

#32
331
1 Answer
Question Image

I want to send an email using PHPMailer library in PHP, so please tell me how it possible?


Related to:
phpphpmailersmtplibraryemail
0
Answer
Answer 1 of 1
# 20

Sending email from your own website using PHPMailer is easy, just follow these steps:

PHPMailer

PHPMailer is a code library used for sending email from a web server, There are many PHP libraries for sending mail, of which PHP Mailer is the most popular.


Installing PHPMailer Library

  • Install PHPMailer in your project folder where you want to use its code using composer or vsCode using following code command:
    composer require phpmailer/phpmailer
    or download the PHPMailer library from gitHub as zip file and extract to your targeted folder.


Send email using phpmailer

After installing the PHPMailer Library, the next step is to create a PHP file for sending emails, in which some necessary information about sending emails and security measures will be attached.


Creat phpmailer code file

  • Create a php file and start php tag, like this:
    <?php

  • Import the PHPMailer classes at the top of your script, not inside a function:
    use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\SMTP; use PHPMailer\PHPMailer\Exception;

  • Add the necessary PHP library files to call the function:

    • If you installed PHP Mailer manually, add these files:
      require 'path/to/PHPMailer/src/PHPMailer.php'; require 'path/to/PHPMailer/src/SMTP.php'; require 'path/to/PHPMailer/src/Exception.php';

    • If you used Composer for installation, add the autoload.php file generated by Composer: require 'path/to/vendor/autoload.php';

    • If you are not using the SMTP class, you do not need to use the line for the SMTP class.

    • Conversely, if you are not using exceptions, you still need to load the Exception class because it is used internally

  • Then create a new PHPMailer object:
    $mail = new PHPMailer();


SMTPDebug

  • Use a SMTP Debug:

    SMTPDebug: Enables debug output, use to display information about problems with connectivity and sending email. It has the following and some other values:

    • 0: This is the default value, it is used to disable debugging, even if you don't use it, debugging will be disabled. It is a good practice to specify this if you want to keep debugging disabled:
      $mail->SMTPDebug = 0;

    • 1: Display the output email sent by the client:
      $mail->SMTPDebug = 1;

    • 2: is used to display the value of step 1 plus the response received from the server:

      $mail->SMTPDebug = 2;

    • 3: Used to display the value of step 2 plus information about the initial connection, this level can help diagnose a STARTTLS failure:
      $mail->SMTPDebug = 3;

    • 4: is used to display the value of step 3 as well as possible lower level information.

      $mail->SMTPDebug = 4;

    • Other SMTP related debug such as:
      $mail->SMTPDebug = SMTP::DEBUG_SERVER;


SMTP Authentication

  • Add an SMTP configuration if you use SMTP Authentication:

    • isSMTP(): used to Specify that you are sending mail using SMTP:
      $mail->isSMTP();

    • Host: used to set the SMTP server through which you want to send mail:
      $mail->Host = 'smtp.example.com';

      • If you have set Gmail's server to send mail, the code will be written something like this smtp.gmail.com

      • If you are using the website's email server, you can see it in the mail section of the website. You can see something like this mail.yourdomain.com

    • SMTOAuth: used for enable SMTP authentication:
      $mail->SMTPAuth = true; 

    • Username: SMTP username:
      $mail->Username = 'user@example.com';

      SMTP username refers to your email ID with which you are trying to do SMTP authentication.

    • Password: SMTP password:
      $mail->Password = 'secret';

      SMTP password refers to the password of your email ID with which you are trying to do SMTP authentication.

    • SMTPSecure: used to specify the encryption technique, accepted values are tls ssl or other:
      $mail->SMTPSecure = 'tls';
      Or
      $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;

    • Port: This specifies the TCP port to which you want to connect and send email:
      $mail->Port = 465;

      Check the port number in the "Mail Configuration Details" mentioned in the mail section of your website.


Email sender and recipients

  • Add a email sender and recipients email

    • setFrom(): add sender email and name:
      $mail->setFrom('info@domainname.com', 'Name');

    • addAddress(): add recipient email:
      $mail->addAddress('receiver@example.com', 'recipient name');  

      Recipient name is optional.

    • Add the next recipient, if any:
      $mail->addAddress('receiver@example.com', 'recipient name');  

    • addReplyTo(): Add a reply email so that the email recipient can reply using this.
      $mail->addReplyTo('info@domainname.com', 'sender name or any other information');

    • addCC, addBCC You can use CC and BCC:
      $mail->addCC('cc@example.com', 'Name');
      $mail->addBCC('bcc@example.com', 'Name');


Add the email content

  • Add the email content you want to send to the recipient:

    • isHTML(): Using this you can set the email format to HTML:
      $mail->isHTML(true);

    • Subject: Enter the subject of the mail:
      $mail->Subject = 'enter mail subject here';

    • Body: add email content you want to send:

      • You can add mail content in HTML format here:
        $mail->Body = 'enter mail content in HTML format';

      • And you can also add HTML content using a variable:
        $mailBody = '<h1>This is my first mail</h1><p>Mail content.</p>';
        $mail->Body = $mailBody;

    • AltBody: You can send mail using plain text, it is usable if recipient's mobile doesn't support HTML:
      $mail->AltBody = 'Mail content in plain text for non-HTML recipient';

    • send(): using this send email:
      $mail->send();

Finally your mail will be sent.


PHPMailer Code Template

A complete PHP code to send email using PHPMailer, In this code the sender used a Gmail for SMTP authentication:

Copy and past this code to your project

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
    $mail->SMTPDebug = 3;
    $mail->isSMTP();
    $mail->Host     = 'smtp.gmail.com;';
    $mail->SMTPAuth = true;
    $mail->Username = 'sendermail@gmail.com';
    $mail->Password = 'sender mail password';
    $mail->SMTPSecure = 'tls';
    $mail->Port  = 587;
    $mail->setFrom('sendermail@gmail.com', 'Name');    
    $mail->addAddress('receiver1@gmail.com');
    $mail->addAddress('receiver2@gmail.com', 'Name');
    $mail->isHTML(true);
    $mail->Subject = 'Subject';
    $mail->Body = 'email content in HTML format';
    $mail->AltBody = 'email content in plain text for non-HTML mail clients';
    $mail->send();
    echo "Mail has been sent successfully!";
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>
0
0
0
Related Articles

If you still have a question about this, submit it in our Q&A community - Ask Question