What is PHPMailer and How to Use It?


New member
Article Titles
navigation region
list of 4 items
What is PHPMailer?
Why is PHPMailer needed?
How to use PHPMailer?
Bug investigation
list end
navigation region end
This tutorial contains instructions for using PHPMailer .
What is PHPMailer?
To send an e-mail to an e-mail address, which can be used in software prepared in PHP programming language, with the connection information you specify.
It is a PHP class that communicates with the server and prepares the appropriate environment.
Why is PHPMailer needed?
The mail() function, which is the standard mail sending function of the PHP programming language, prevents spam mailing in all shared hosting services
in order to prevent - disabled. However, this does not mean that mail can not be sent through the server . Alternatively, the SMTP protocol with the server
A mailer script can be used that communicates via the server and authenticates on the server. PHPMailer best meets this need.
How to use PHPMailer?
You can create a contact form and a PHP page to send mail by following the subheadings.
Create an email account
There is no real need for PHPMailer to authenticate to the mail server.
to your email account
needs. First, create an email account and write down the password.
Create a contact form page
Create a file with PHP extension and include the following codes in this file.
<?php header("Content-type: text/html; charset=utf-8"); ?>
<form action="sendmail.php" method="post">
<label for="name">Name:</label><br>
<input type="text" name="name" id="name"><br>
<label for="mail">Mail</label><br>
<input type="text" name="mail" id="mail"><br>
<label for="subject">Subject</label><br>
<input type="text" name="subject" id="subject"><br>
<label for="message">Your Message</label><br>
<textarea name="message" cols="30" rows="10" id="message"></textarea><br><br>
<input type="submit" value="submit">
Create transaction sheet
Create a PHP file with the name sendmail.php and include the following codes in this file.
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'src/Exception.php';
require 'src/PHPMailer.php';
require 'src/SMTP.php';
$mail = new PHPMailer(true);
try {
//Server settings
$mail->CharSet = 'UTF-8';
$mail->SMTPDebug = 0; // debug on - off
$mail->host = 'mail.yourdomain.com'; // SMTP server example: mail.domain.com
$mail->SMTPauth = true; // SMTP Authentication
$mail->Username = 'name@yourdomain.com'; // Email username
$mail->Password = 'Your Password'; // Mail password
$mail->SMTPSecure = 'tls'; // Encryption
$mail->Port = 587; // SMTP Port
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
// Recipients
$mail->setfrom('name@yourdomain.com', 'Contact Form');
$mail->addReplyTo($_POST['mail'], $_POST['name']);
$mail->Subject = 'Contact Form.';
$mail->Body = $_POST['message'];
echo "Your Message Sent --> ".$_POST['mail']."<br>";
} catch (Exception $e) {
echo 'Your message could not be delivered. Error: ', $mail->ErrorInfo;
Edit the transaction page
We write the address of the mail server to the variable in the 6th line. If you are using cPanel or Plesk Panel; Mail.domain.co, if you use Maestropanel
smtp.domain.co my form
You must edit.
Write the email account you created in the variable on line 8.
Type the password of the email account you created in the variable on line 9.
Type the encryption method you want to connect to the variable on line 10 , you can use tls or ssl.
Enter the port number according to the encryption method you have connected to the variable on the 11th line . Type 587 if you are using TLS and 465 if
you are using SSL.
In the variable on line 12, write the email account you wrote on line 8.
Type the name to be displayed in the incoming message to the variable on line 13.
In the variable on line 15, write the email address to which the message will be sent.
Upload PHPMailer files
Download PHPMailer from Github and upload it to the directory where your files are located.
You can download these files from It will be enough to upload the src folder to your site.
Bug investigation
If you are having problems while sending mail, you can activate the error message with the code below . You should write this code before line 14.
$phpmailer->SMTPDebug = 2;