In this article you are going to learn how to send an email using php scripts. An email is simply a message sent through the digital platforms such as mobile phones, computers etc between two users using the internet. Likewise, a PHP mail is a message sent through the digital platforms by using the php scripts.
This is similar to the way we send a normal mail using a gmail account in which three main fields are required namely receiver’s address, subject and the message to be sent. Similarly, these three parameters are required while sending an email using php scripts. Join Our php training in Gurgaon and learn sending emails and other advanced php functionality with help of professional instruction.
CONFIGURATION REQUIRED TO SEND EMAIL USING PHP
Sending an email requires the correct configuration of the mail function which you can check by opening the php.ini file. Here, you will find the mail function section. After opening it, you will see the two main details, first one is SMTP server address and the second one is sendmail_from i.e, email address as shown below.[mail function] ; for win32 only
SMTP = smtp.secureserver.net
; for win32 only
sendmail_from = php@w3trainingschool.com
PHP MAIL SYNTAX
mail($to, $subject, $message, $additional_headers, $additional_parameters);
There are five parameters used in a php mail which are described below :
(1) $to – it denotes the receiver’s address( to whom mail/message is to be sent). For instance, $to = ‘username12@example.com’. One thing should be noted regarding the receiver’s address, it should follow the message format RFC(Request For Comments)-2822.
(2) $subject – it denotes the subject matter to be sent to the receiver. It should follow the message format RFC-2047.
(3) $message – it denotes the message to be sent to the receiver by the sender. It is the main part of the email. ASCII 13(\r) and ASCII 10(\n) codes should be used to separate the lines of the message. Try to complete each line within 70 characters. But if in case your lines exceeds 70 characters, use the function wordwrap() whose basic syntax is $message = wordwrap($message, 70, “\r\n”);
(4) $additional_headers – this string parameter represents extra headers like From, Cc, Bcc that a sender can use in a php mail. Some of the programmers treat this header as a optional parameter, but it should be noted that the additional header ‘From’ is the essential part of the mail. Excluding this header from the message body might result in a error(warning) message.
(5) $additional_parameters – this string parameter is used to pass additional parameters such as flags etc. using the sendmail_path. It uses an envelope sender denoted by -f.
EXAMPLE OF SENDING AN EMAIL USING PHP
(1) Without using additional headers and parameters(a simpler way) :
<?php
// Receiver’s address
$to = ‘xyz@example.com’;
// Subject matter
$subject = ‘abc’;
// Message to be sent
$message = “Message_Line 1\r\nMessage_Line 2\r\nMessage_Line 3”;
// If message line exceeds 70 characters, use wordwrap() function
$message = wordwrap($message, 70, “\r\n”);
// Sending the message
if (mail($to, $subject, $message)) {
echo(“Email Sent Successfully“);
} else {
echo(“Email Could Not Be Sent“);
}
?>
(2) Using additional headers :
<?php
$to = ‘xyz@example.com’;
$subject = ‘abc’;
$message = ‘hello’;
$headers = ‘From: abc@example.com’ . “\r\n”.
‘Cc: abcd@example.com’;
if (mail($to, $subject, $message, $headers)) {
echo(“Email Sent Successfully“);
} else {
echo(“Email Could Not Be Sent“);
}
?>
(3) Using additional parameters :
<?php
$to = ‘xyz@example.com’;
$subject = ‘abc’;
$message = ‘hello’;
$parameter = ‘-fwebmaster@example.com’;
if (mail($to, $subject, $message, parameter)) {
echo(“Email Sent Successfully“);
} else {
echo(“Email Could Not Be Sent“);
}
?>
(4) Sending a HTML mail using PHP

Output :
