How to set up mail or send PHP email

Introduction

The PHP mail () function is designed to send email using PHP, in other words – sending PHP email. This is useful if you want to create a contact form on your site. With the PHP mail () function, emails or emails can be sent using a simple PHP script. In this guide, you will learn how PHP email is sent and test the PHP mail () function.

What is needed

Before proceeding with the manual, check the availability of:

  • Access to your hosting account
  • PHP mail function enabled ()

Step 1 – Create a new PHP file

First of all, you need to create a PHP file on your hosting account. Your PHP file must be accessible by domain name, therefore, it must be placed in the public_html directory. To create a file you can use File Manager or FTP client. Name your file testmail.php.

Step 2 – Editing the PHP File

Now place the following code inside the testmail.php file:

<?php
    ini_set( 'display_errors', 1 );

    error_reporting( E_ALL );

    $from = "[email protected]";

    $to = "recipientemailaddress";

    $subject = "Checking PHP mail";

    $message = "PHP mail works just fine";

    $headers = "From:" . $from;

    mail($to,$subject,$message, $headers);

    echo "The email message was sent.";
?>
  • The first two lines include error messages, so you can see if the script contains errors and does not start.
  • $from – the email address of the sender. Many hosting providers do not allow the use of a random email address, so you need to enter the email address created for your domain name in this field for successful authorization and sending emails.
  • $to – the email address of the recipient. If you are sending an email for verification, then this should be your own email address.
  • $subject – email subject.
  • $message – the contents of the email.
  • $headers – each email has headers. They contain important information such as From ( From) email address.
  • mail ($ to, $ subject, $ message, $ headers) is a PHP function that performs mailing. Do not change anything in this line.
  • echo “Your email has been sent successfully.” – The PHP echo function will display a message on the screen as soon as the script has successfully completed.

Step 3 – Running the script and sending email to PHP

If (as recommended above) you have placed the PHP script in the public_html directory, you can run the script by accessing yourdomainname / testmail.php . If you see a message 
Your email has been sent successfully in your browser – this means that the email was sent successfully and you can check it in your email inbox (do not forget to check the SPAM / JUNK folders).

If you get any error, check all the parts you have filled out correctly. If the error does not appear in the browser, you can check the error_log file in your account. If you are not sure how to check this file, see the manual for the error logs link.

Conclusion

Now you know how PHP emails are sent. Although this guide is intended for testing, the same syntax can be used to develop a contact form on your site. However, if you need a more complex email sending PHP, you can search for scripts here: PHPMailer project. The project has built-in support for SMTP, extensive error_logs in 47 languages, and many other useful features.

Add a Comment

Your email address will not be published. Required fields are marked *