How To Send Mail To Another With PhpMailer In Php?

Send Mail With PhpMailer is one of the best option to send mail to another person using this library.

In this post we discuss about PhpMailer Like Use of PhpMailer,Integration etc…

Features Of PhpMailer :

  • Probably the world’s most popular code for sending email from PHP
  • Used by many open-source projects: WordPress, Drupal, 1CRM, SugarCRM, Yii, Joomla! and many more
  • Integrated SMTP support – send without a local mail server
  • Send emails with multiple To, CC, BCC and Reply-to addresses
  • Multipart/alternative emails for mail clients that do not read HTML email
  • Add attachments, including inline
  • Support for UTF-8 content and 8bit, base64, binary, and quoted-printable encodings
  • SMTP authentication with LOGIN, PLAIN, CRAM-MD5, and XOAUTH2 mechanisms over SMTPS and SMTP+STARTTLS transports
  • Validates email addresses automatically
  • Protects against header injection attacks
  • Error messages in over 50 languages!
  • DKIM and S/MIME signing support
  • Compatible with PHP 5.5 and later, including PHP 8.1
  • Namespaced to prevent name clashes
  • Much more!

2 Way To Integrate PhpMailer :

Step 1 : You can integrate PhpMailer Using Composer.In Command Prompt Enter Below Line & Run This.

composer require phpmailer/phpmailer

Step 2 : You can download the library & use this.For download the PhpMailer Library Click Here

Important :

For run the PhpMailer add below line in the code.without this line PhpMailer library can’t work.

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

require 'path/to/PHPMailer/src/Exception.php';
require 'path/to/PHPMailer/src/PHPMailer.php';
require 'path/to/PHPMailer/src/SMTP.php';

If you’re not using the SMTP class explicitly (you’re probably not), you don’t need a use line for the SMTP class. Even if you’re not using exceptions, you do still need to load the Exception class as it is used internally.

Send Mail With PhpMailer Example :

//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\SMTP;
use PHPMailer\PHPMailer\Exception;

//Load Composer's autoloader
require 'vendor/autoload.php';

//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);

try {
    //Server settings
    $mail->isSMTP();                                            //Send using SMTP
    $mail->Host       = '';                     //Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                   //Enable SMTP authentication
    $mail->Username   = '';                     //SMTP username
    $mail->Password   = 'EnterYorE-MailPassword';               //SMTP password
    $mail->SMTPSecure = 'ssl';            
    $mail->Port       = 465;              //use 587 if you have tls encryption

    $mail->setFrom('', 'Mailer');
    $mail->addAddress('', 'Joe User');     //Add a recipient
    $mail->addAddress('');               //Name is optional
    $mail->addReplyTo('', 'Information');

    $mail->isHTML(true);                                  //Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';

    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
Add Attachment In Mail :

Add below line in code for attach image or doc file in mail.

    $mail->addAttachment('/var/tmp/file.tar.gz');         //Add attachments
    $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    //Optional name

Recent Posts

Leave a Reply

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