How Do I Send an Email Message Using the PHP Mail() Function?

Home » Knowledge Base » Email » Mailing Scripts » How Do I Send an Email Message Using the PHP Mail() Function?

One of the most ubiquitous methods of communication nowadays is the sending and receiving of email messages. Unlike other forms of electronic communication, emails are very reliable and are universally accepted – when you send a message to someone, you know that they will be able to read it regardless of the operating system, email client, or mail provider they use. Due to these immense benefits, email has become the preferred method of communication for countless individuals and businesses around the world.

Writing email messages using your favorite email program or your mail provider’s web app is probably second nature to you. But did you know that you can also send messages via scripts from your hosting space? In this tutorial, we will show you how you can send email messages via a PHP script.

 

How Can I Check Whether the PHP Mail() Function Is Enabled on My Account?

Before you attempt to send an email message using the PHP mail() function, you should make sure the PHP mail() function is actually enabled on the server. You can perform this check by running a small test script. Follow the steps below in order to create this script and execute it:

  1. Open the File Manager section of the Control Panel. If prompted, log into your account.
  2. Open your domain name folder.
  3. Use the Create button to make a new file. Call the file something like test.php.
  4. Copy over the code below and save the file.
 <?php
    if(function_exists('mail')) {
        echo "PHP mail() function is enabled";
    }
    else {
        echo "PHP mail() function is not enabled";
    }
 ?>
  1. Now copy the path to the test script and run it in your web browser. The path should look something like .

If all goes well, you should see the message PHP mail() function is enabled as shown in the screenshot below:

The test script confirms that the PHP mail() function is enabled on the server.
The test script confirms that the PHP mail() function is enabled on the server.

If there is a problem, you will see a PHP mail() function is not enabled message. In the unlikely event that this happens, you should contact the 24/7 Technical Support Team so they can resolve any problems with the server.

The test script shows that the PHP mail() function is disabled which is likely due to a bad server configuration.
The test script shows that the PHP mail() function is disabled which is likely due to a bad server configuration.

 

Are Free Accounts Allowed to Send Messages Using the PHP Mail() Function?

If you are using our free hosting service, you may be worried that this functionality will not be available to you. The good news is that even free hosting accounts are allowed to use the PHP mail() function. There are just two caveats that you need to keep in mind.

First, to send an email message, you need to have an email account created in your hosting space. This email account will act as the sender. Email accounts can be created through the Email Accounts section of the Control Panel. To have an email account, you need to own a fully-qualified domain name or grab a free .DX.AM domain name from the Domain Manager.

The second caveat to note is that in order to prevent spammers from abusing our free service, we have limited the number of messages that can be sent to 31 messages in any given 31-day period. You can lift this restriction by upgrading to one of the paid hosting packages that we offer.

 

How Do I Send an Email Message Using the PHP Mail() Function?

Email messages can be sent via a PHP script by running it. Running a script is as simple as opening it in your web browser, just like a regular page on the web. Follow along as we show you how to create and run your first script that uses the PHP mail() function:

  1. Log in to the Hosting Control Panel.
  2. If you don’t have an email account yet, create one in the Email Accounts section of the Control Panel. Not sure how to make an email account? Check our guide on how to create an email account.
  3. Navigate to the File Manager section of the Control Panel.
  4. Double-click on your domain folder in order to open it.
  5. Use the Create button to make a new file. Call the file something like mail-test.php.
  6. Copy over the code below into the empty .PHP file you just created.
<?php

$awardspaceEmail = "FROM";
$recipientEmail = "TO";

$from = "From: Mail Contact Form <" . $awardspaceEmail . ">";
$to = $recipientEmail;

$subject = "PHP mail() Test";

$body = "This is a test message sent with the PHP mail function!";

if(mail($to,$subject,$body,$from)){
    echo 'E-mail message sent!';
} else {
    echo 'E-mail delivery failure!';
}

?>
  1. You will now have to make two additions to the script. Near the beginning of the script, you will notice the following line of code: $awardspaceEmail = "FROM";. You need to delete the FROM text and in its place enter the email address that you created earlier in Step 2 using the Email Accounts section of the Control Panel. Similarly, the next line is $recipientEmail = "TO";. Here you need to remove the TO text and replace it with the email address where you would like to receive your test message.
  2. After updating the two fields mentioned in the above step, save your changes.
  3. Now copy the path to the mail test script and run it in your web browser. The path should look something like .
  4. Once the script finishes loading, you should see the message E-mail message sent! printed on the screen if the message was sent successfully. If there’s a problem, you will see an E-mail delivery failure! instead. In case of an error, you can contact our 24/7 Technical Support Team and they will assist you in resolving any issues that are present.
  5. Verify that your script has successfully sent the test message by checking your email inbox. You should receive a test message that looks like the one shown below:
The PHP mail() function test script has successfully sent a message and it has arrived in the recipient's inbox.
The PHP mail() function test script has successfully sent a message and it has arrived in the recipient’s inbox.

 

Is There a Limit to the Number of Messages I Can Send Using the PHP Mail() Function?

As previously mentioned, the free hosting plan allows you to send up to 31 messages for a 31-day period. Upgrading to one of our paid shared hosting plans significantly increases this limit. With a paid plan you can send up to 1000 messages per day. If you need to send even more messages, our Semi-Dedicated hosting enables you to send up to 2000 messages per day. Last, but certainly not least, getting a Virtual Private Server (VPS) package would allow you to send an unlimited number of messages. A VPS is only limited by his hardware and processing power as to the number of messages it can send.

A word of warning: AwardSpace has a zero-tolerance policy when it comes to the sending of unsolicited email messages (SPAM). If you use our mail servers for such an activity, you risk having your hosting account permanently suspended without the option to get a refund.

 

How Do I Create a Contact Form for My Website?

Now that you are able to send messages via PHP scripts, you may have your sights set on implementing a full-blown contact form into your website. That’s actually surprisingly easy to do. We cover the whole process in our guide on how to create a working contact form.

 

Conclusion

The ability to send email messages through your website enhances the experience your visitors have when they visit your site. Implementing a contact form, sending order confirmation messages, and emailing a password reset link to a member of your site who has misplaced their password can all make your website much more professional and appealing. And best of all – PHP allows you to automate these messages, so once you set up your site, it will just work with no extra effort required on your part.

Was this post helpful?

i

Relevant tags:

Connect

Latest posts:

How to Find Modules in MediaWiki

After you install MediaWiki, log in to the CMS, and presumably install extensions, it is time to learn how to find modules in MediaWiki.   What are MediaWiki Modules Modules in MediaWiki are Lua-based scripts that allow you to add various functionalities to your...

How to Fix MediaWiki Internal Error: A Step-By-Step Guide

MediaWiki Internal Error message can be very frustrating and can significantly harm your MediaWiki project. Luckily, the issue can be fixed in a few easy-to-follow steps to get your website up and running in no time. There are various causes for the issue: Incorrect...

How to Add Infobox Module in MediaWiki

The Infobox in MediaWiki is a handy tool for presenting content and key information in a structured and visually pleasing way. Infoboxes are useful for any kind of MediaWiki project and can provide your readers with a quick snapshot of important data and details. In...

How to Install a MediaWiki Skin

In MediaWiki, skins determine the visual appearance of your wiki website. Skins define how content is presented to users and include such settings for the overall website layout, typography, color scheme, and more. By default, when you install MediaWiki, the platform...

How to Create a Page in MediaWiki

Creating pages in MediaWiki is а core feature that allows you to add content to your MediaWiki website. Whether you are adding details for a project, contributing to a knowledge base, or anything else, learning how to create a page in MediaWiki is crucial for your...



Create a website for free!


Free forever

Our Support Team is Here to Help

 

If you need any questions answered, don't hesitate and contact us. Click the button below and follow the instructions. You can expect an answer within an hour.

 

Contact AwardSpace

 

iNewest knowledge base articles

How to Find Modules in MediaWiki

After you install MediaWiki, log in to the CMS, and presumably install extensions, it is time to learn how to find modules in MediaWiki.   What are MediaWiki Modules Modules in MediaWiki are Lua-based scripts that allow you to add various functionalities to your...

How to Fix MediaWiki Internal Error: A Step-By-Step Guide

MediaWiki Internal Error message can be very frustrating and can significantly harm your MediaWiki project. Luckily, the issue can be fixed in a few easy-to-follow steps to get your website up and running in no time. There are various causes for the issue: Incorrect...

How to Add Infobox Module in MediaWiki

The Infobox in MediaWiki is a handy tool for presenting content and key information in a structured and visually pleasing way. Infoboxes are useful for any kind of MediaWiki project and can provide your readers with a quick snapshot of important data and details. In...

How to Install a MediaWiki Skin

In MediaWiki, skins determine the visual appearance of your wiki website. Skins define how content is presented to users and include such settings for the overall website layout, typography, color scheme, and more. By default, when you install MediaWiki, the platform...

How to Create a Page in MediaWiki

Creating pages in MediaWiki is а core feature that allows you to add content to your MediaWiki website. Whether you are adding details for a project, contributing to a knowledge base, or anything else, learning how to create a page in MediaWiki is crucial for your...

How to Install MediaWiki Extensions

Now that you have installed MediaWiki and learned how to log in to the CMS, it is time to extend MediaWiki's functionality via extensions. MediaWiki extensions are sets of files that add various types of functionality to your MediaWiki website. You can use extensions...

Even more web tutorials

Check out our web hosting knowledge base and the WordPress tutorials to learn more, and be better prepared for your website creation and maintenance journey.