Php Script for Email
Php Script for Email is a platform that makes email development a breeze. It is based on the principle that template-based emails can be created automatically from data, and coded easily with standard PHP code. We can write templates with all the html tags you like, and to finish it off we will also have access to a full on MySQL database along with laravel model functions to complete the process.
The internet is not just for browsing cat photos. It can be for making money. We have made a simple, yet robust and expandable php script for email marketing. This script runs on Linux/Unix, and clients for both Linux and Windows are available for free.
The PHP mail()
Function
Sending email messages are very common for a web application, for example, sending welcome email when a user create an account on your website, sending newsletters to your registered users, or getting user feedback or comment through website’s contact form, and so on.
You can use the PHP built-in mail()
function for creating and sending email messages to one or more recipients dynamically from your PHP application either in a plain-text form or formatted HTML. The basic syntax of this function can be given with:mail(to, subject, message, headers, parameters)
The following table summarizes the parameters of this function.
Parameter | Description |
---|---|
Required — The following parameters are required | |
to | The recipient’s email address. |
subject | Subject of the email to be sent. This parameter i.e. the subject line cannot contain any newline character (\n ). |
message | Defines the message to be sent. Each line should be separated with a line feed-LF (\n ). Lines should not exceed 70 characters. |
Optional — The following parameters are optional | |
headers | This is typically used to add extra headers such as “From”, “Cc”, “Bcc”. The additional headers should be separated with a carriage return plus a line feed-CRLF (\r\n ). |
parameters | Used to pass additional parameters. |
The PHP mail()
Function
Sending email messages are very common for a web application, for example, sending welcome email when a user create an account on your website, sending newsletters to your registered users, or getting user feedback or comment through website’s contact form, and so on.
You can use the PHP built-in mail()
function for creating and sending email messages to one or more recipients dynamically from your PHP application either in a plain-text form or formatted HTML. The basic syntax of this function can be given with:mail(to, subject, message, headers, parameters)
The following table summarizes the parameters of this function.
Parameter | Description |
---|---|
Required — The following parameters are required | |
to | The recipient’s email address. |
subject | Subject of the email to be sent. This parameter i.e. the subject line cannot contain any newline character (\n ). |
message | Defines the message to be sent. Each line should be separated with a line feed-LF (\n ). Lines should not exceed 70 characters. |
Optional — The following parameters are optional | |
headers | This is typically used to add extra headers such as “From”, “Cc”, “Bcc”. The additional headers should be separated with a carriage return plus a line feed-CRLF (\r\n ). |
parameters | Used to pass additional parameters. |
Creating a Test File for PHP Mail
After making sure that Sendmail is active, we’ll create a PHP mail script file and place it in the public_html directory.
Here’s how to do it:
- From hPanel, navigate to Files -> File Manager to access the Hostinger File Manager.
- Double click on the public_html folder and select the New File icon at the top bar. Name this new file testmail.php, then hit Create.

- Double click on testmail.php to edit it. You can use the basic PHP code below, but make sure to change its parameters accordingly. We’ll describe the script components in more detail in the next subsection.
<?php
ini_set( ‘display_errors’, 1 );
error_reporting( E_ALL );
$from = “[email protected]”;
$to = “[email protected]”;
$subject = “Checking PHP mail”;
$message = “PHP mail works just fine”;
$headers = “From:” . $from;
if(mail($to,$subject,$message, $headers)) {
echo “The email message was sent.”;
} else {
echo “The email message was not sent.”;
}
?> - When you’re done editing, click Save & Close.

- Send the email by accessing YourDomain/testmail.php from the browser. Remember to change YourDomain to the domain used when creating testmail.php.
- The recipient will receive the message below:

Sending HTML email
When you send a text message using PHP then all the content will be treated as simple text. Even if you will include HTML tags in a text message, it will be displayed as simple text and HTML tags will not be formatted according to HTML syntax. But PHP provides option to send an HTML message as actual HTML message.
While sending an email message you can specify a Mime version, content type and character set to send an HTML email.
Example
Following example will send an HTML email message to [email protected] copying it to [email protected] You can code this program in such a way that it should receive all content from the user and then it should send an email.
<html> <head> <title>Sending HTML email using PHP</title> </head> <body> <?php $to = "[email protected]"; $subject = "This is subject"; $message = "<b>This is HTML message.</b>"; $message .= "<h1>This is headline.</h1>"; $header = "From:[email protected] \r\n"; $header .= "Cc:[email protected] \r\n"; $header .= "MIME-Version: 1.0\r\n"; $header .= "Content-type: text/html\r\n"; $retval = mail ($to,$subject,$message,$header); if( $retval == true ) { echo "Message sent successfully..."; }else { echo "Message could not be sent..."; } ?> </body> </html> Understanding PHP Mail Components To help you understand the PHP mail() function, we’ll go over the components of the PHP script we used in the previous section: ini_set( 'display_errors', 1 ); error_reporting( E_ALL ); The first two lines above enable error reporting to tell you if the PHP script has failed to execute. $from = "[email protected]"; This line should contain the sender’s email address. Most hosting providers forbid adding random email addresses here, as they can be used for spoofing. Thus, it’s better to use one with your domain name to execute the script successfully. $to = "[email protected]"; The recipient’s email address goes here. If you want to deliver the message to multiple recipients, separate their email addresses with commas. $subject = "Checking PHP mail"; Enter the subject line for the email here. $message = "PHP mail works just fine"; Here, input the body of your email message. $headers = "From:" . $from; This line is commonly used to add additional headers, such as From, Reply-To, and Cc – these extra headers should be separated with a CRLF (\r\n). if (mail ($to,$subject,$message,$headers)) This line is used to execute the function and check whether it has run successfully. echo "The email message was sent."; The message above will appear when the script is executed successfully. Alternatively, the message below will be displayed. echo "The email message was not sent."; Keep in mind that although additional headers are optional, it’s essential to mention the From header when sending mail. Otherwise, you’ll receive a notification like: Warning: mail(): "sendmail_from" not set in php.ini or custom "From:" header missing. Conclusion If you need a script for sending email with php, here it is. This php script for email is simple, easy to install and flexible. It lets the user create plain text or html emails and send them via SMTP (sendmail)