Created by Technooriginal.com, PHP Script For Sending Email is a simple script meant to send email to php mail function. The main feature of PHP Script For Sending Mail is that this php mail script is customizable and very easy to use. PHP Script for sending email has many features such as adding images to the email body and attachment, using HTML or plain text format, adding special chars or symbols in the subject line and other options that can be enabled or disable according to your requirements.
Many companies use email as an important part of their day-to-day communication with clients and end users. Because email is such an important method of communication, it is often one of the first things replaced after a disaster. Unfortunately, many people do not create a plan for backing up their email databases before they are forced to replace them. They also don’t have tools on hand to migrate their data back into their working environments when they need to restore.
How to Send Emails Using PHP mail() Function
The first method to send emails directly from a PHP script is by using the built-in mail() function.
To use the PHP send mail feature, users hosting their PHP application or site on a local server will need to configure a Sendmail program by changing the php.ini file in their PHP installation folder.
If you use a hosting server, Sendmail is usually already configured. However, you need to make sure that your hosting provider allows you to manually manage the Sendmail service option.
Hostinger users can toggle this function by accessing the hPanel and navigating to Emails -> Mail Service Control.
By default, the Sendmail service is already enabled. Nevertheless, double-check it to be sure.
More PHP Email Options
Here are a few more options to keep in mind:
- If you want to include a From header line, it’s easy to add it to your PHP script.
- The mail() function included with stock PHP doesn’t support SMTP authentication. If you need this function, send the email using SMTP authentication.
- To make sure users enter an actual email address, validate the text field to make sure it contains an email-like structure.
- If you want to specify the receiver’s name in addition to the To address, add the name within the quotes and then put the email address in brackets.
PHP Mail Function
|to||Required. Specifies the receiver / receivers of the email|
|subject||Required. Specifies the subject of the email. Note: This parameter cannot contain any newline characters|
|message||Required. Defines the message to be sent. Each line should be separated with a LF (\n). Lines should not exceed 70 characters.Windows note: If a full stop is found on the beginning of a line in the message, it might be removed. To solve this problem, replace the full stop with a double dot:|
$txt = str_replace(“\n.”, “\n..”, $txt);
|headers||Optional. Specifies additional headers, like From, Cc, and Bcc. The additional headers should be separated with a CRLF (\r\n).Note: When sending an email, it must contain a From header. This can be set with this parameter or in the php.ini file.|
|parameters||Optional. Specifies an additional parameter to the sendmail program (the one defined in the sendmail_path configuration setting). (i.e. this can be used to set the envelope sender address when using sendmail with the -f sendmail option)|
First, create a HTML form with three fields
How to Send PHP Mail with Authentication
You can download a fully functional PHP Contact Form for your Website at: Contact Form with PHP Email to Send Email from your Website
Utiware’s SMTP Email Servers require authentication before emails can be sent. Please use the following PHP emailing script to see how to include SMTP authentication in your PHP code. Replace “firstname.lastname@example.org”with your valid email address that is hosted on our server, and use its corresponding password for authentication.
Our Email Servers will not send emails on behalf of random people, so this is why the Sender of the email must match EXACTLY the email address that is used for SMTP Authentication.
For the $host variable, please replace “smtp.YourDomain.com” with your own domain’s smtp address.
require_once “Mail.php”; //Mail.php is already installed on the Web Server. You do not need to upload this file
$headers = array (‘From’ => $from,
‘To’ => $to,
‘Subject’ => $subject);
$smtp = Mail::factory(‘smtp’,
array (‘host’ => $host,
‘auth’ => true,
‘username’ => $username,
‘password’ => $password));
$mail = $smtp->send($to, $headers, $body);
echo(“” . $mail->getMessage() . “”);
echo(“Message successfully sent!”);
How to send HTML mails using PEAR Mail
To send HTML Mail, please add the following line to your PHP code to include mime features.
require_once “Mail/mime.php”; //mime.php is already installed on the Web Server. You do not need to upload this file
Please see below for an example code of how to send HTML emails using PEAR Mail
require_once (“Mail/mime.php”); //mime.php is already installed on the Web Server. You do not need to upload this file
$from = “Matha Sender <email@example.com>”;
$to = “James Recipient <firstname.lastname@example.org>”;
$subject = “Hi! This is the HTML Mail example”;
$host = “smtp.YourDomain.com”; //replace this with your domain’s SMTP address
$port = ’25’;
$username = “email@example.com”;
$password = “Put your firstname.lastname@example.org password here”;
$text = “Hello, How are you doing?”;
$html_message = “<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>
<meta http-equiv=”content-type” content=”text/html; charset=UTF-8″>
<title>HTML Email Test Page</title>
<p>Hello, How are you doing?</p>
$headers[‘From’] = $from;
$headers[‘To’] = $to;
$headers[‘Subject’] = $subject;
$headers[‘Content-Type’] = ‘text/html; charset=UTF-8’;
$mime = new Mail_mime;
//$mimeparams[‘debug’] = ‘True’; //Uncomment this if you want to view Debug information
$body = $mime->get($mimeparams);
$headers = $mime->headers($headers);
$smtpinfo[‘host’] = $host;
$smtpinfo[‘port’] = $port;
$smtpinfo[‘auth’] = true;
$smtpinfo[‘username’] = $username;
$smtpinfo[‘password’] = $password;
//$smtpinfo[‘debug’] = ‘True’; //Uncomment this if you want to view Debug information
$mail=& Mail::factory(‘smtp’, $smtpinfo);
$mail->send($to, $headers, $body);
What Are the Options to Send Emails in PHP?
PHP is a widely used server side scripting language. It provides many built-in functions which we call predefined functions of PHP. PHP provides email support via a built-in mail() function. Using this function, you can easily send emails directly through your PHP script. Alternatively you may use the phpmailer library to send emails. Check out this tutorial on phpmailer example to see how to use this library.
But it’s not the only external mail package for PHP. There are several popular options, such as Pear Mail and Swift Mailer. Unlike the mail() function, PHP mail libraries offer much more advanced solutions for handling emails. Explore them in the guide on sending email in PHP tailored by the Mailtrap.
A tool for sending business email. It works as an independent software and you can use it to send mails from different locations with different servers and using different protocols. It has been written in PHP, so it is easily deployable on any over Apache compatible server (i.e. Windows, Linux, BSD, OSX etc.)