Search The Hostwinds Guides Knowledge Base

Why Won’t my PHP Script send mail?

Share This Article

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 as well as 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

There are various ways that can be used in PHP to send emails. In this example, we will be using the PHPMailer. Make sure 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.

  1. Since this will be done using PHP code, you can create a test PHP file. For now, name it as sendemail.php
  2. 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.
  3. Once you have the file opened. You will want to type some code in. Here is a small snippet that we will be using,
  4. 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.
  5. Save the document so that your changes will be kept, and can be used to send the email.
  6. Now when you visit the page that you just created within your web browser, it should now 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?

In regards to being unable to send 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 was not able to 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.
