How to Use PHP Mail and PHPMailer: Complete Guide

Email is an integral part of any project or business. While there are numerous business email platforms, such as , and , you can also send email using PHP. In this tutorial, we will learn how to send emails using the built-in PHP mail() function and the PHPMailer with the Simple Mail Transfer Protocol ().

Use the PHP mail() function

The function mail() invoke a program Sendmailusually configured by the system administrator, which allows you to send emails.

To use this feature, make sure your hosting provider allows you to manually manage the host option. Sendmail service.

If you are already using , you can enable or disable this feature by accessing your hPanel. click on Emails-> Control of the mail service.

The sendmail service is enabled by default. But in case it isn’t, you can do it with just a couple of clicks.

Creating a test file for PHP mail

First of all, you need to create a file for the PHP mail script and place it in the directory public_html so that it can be easily accessed through the domain name.

  1. To access the hPanel, click on the File manager.
  2. Create a new file by clicking add new. In this example we call it testmail.php and then press To create.
  3. double click on testmail.php since you will need to edit it using the PHP function mail(). Here is an example of the basic components of the function that we will use in this tutorial. These will be explained in more detail in the next section. For now, click save and close when you’re done editing.
  4. Now, you can send an email by accessing YourDomain/testmail.php from the browser. Remember you must change YourDomain to the domain you used to create testmail.php.
  5. Your target email will receive the message you sent. Your recipient’s inbox will receive the test email.

Understanding PHP Mail Components

As mentioned before, we will introduce you to the components of a basic PHP mail script. For this example, we’ve used basic email syntax to help you understand this feature in more detail.

See also  When does a StartUp stop being a StartUp?

However, if you need more information about the function Sendmail and its components, you can consult the.

Here is the PHP syntax we used in the previous section:

Let’s break it down step by step:

ini_set( ‘display_errors’, 1 ); error_reporting( E_ALL );

These first two lines allow error notification: they will inform you if the script does not run.

$from = “test@-tutorials.com”;

This line must contain the sender’s email address. Most hosting providers do not allow random email addresses here as it can be used for spoofing. Use one created for your domain name or brand to run PHP mail successfully.

$to = “test@gmail.com”;

This is where you insert your recipient’s email address.

$subject = “Checking PHP mail”;

Enter the subject of your email message on this line.

$message = “PHP mail works just fine”;

Here you can write your message.

$headers = “From:” . $from;

Detail vital information like sender address, reply location, etc.

mail ($to,$subject,$message,$headers);

This line is used to execute the function.

echo “The email message was sent.”;

The message that will appear once the script runs successfully.

Use PHPMailer to send mail

is a popular mailing library for PHP. It supports sending mail through the function mail() either Simple Mail Transfer Protocol (). This library simplifies the complicated process of creating a PHP email by providing a set of functions to create and send an email.

Installing PHPMailer is quite simple, especially if you have it installed. If you are using , you don’t need to worry about this as it comes pre-installed with all our hosting plans.

However, if you need to install PHPMailer manually, you must connect your hosting account through the . Follow these steps:

  1. Download and install the PuTTY SSH client.
  2. Go to your hPanel dashboard, find and click SSH access in the section Advanced.
  3. Take note of the SSH access information. you will need the SSH-IP address, port, username Y password.
  4. Open PuTTY, then configure your SSH-IP and the corresponding port.
  5. press Open and a command window will appear. write your Username Y ssh-password. Once done, press Enter.
  6. NOTE: putty NO will show your password. Do not be confused if your password does not appear on the screen.

  7. Run the following command: cd public_html
  8. Hit Enter, then run this command:

    composer require phpmailer/phpmailer
  9. Wait a moment for the installation process to finish.

See also  How John grew his Minecraft business with

Using PHPMailer with SMTP

Once you have PHPMailer ready, you can use it to send PHP mail using SMTP.

  1. Create an email account by accessing your hPanel, then go to Email account -> Create a new email account. Please fill in the new email address and set a password before clicking To create.
  2. When you’re done, pay attention to your SMTP data located in the same section:

    NOTE: You must remember the email account usernamethe account password, SMTP Host Y SMTP port to send an email using PHPMailer.

  3. create a file testphpmailer.php in public_html. Access the hPanel control panel and click on file manager.
  4. press add new. Name the file as testphpmailer.php and click To create.
  5. Double click on the file testphpmailer.php newly created, then copy and paste the following: isSMTP(); $mail->SMTPDebug = 2; $mail->Host=”smtp..com”; $mail->Port = 587; $mail->SMTPAuth = true; $mail->Username=”test@-tutorials.com”; $mail->Password = ‘YOUR PASSWORD HERE’; $mail->setFrom(‘test@-tutorials.com’, ‘Your Name’); $mail->addReplyTo(‘test@-tutorials.com’, ‘Your Name’); $mail->addAddress(‘example@email.com’, ‘Receiver Name’); $mail->Subject=”PHPMailer Testing”; $mail->msgHTML(file_get_contents(‘message.html’), __DIR__); $mail->Body = ‘This is a plain text message body’; //$mail->addAttachment(‘test.txt’); if (!$mail->send()) { echo ‘Mailer Error: ‘ . $mail->ErrorInfo; } else { echo ‘The email message was sent.’; } ?>
  6. Modify the above code as appropriate. For example, you should replace EMAIL_ACCOUNT_PASSWORD by your email password or test@-tutorials.com with your username, example@gmail.com with the your recipient’s email address, etc. Once done, click save and close.
  7. Your PHPMailer is now ready to be used. Run the script by entering YourDomain.com/testphpmailer.php in your browser.

Understanding PHPMailer Components

To understand how PHPMailer works, let’s investigate the example script above that uses SMTP for email delivery. Here is the explanation of each component:

use PHPMailer\PHPMailer\PHPMailer;

This line imports the PHPMailer class into the global namespace.

require ‘../vendor/autoload.php’;

It includes several libraries that PHPMailer needs.

$mail->

All of these variables contain vital information such as server details, headers, messages, attachments, and more. In short, they make sure the sender is legitimate with SMTP authentication.

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

Defines what happens when the script is executed.

echo ‘MailerError: ‘ . $mail->ErrorInfo;

Will display an error message with an explanation when the script fails to submit.

} else {

Defines what happens if the script runs.

echo ‘The email message was sent!’;

If the email is sent successfully, this message will appear.

professional tip: the line SMTPDebug=2; it’s only useful when you’re testing a script and want to see how it works. You must change it to SMTPDebug = 0; if you already finished the test. This is done to prevent the end user from seeing the SMTP delivery report.

If you went through all the code, you’ll notice that we’re doing something a little different than the first example, we’re sending an HTML message instead of a piece of text.

So your message will load your content from the file message.html located in the same directory: public_html.

This format offers more functionality compared to plain text messages as HTML is highly customizable. You can change the color, style, image, or even include media files that are usually outdated in a plain text email.

PHPMailer Contact Form

You can use PHPMailer for much more than just sending simple messages through PHP. One way to use it is to create a contact form where your visitors or users can get in touch with you.

Here is a basic example of such a script:

isSMTP(); $mail->Host=”smtp..com”; $mail->Port = 587; $mail->SMTPAuth = true; $mail->Username=”test@-tutorials.com”; $mail->Password = ‘EMAIL_ACCOUNT_PASSWORD’; $mail->setFrom(‘test@-tutorials.com’, ‘Mr. Drago’); $mail->addAddress(‘example@gmail.com’, ‘Receiver Name’); if ($mail->addReplyTo($_POST, $_POST)) { $mail->Subject=”PHPMailer contact form”; $mail->isHTML(false); $mail->Body = <<send()) { $msg = ‘Sorry, something went wrong. Please try again later.’; } else { $msg = ‘Message sent! Thanks for contacting us.’; } } else { $msg = ‘Share it with us!’; } ?> Contact form

Do You Have Anything in Mind?

$msg

“; } ?>

< label for="email">Email: