Warning icon

New location coming soon: Madrid, Spain

How to Send Emails Using PHP Mail and PHPMailer

How to Send Emails Using PHP Mail and PHPMailer

Using PHP mail() Function 

Although it may look daunting, using the PHP mail() function is quite easy once you’ve taken time to read about it and understand how it works, particularly if you’re using a Linux server.

The PHP mail() function is an inbuilt function that requires the user to manage the Sendmail service option manually, as the PHP mail() function uses the Sendmail program.

The ability to manage the Sendmail service option manually is determined by the server hosting service and as such, users who wish to use this function should ensure that their server hosting provider allows for it.

While server hosting service providers usually enable the Sendmail service, you should check your server dashboard to be sure. Here’s one reason why having a quality server hosting is important.

When you’re sure that your server is Sendmail service- enabled, you can send emails PHP by first creating a test file for PHP mail script. Create and name a new file by clicking ‘add new’, typing file name and click ‘create’ on your file manage, double click on the created file, edit it using the PHP mail() function, then click “save and close”. You can now send emails PHP by accessing your created file through your domain from your browser.

Some users prefer to send emails PHP through PHPMailer, so let’s take a look at how to send emails PHP through PHPMailer.

Using PHPMailer to Send Emails 

You can send emails PHP through PHPMailer. PHPMailer is a library for sending mails with PHP and is installed to make users send emails PHP much conveniently. The PHPMailer is supported by the STMP and the mail() function that allows it to send emails PHP without hitches. Be sure to install PHPMailer to use the service.

PHPMailer works using the following syntax:

use PHPMailer\PHPMailer\PHPMailer

The syntax above is for importing the PHPMailer class to the global namespace.

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

This is for including libraries the PHPMailer needs.

PHP Mail and PHPMailer Troubleshooting 

Some users may experience some errors with PHPMailer and PHP mail while trying to send emails PHP. Here are some usual errors Linux server users face with PHP mail and PHPMailer and some idea on how to resolve them:

  • When the sender’s address is rejected, it may mean that the server could not verify the details. To resolve this, make sure that your SPF record is enabled and the email address is in sync with an existing mailbox.
  • If your Gmail cannot verify that yourdomain.com sent the message, you should either enable your SPF record or check that the email address the message was sent to is valid
  • If your mail goes to spam, set a clear intent in your message, confirm that the sender address is valid, and check if your mail has an unsubscribe button.


The PHPMailer is a platform that uses STMP to send emails PHP from a Linux server, probably to another Linux server. The PHPMailer is a more convenient way of sending email messages but users need to understand its syntax before truly appreciating its functionality.

The ability to do this would depend a great deal on what features their server hosting service provider provides, although you can always install PHPMailer using the composer.

Linux server users may sometimes experience some troubles using sending emails through PHP mail or PHPMailer. In this article, we have touched on the basics of these areas and have made it easy for you to understand how to use the PHPMailer and also how to deal with some common issues arising from using the PHPMailer.