Hostwinds Tutorials

Search results for:

Table of Contents

How To Send Mail From A PHP Script
What If This Is With A WordPress Plugin?

Why Won’t my PHP Script send mail?

Tags: Email,  WordPress 

How To Send Mail From A PHP Script
What If This Is With A WordPress Plugin?

There are many reasons you may be unable to send emails from your PHP code. One of the most common reasons would be that the script is not using authentication. Most email servers require that you authenticate the email account before you can send emails from it. This is to prevent any potential spoofing of the emails and unauthorized emails being sent through the email accounts.

While Hostwinds does not usually assist with the coding or development of the site, here is a short guide on an example PHP Script to send emails.

How To Send Mail From A PHP Script

Various ways can be used in PHP to send emails. In this example, we will be using the PHPMailer. Ensure that you have the email address that you will be sending emails from already created on the server. For cPanel, we have a guide on how to do that here. Once you have that email address created, you can proceed with the steps below.

Since this will be done using PHP code, you can create a test PHP file. For now, name it as sendemail.php

After the page is created, you will want to edit that file. You can either edit this file directly in cPanel or on your local computer. If you edit it on your local computer, make sure that you upload the file back to your server.

Once you have the file opened. You will want to type some code in. Here is a small snippet that we will be using,


// This will allow us to incorporate the PHPMailer class into our program. 
// This assumes that PHPMailer is located in a folder called PHPMailer in the same directory.

// This enables us to use the namespace of PHPMailer to instantiate the class.
use PHPMailer\\PHPMailer\\PHPMailer;
    // Make sure that you have included the necessary PHPMailer files to be used with this code
    $t\_mailer = new PHPMailer;

    // Set the server to use SMTP Authentication (Check Username and Password)
    $t\_mailer->SMTPAuth = true;

    // The username that will be used to login to the webserver to send the email.
    $t\_mailer->Username = "";

    // The password that will be used to login to the webserver as well.
    $t\_mailer->Password = "SecretPassword";

    // This is the method of authentication with the webserver.
    $t\_mailer->SMTPSecure = 'tls';

    // This is the port that your SMTP server is using to connect.
    $t\_mailer->Port = 587;

    // Here you will set the from address. This would be the email account that is sending the mail.
    $t\_mailer->setFrom("", "Name for the owner of the Account");

    // Here you will add the addresses that will be receiving the mail. You can add more than one if you would like.
    $t\_mailer->addAddress("", "Name for who is being sent the email.");

    // This is where you can set the subject for the email being sent.
    $t\_mailer->Subject = "Here you can put the subject of the message.";

    // Here you will type out the text inside the email that is being sent.
    $t\_mailer->Body = "This will be the message body that is sent.";

    // This is a basic If statement, that can be used to send mail. 
    if(!$t\_mailer->send()) {
        // If the mailer was unable to send the email, it will display an error message.
        echo "Message has not been sent!";
        echo "Mailer Error: " . $t\_mailer->ErrorInfo;
    } else {
        // If the mailer was able to send the email, it will say sent successfully.
        echo "Message has been sent successfully!";
  1. Make sure that when you are typing the above code or using it as a reference. That you replace the email addresses and passwords that are used. If these are incorrect, or the email account does not exist on the server, the email will fail to send.
  2. Save the document so that your changes will be kept and can be used to send the email.
  3. When you visit the page, you just created within your web browser. It should send an email. This was just an example that could be used. With the code, you can do almost anything from sending confirmation emails to newsletters.

What If This Is With A WordPress Plugin?

Regarding being unable to send an email with a WordPress plugin, the issue may be that the email account does not exist. Please make sure that the email account exists and the password being used is correct.

There is also a log file that can be viewed for error messages when the plugin tried to send emails. That message will usually give a brief description or code on why the email could not be sent. From there, it would be easier to diagnose why the plugin could not send the email.

Even though we don't assist with the coding or development side of things, we are more than happy to take a look into this for you. If you are having issues sending emails and would like assistance, we are always available and will do our best to point you in the right direction.

If you should have any questions or would like assistance, please contact us through Live Chat or by submitting a ticket with our Technical Support team.

Written by Michael Brower  /  October 30, 2017