Knowledge Base
 Up to 70% off  on  WordPress  hosting for WordPress Websites and Stores!

Sending PHP Email through SMTP

Whenever mail is sent using a simple PHP mail function, the email envelope is user@serverhostname. If the server hostname doesn't have a TXT record, which includes an outbound/gateway, then the emails from these servers are rejected by Gmail. This can cause problems if the FROM address is not configured correctly or your email is not hosted by Bluehost. To solve this problem, you need to send PHP emails through SMTP authentication.

You can only use the option if you're creating a custom-built mail form. If you're using software like WordPress, use the built-in tools or plugins to send via SMTP.

Securely Send PHP Email Through SMTP with Authentication

Setting up PEAR to secure email sending

The steps below will walk you through how to enable PEAR:

  1. To install PEAR on your web server, check out the PEAR article for step-by-step instructions.
  2. Install the PEAR Mail packages that are required. You'll need the Mail and Net SMTP packages. For example:
    [server]$ pear install --alldeps Mail
  3. Create a PHP file that sends email via SMTP server using PEAR.

Setting up PHP to send SMTP mail

The following example demonstrates how to send SMTP mail using PEAR Mail.

  1. Create a file similar to phpmail.php with the code below. For step-by-step instructions on how to create/edit files using File Manager or FTP, see the articles File Manager - How to Create, Remove, and Edit Files and Intro to FTP.
    error_reporting (E_ALL ^ E_NOTICE ^ E_DEPRECATED ^ E_STRICT);
    require_once "Mail.php";
    #host = "ssl://smtp.bluehost.com";
    $username = "[email protected]";
    $password = "your email password",
    $port = "465";
    $to = "[email protected]";
    $email_from = "[email protected]:;
    $email_subject = "Subject Line Here:" ;
    $email_body = "write your body content here: ;
    $email_address = "[email protected]" ;
    $headers = array ('From' => $email_from, 'To' => $to, 'Subject' => $email_subect, 'Reply-To' =>
    $smtp = Mail::factory ('smtp', array ('host' => $host, 'port' => $port, 'auth' => true, 'username'
    $mail = $smtp->send($to, $headers, $email_body);
    if (PEAR::isError($mail) )  {
    echo ("<p>" . $mail->getMessage () . "</p>") ; 
    } else {
    echo ("<p>Message successfully sent!</p>") ;

    Mail.php is placed under /home/username/pear/share/pear. It is generated when you install the Mail package and is referenced by the code you inserted into your phprc file when you install Pear.

    2. If your email is hosted by Google, you must update the host line to the following:

     $host = "ssl://smtp.gmail.com"

Helpful articles:

Permitting Google Workspace to send from your email client

If you are utilizing your Gmail address to send via SMTP, you must first provide access to your Gmail address to your application. If you fail to comply, your email will not be authenticated or sent. See the following article for further information:

PHP Email Not Sending Through SMTP: Troubleshooting

Below are common issues you may encounter with the PHP Mail function and the solutions to fix them.

Gmail Couldn’t Verify Domain

Below are possible reasons for seeing the error:

  • The SPF record is not set up. If your TXT record is missing or if you are using external name servers, please update your DNS records manually. Please see DNS Management: How to Add, Edit, or Delete DNS Entries for instructions.
  • Your SMTP authentication details are invalid. Double-check the email address you used to send the message and ensure it is valid.

Mail Goes to Spam Folder

There are several reasons why sending emails through PHP mail can trigger spam filters. Below are some of the most common causes:

  • Using terms such as "urgent" or "test" as a subject line
  • Adding an incorrect email address
  • Using spammy words (for example, this isn't spam)

Sender Address Rejected Error

This error happens when your mail server fails to authenticate based on the information provided. To fix this issue, double-check the email address you used to send the message and ensure it is valid.

Also, check if you have set up your SPF record. For instructions, see How To Setup a DNS SPF (Sender Policy Framework) Record.


Sending emails is a part of our daily communication routine. However, ensuring email security is equally important. This guide provides valuable insights on sending secure PHP emails through SMTP with authentication. Issues may arise when using the PHP mail function. Therefore, we recommend using SMTP authentication scripts to fix the problem. Our step-by-step guide on setting up PEAR and PHP to send SMTP mail will help you avoid issues. Moreover, check out our troubleshooting tips for common problems you may face while sending PHP emails.

If you need further assistance, feel free to contact us via Chat or Phone:

  • Chat Support - While on our website, you should see a CHAT bubble in the bottom right-hand corner of the page. Click anywhere on the bubble to begin a chat session.
  • Phone Support -
    • US: 888-401-4678
    • International: +1 801-765-9400

You may also refer to our Knowledge Base articles to help answer common questions and guide you through various setup, configuration, and troubleshooting steps.

Did you find this article helpful?

* Your feedback is too short