Email is very important for any website. You can use email to interact with users for a number of reasons. You can confirm them for certain promotions, encourage them to return to the site by promoting some great offers, notify them by sending them new updates, information, etc.

Sending email via script is the most commonly used function in web applications. Generally, the PHP mail() function is used to send emails from a PHP script. When you send an email using the mail() function in PHP, the email is sent from your web server. Sometimes there is a problem in sending the email and the email cannot be delivered to the recipient. You can use SMTP to work around this problem. SMTP is the most recommended method for sending emails from PHP scripts. When you send an email using SMTP, the email is sent from the email server, not the web hosting server.

How to use PHPMailer SMTP Gmail with Example

The easiest way to send PHP emails using SMTP is to use the PHPMailer library. PHPMailer offers the possibility to send emails with PHP via an SMTP server. The various configuration options of the PHPMailer library allow you to configure and customize the email functionality according to your needs. You can send text or HTML emails with one or more attachments using PHPMailer. PHPMailer is probably the most popular open-source PHP library for sending emails. It was first released in 2001 and has since become the method of choice for PHP developers to send email programmatically, with the exception of a few other fan favorites such as Swiftmailer.

Why use the PHPMailer library?

Because the mail function in PHP does not provide any attachments. Some other functions like authentication, encryption, or HTML messages are very well developed in PHPMailer. It has a built-in SMTP so you don’t need to use a local mail server. This is useful for platforms like Pantheon that don’t support local mail servers. Or you can use a library like ZetaComponents, SwiftMailer, or Laminas/Mail. They are all really great options for sending emails securely.

This article outlines the steps for using PHP Mailer to send an email.

Installation

The best way to install PHPMailer is to use composer. Before proceeding, install the composer.

  • Open a command prompt and change to the project directory where you want to use PHPMailer.
  • Run the following command: Composer requires phpmailer / phpmailer
  • Wait until the installation is complete. This will download all the required classes into your project folder.

Using PHPMailer with Hostinger SMTP

After installing PHPMailer, you can start sending PHP emails.

In this section we’ll show you how to use PHPMailer to send email via Hostinger’s SMTP server:

  • Create an email account by logging into CPanel, then go to Email -> Email Accounts -> Create New Email Account.
  • Enter a new email address and set a password. Then click Create. Be sure to remember this information as you will be using it to send email via PHPMailer.
  • On the same page, go to Configuration Settings -> Manual Configuration and consider the SMTP host and port.
  • Go to the CPanel control panel and go to File -> File Manager. Click the public_html folder and select Add New to create a new file. Name the file testphpmailer.php and click Create.
  • Double click the testphpmailer.php file, copy and paste the following code and modify it as needed. Be sure to replace test@hostinger-tutorials.com with your Hostinger email address and EMAIL_ACCOUNT_PASSWORD with the password.
  • After editing the code, click Save & Close. To run the script, type YourDomain.com/testphpmailer.php into your browser.

Using PHPMailer:

Import the PHPMailer class into the global namespace.

Note: Make sure these lines are at the beginning of the script, not in every function.

use PHPMailer\PHPMailer\PHPMailer;

use PHPMailer\PHPMailer\Exception;

 

Load the composer’s autoloader.

 

require ‘vendor/autoload.php’;

 

Create a PHPMailer class object.

 

$mail = PHPMailer()

 

Create a PHPMailer contact form

Apart from using PHPMailer to send simple PHP emails, users can also create contact forms that audiences can use to connect with them. As with the previous PHP scripts, it is important to create a new PHP file in the public_html folder before proceeding. Name it formscript.php. Then copy and paste the following script into the newly created file and change the information in it:

<?php

use PHPMailer\PHPMailer\PHPMailer;

require ‘vendor/autoload.php’;

$mail = new PHPMailer;

$mail->isSMTP();

$mail->Host = ‘smtp.hostinger.com’;

$mail->Port = 587;

$mail->SMTPAuth = true;

$mail->Username = ‘test@hostinger-tutorials.com’;

$mail->Password = ‘EMAIL_ACCOUNT_PASSWORD’;

$mail->setFrom(‘test@hostinger-tutorials.com’, ‘Mr. Drago’);

$mail->addAddress(‘example@gmail.com’, ‘Receiver Name’);

if ($mail->addReplyTo($_POST[’email’], $_POST[‘name’])) {

$mail->Subject = ‘PHPMailer contact form’;

$mail->isHTML(false);

$mail->Body = <<<EOT

Email: {$_POST[’email’]}

Name: {$_POST[‘name’]}

Message: {$_POST[‘message’]}

EOT;

if (!$mail->send()) {

$msg = ‘Sorry, something went wrong. Please try again later.’;

} else {

$msg = ‘Message sent! Thanks for contacting us.’;

}

} else {

$msg = ‘Share it with us!’;

}

?>

<!DOCTYPE html>

<html lang=”en”>

<head>

<meta charset=”UTF-8″>

<title>Contact form</title>

</head>

<body>

<h1>Do You Have Anything in Mind?</h1>

<?php if (!empty($msg)) {

echo “<h2>$msg</h2>”;

} ?>

<form method=”POST”>

<label for=”name”>Name: <input type=”text” name=”name” id=”name”></label><br><br>

<label for=”email”>Email: <input type=”email” name=”email” id=”email”></label><br><br>

<label for=”message”>Message: <textarea name=”message” id=”message” rows=”8″ cols=”20″></textarea></label><br><br>

<input type=”submit” value=”Send”>

</form>

</body>

</html>

Save your changes, then run the script in your browser.

If a visitor sends a message via the form, he or she receives a confirmation message and the form’s contents end up in the inbox of the email address entered here:

$mail->addAddress(‘example@gmail.com’, ‘Recipient Name’);

For more examples of using this library to send an email, check out the official PHPMailer repository on GitHub. If you’re a WordPress user, you can use a plugin like Formidable Forms, Gravity Forms, or WPForms to create a contact form.