How to use phpmailer in your web application

0
1238
How to use phpmailer in your web application

When a developer wants to create a web application or website, they want to send email to visitors, subscribers or customers. But some problems found in the default functions like mail() which is default function in PHP. That’s why we need to include third-party scripts or framework in our application. Before using a script like phpmailer we have to know that, why we will use other scripts for mail functionality. The first reason is, if we use mail() in our local server it doesn’t work. That’s why we can’t check our application functionalities instant. There some other reasons, but this is the principal reason.

How many scripts are available for email.

There are many scripts is available for email script. You will find many script but SwiftMailer and phpmailer popular script. I will show you how to send email using phpmailer from your local server.

What is PHPMailer

PHPMaliler is the most popular PHP script for sending email from a local or live server. It will help you send emails to your subscribers, visitors or customers using PHP. This script created by some creative guys. This is script is using WordPress. You may know about WordPress by reading What is WordPress and How it Works

How to use PHPMalier

If you want to use PHPMalier, you have to download this script using the composer. If you don’t have an installed composer on your computer, you have to download and install it. It’s not a complicated process. You can install composer like normal windows software. When you have completed the installation of the composer open Command Prompt in your pc which is default software in the Windows operating system. Now go to your application root folder or desired folder using Command Prompt. Now type the following command:

composer require phpmailer/phpmailer

If you have a fast internet connection, it will be download quickly. Wait until all files downloaded.

Once it’s completely downloaded, find out the autoload.php file. Now include in your application file where you have written codes for sending the email. At last, put the following codes in your file where you have written your codes for sending an email.

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';

$mail = new PHPMailer(true);
$mail->SMTPDebug = 0; // Enable verbose debug output
$mail->isSMTP(); // Send using SMTP
$mail->Host       = 'smtp.gmail.com'; // Set the SMTP server to send through
$mail->SMTPAuth   = true; // Enable SMTP authentication
$mail->Username   = 'user@example.com'; // SMTP username
$mail->Password   = 'secret'; // SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;        
$mail->Port       = 587;

$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('joe@example.net', 'Joe User');
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if($mail->send()){
echo 'Message has been sent';
}

Now, change some codes here. You have to change the host if you are using your own hosting account server. If you want to use the Gmail account server don’t change Host. You have to change also the username and password. Give your own email account password and username.

Now you’re all set. When you will reload your file in browser, this code will be sent an email to your desired email address.

If you face any problem, you can comment at below. I will try to help you. Thanks…

Previous articleHow to set up SMTP on Your WordPress Website
Next articleBest Premium WordPress Membership Plugin
Md Dalwar
Md Dalwar is experienced web developer who is working with WordPress and Laravel. He has very helpful mind to help people about any type of thing that he can. Also he provides web related services for peoples. His hobby is writing and coding.