PHP – Send Emails , Tutorial and Working examples
Email is the most popular Internet service today. A plenty of emails are sent and delivered each day. The goal of this tutorial is to demonstrate how to generate and send emails in PHP.
Definition
PHP makes use of mail() function to send an email.
Syntax
mail(to,subject,message,headers,parameters);
NOTE: to ,subject and message are mandatory arguments.
Here is the description for each parameters.
to – Required. Specifies the receiver / receivers of the email
subject– Required. Specifies the subject of the email. 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
headers– Optional. Specifies additional headers, like From, Cc, and Bcc. The additional headers should be separated with a CRLF (\r\n)
parameters– Optional. Specifies an additional parameter to the sendmail program
As soon as the mail function is called PHP will attempt to send the email then it will return true if successful or false if it is failed.
Testing mail() without header argument:
<?php
$mail = mail('webmaster@example.com', 'PHP Enquiry',
'Please send us PHP course and fee details');
if ($mail == true){
echo 'Mail Sent Successfully';
}else{
echo 'Mail is not Sent';
}
?>
|
Testing mail() with header argument:
<?php
$mail = mail('abc@domain.com', 'PHP Enquiry', 'Please send us PHP course details.',
'From: webmaster@example.com');
if ($mail == true){
echo 'Mail Sent Successfully';
}else{
echo 'Mail is not Sent';
}
?>
|
Note: When sending an email, it must contain a From header. This can be set with ‘header’ parameter or in the php.ini file.
PHP Send Text Email Example
<?php
$to = "abc@domain.com";
$subject = "PHP Enquiry";
$txt = "Please send us PHP course and fee details.";
$headers = "From: webmaster@example.com" . "\r\n" .
"CC: xyz@example.com";
mail($to,$subject,$txt,$headers);
?>
|
PHP Send HTML Email Example
<?php
$to = "abc@domain.com";
$subject = "HTML email";
$message = "
<html>
<head>
<title>PHP Enquiry</title>
</head>
<body>
<p>Course Details.</p>
<table>
<tbody>
<tr>
<th>PHP Basics</th>
<th>PHP Advanced</th>
</tr>
<tr>
<td>15 days</td>
<td>25 days</td>
</tr>
</tbody>
</table>
</body>
</html>";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: <webmaster@example.com>' . "\r\n";
$headers .= 'Cc: myboss@example.com' . "\r\n";
mail($to,$subject,$message,$headers);
?>
|
NOTE:
- If your message is larger than 70 characters, you should use wordwrap()
Example : $message = wordwrap($message, 70, “\r\n”);
- You can display your name at sender’s email address.
Example : $headers = ‘From: CMS Computer <webmaster@example.com>’ . “\r\n”;
- You can send email to many people by adding ‘ , ‘ after each email address.
Example : $to = “html@domain.com, css@domain.com, php@domain.com”;
- You can also add Reply_to address in $headers.
Example : $headers = ‘From: webmaster@example.com’ . “\r\n” .
‘Reply-To: webmaster@example.com’ . “\r\n” .
‘X-Mailer: PHP/’ . phpversion();
- You can also add addtional headers like CC, BCC.
Example : $headers .= ‘From: CMS Computer Training Center <cms@example.com>’ . “\r\n”;
$headers .= ‘Cc: php@example.com’ . “\r\n”;
$headers .= ‘Bcc: mysql@example.com’ . “\r\n”;
- Each line should be separated with a CRLF – Carriage Return Line Feed (\r\n).
- In the above examples ‘ .’ is used to concatenate strings.