How to Send Emails Using PHP Mail and PHPMailer?

You may use PHP mail to send emails if your website or web application is integrated inside PHP. For example, it might be helpful to create custom mail forms and send text-based emails. Using a built-in PHP mail() feature or a mailing library such as PHPMailer, there are usually two methods to send emails using PHP. In this article, we will discuss how to send emails using both ways to detect and repair common PHP mail problems.

Sending Emails Using PHP mail()

You use the built-in mail() function to send emails straight from a PHP script.

  • To utilize the PHP sent function, you must set up a Sendmail program by modifying the php.ini file in your PHP installation folder to host your PHP application or site on a local server.
  • Usually, Sendmail is already configured when you utilize a hosting server. However, you need to ensure that your hosting provider manually manages the Sendmail service option
  • Hosting users may switch this function through access to the CPanel and emails and then Mail service monitoring.
  • Sendmail is already on by default. Double-check, though, for sure.

Create a PHP Mail Test File

We’ll build a PHP mail script file and put it in the public HTML directory once we have ensured that Sendmail is functioning.

How can I do it?

  • To get Hostinger File Manager, browse from CPanel to Files and then File Manager.
  • Choose the New File icon on the top bar and double-click the public HTML folder. Name the testmail.php new file, then click Create.
  • Click testmail.php to modify it double-click. For example, you can use the following simple PHP code, but make sure to modify its arguments.
  • Click Save & Close after you finish editing.
  • Send the Email from your browser by using YourDomain/testmail.php. Note that while generating testmail.php, you replace your domain with the domain used.

HTML emails to be sent in PHP

Also for sending HTML-formatted emails can utilise the PHP mail() function. Compared to simple text messages, this format may be greatly customized. The method for sending HTML mail is the same, but an HTML message and other parameter headers must be included this time.

How to Send Emails using PHPMailer

It is advised that you use an external PHP mailing package if you wish to send several messages. The PHP mail() function does not work for vast quantities of emails, as it creates and closes a socket connection with each Email to a Simple Mail Transfer Protocol (SMTP).

Many PHP mail programs, like the Pear Mail and Swift Mailer, are available. We’re going to utilize PHPMailer in this post. PHPMailer is a popular mail-sending library that supports mail-sending using mail() or SMTP. It provides access to several email sending methods that ease the configuration process of PHP mail.

Installation of PHPMailer

PHPMailer installation is relatively easy, particularly with Composer installed; this utility is included in most shared hosting plans. Connect your hosting account to the SSH terminal manually to install PHPMailer by following the following steps:

  • The PuTTY SSH client is downloaded and installed.
  • Take note of the SSH IP, port, username, and password under the SSH Access information from your CPanel dashboard, under Advanced -> SSH Access.
  • Open puTTY in the Host Name (or IP address) and Port slots and enter your SSH information. Click Open.
  • Only enter in your SSH name and password and click Enter once a command window is displayed. Note that PuTTY will not show your password, so be not puzzled when it is not displayed on your screen.
  • Press Enter to execute the cd public HTML command.
  • Then execute the phpmailer and click Enter command.
  • Wait till the installation is complete for a time.

Use of PHPMailer to Hostinger SMTP

You will be able to start sending PHP emails when PHPMailer has been installed. In this article, you will find a way of sending emails through PHPMailer over the Hostinger SMTP server:

  • Creating a CPanel email account, then visit Email> Email Accounts > New E-mail Account Create.
  • Complete and enter a new email address. Click Create then. Keep in mind this information as you will send emails using PHPMailer with this information.
  • Go to Settings -> Manual Sets-Up on the same page, and notice the SMTP Host and Port. Please note it.
  • Go into the files->file manager and browse to the CPanel dashboard. To create a new file, click on the folder publish HTML and select Add New. Next, click the Create button on the file testphpmailer.php.
  • Double-click testphpmailer.php, then copy, paste and amend the code below. Make sure that the Hostinger email address and EMAIL ACCOUNT PASSWORD are used to replace
  • Click Save & Close after altering the code. Then, enter in the browser to execute the script.

Create a contact form for PHPMailer

Aside from utilizing PHPMailer, users can also use it to construct a contact form so that their audience may contact them. As with the previous PHP scripts, it’s essential before you start to create a new PHP file in the public HTML subdirectory. Name it.

Ordinary PHP Mail and PHPMailer Errors can be resolved?

We’ll discuss the most frequent problems in the following sections when using the PHP mail() or PHPMailer function and how they may be fixed.

  • Rejected Sender Address: Not owned by the user. This mistake implies the server could not authenticate the sender using the data given. See the email address you used to send the message to correct it and make sure it meets an existent email address. Change it if it points to an incorrect address.
  • Gmail cannot verify the sending of this message from When testing a PHP mail script, you might encounter this warning, one of the reasons can be:
  1. In the DNS zone of the domain, there is no SPF record. If there are no record or external name servers, manually add the new SPF TXT record to your CPanel or cPanel.
  2. The SMTP authentication information was invalid. Make sure you use your existing and your email address.


The PHP mail() method is ideal for transmitting essential text messages in modest quantities. On the other hand, PHPMailer is a better way to send mass emails or create a contact form.

Leave a Comment