What is PhpMailer??

Phpmailer, written in the PHP programming language, is a PHP library that users use to send mail to their email addresses without the need for any mail server.

Why should we use PhpMailer?

The PhpMailer library was developed in 2001 because the standard mail function (mail()) in the PHP programming language could not fully meet the needs of the user.

Because the Mail feature sends mail without using any authentication, the mail sent is forwarded as spam. To avoid this situation, PhpMailer, which can authenticate via SMTP on the server, gives the user a huge advantage. In this way, you can securely use the function of sending e-mail to one or more users from the e-mail address you have through your website.

Although PhpMailer provides an object-oriented interface, the mail() function is not object-oriented.

In addition to the domain extension email address you get from the host address, it also offers the ability to send messages from email addresses such as Hotmail, Gmail, and Yandex.

PhpMailer is also used in ready-made website builder sites.

How to use PhpMailer? Installation and Mailing Operations

To use PHPMailer, a mail account registered on a mail server supported by SMTP is required. This mail server can be a well-known mail server such as gmail, hotmail, yandex, or your site's own mail server.

First, I will show you the SMTP settings of several mail servers as examples.

1.Gmail SMTP Settings

SMTP Host = smtp.gmail.com

SMTP Port = 587/465

SMTP Secure(Privacy) = tls/ssl

2.Hotmail SMTP Settings

SMTP Host = smtp.live.com

SMTP Port = 587

SMTP Secure(Privacy) = tls

3.Yandex SMTP Settings

SMTP Host = smtp.yandex.com

SMTP Port = 587/465

SMTP Secure(Privacy) = tls/ssl

Custom SMTP Supported Server Settings

The hosting company you buy hosting and domain service from has an email service. You can send email using this email information.

How to install PhpMailer?

The files needed for installation can be found here. In the files you download:

  • This file in php:src folder is very important for sending mail. Include it in your project.
  • php : This file in the src folder is used to display any errors that will occur in the project.
  • language: The files in this folder allow you to select the language in which errors will be displayed with Exception.php.

First, let's add our files to the project as shown below.

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

use 'PHPMailer/PHPMailer/PHPMailer';
use 'PHPMailer/PHPMailer/Exception';


Now let's turn to our PhpMailer class.

$mail = new PHPMailer(true);


Let's apply the SMTP settings of the mail servers as explained above. The fields in this section should include the server information of the person who sent the email.

$mail->Host = 'mail.mailserver.com';                          //İstifadə olunacaq host
$mail->Username = '[email protected]';                     //Göndərənin mail adresi
$mail->Password = 'mail_password';                           //Göndərənin mail şifrəsi
$mail->SMTPSecure = 'tls';                                          //Tls aktiv edilmesi
$mail->Port = 'port';                                                    //Göndərilən mail portu


Then we say that we will send mail with SMTP and that there is an account verification process.

$mail->SMTPAuth = true;


If a message we send as email contains HTML tags, they are sent as plain text. To avoid this, we enable it by applying the following code.



We add the following code to avoid character error that may occur during sending mail.

$mail->CharSet = 'UTF-8';


Now, let's add the code to the language folder that will show the errors in Azerbaijani, and let's add the code that will allow us to see the errors that may occur when sending mail and the sending process in more detail.

$mail->setLanguage( 'az' , 'language/' );
$mail->SMTPDebug = 2;

Other values that the SMTPDebug Property can take are:

  • "1" is written to indicate a user-side process.
    "2" is written to indicate both user and server side process.
    "3" is written to show the user, server and connection process.

We have identified the basic information needed to send mail. Now let's look at the email sending and content part.

$mail -> setFrom( '[email protected]' , 'kananmirza.com' ); 

/* Birinci parametr olaraq mail göndərənin mail adressi, ikinci parametr olaraq başlıq hissə yazılır; */


Then, with this function, we record the email address of the person who will receive the email. It can be used several times.

$mail->addAddress( '[email protected]' );


In the next section, we define the content and subject of the email to be sent.

$mail->Subject = 'Göndərilən mail haqqında';
$mail->Body = 'Göndərilən mail məzmunu';


We may want to send a file inside the email we want to send. For this we need to use the following function.

$mail->addAttachment( 'sekil/sekil_yolu.jpg' , 'Şəklin adı' )

/* Burada birinci parametr olaraq şəklin yolunu daxil edirik. İkinci parametr olaraq isə şəkli göstərmək istədəyimiz başlığını yazırığ.
Əgər ikinci parametr yazmazsaq şəkil original adı ilə göndəriləcəkdir. */


The mail is ready to be sent. Mail is sent with the following function.

$mail -> send();


If an error occurs while sending the mail, the following function is used to print the error.

echo $mail -> ErrorInfo;

If you want to read more articles about programming, you can click here.

Kanan Mirzayev
Full Stack Web Developer

The best way to get to know a programmer is to look at the code they write. You can provide a link to my Github account.

Write a comment

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

1 Comment