Search The Hostwinds Guides Knowledge Base

Why Won’t my PHP Script send mail?

Share This Article [TheChamp-Sharing]

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,
    <?php
    	// 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.
    	require_once("PHPMailer/PHPMailer.php");
    	
    	// 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 = "from@example.com";
    	
    	// 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("from@example.com", "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("to@example.com", "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!";
    	}
    ?>
  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.

If you should have any questions or would like assistance, do feel free to contact us through Live Chat, on our Phones, or by submitting a ticket with our Technical Support team.