PHP Send Email Example
E-mail(electronic mail) has become an integral part of the internet. Approximately all tech savy people have an email address. If you have a facebook account, you sure have registered it with an email address.
E-mails are simply messages that can contain text, attachments and they are sent through a computer network to individuals or groups of individuals.
In this example we are going to learn how to send e-mails with PHP.
For this example we will use:
- A computer with PHP 5.5 installed
- notepad++
1. PHP mail() function
function boolean mail(to,subject,message,headers,parameters)
is used to send e-mails in PHP. It takes six parameters and returns true if email was sent, or false on failure.
- to: This is a required field and it specifies the receiver of the email. It must be a correctly formated e-mail address.
- subject: This is a required field. It specifies the subject of the email. This field should not contain new line characters.
- message: This is a required field. It specifies the message to be sent. Lines should not be larger than 70 characters and each line should be separated by (\n)
Note: (Windows only) 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:<?php $text = str_replace("\n.", "\n..", $text); ?>
- headers: This field is optional. It specifies the headers
- parameters: This is an optional field, it specifies additional parameter.
mail.php
<?php if(isset($_POST["sendmail"])){ // the message $msg = $_POST["message"]; $rece= $_POST["receiver"]; $subject=$_POST["subject"]; // use if lines arelonger than 70 characters $msg = wordwrap($msg,70); // send email if(mail($rece,$subject,$msg)){ echo "done"; } else{ echo "error"; } } ?> <!DOCTYPE html> <html lang=en> <head> <style> html, body{ width:100%; height:100%; margin:0%; font-family:"helvetica","verdana","calibri", "san serif"; overflow:hidden; padding:0%; border:0%; } .a{ width:20%; margin:10px; height:40px; } .a1{ display:inline; width:20%; height:30px; } #sub{ display:inline; } </style> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, target-densitydpi=device-dpi"/> <title></title> </head> <body> <div id=cen align=center> <h1>Email Client</h1> <form action="mail.php" method=post> <input type=email class=a placeholder=receiver name=receiver required><br> <input type=text class=a placeholder=subject name=subject required><br> <textarea rows="4" cols="50" required name=message></textarea><br> <input type=submit class=a value="Send Mail" name=sendmail> </form> </div> </body> </html>
This script presents an html form to the user. The users fill in the receiver email. The subject of the email and the email body. On line ten we call PHP mail()
function and pass it the required parameters. On line 8 we use PHP wordwrap
function to break the message into new lines.
It is important to note that just because the mail was accepted for delivery, it does NOT mean that the mail will actually reach the intended destination.
1.2 Sending emails with extra headers
This script sends an email to test@example.com
mail1.php
<?php $to = 'test@example.com' ; $subject = 'Just Testing PHP mail function' ; $message = 'I am just testing PHP mail function' ; $headers = 'From: tester@example. com' . "\r\n" . "CC: somebodyelse@example.com"; mail ( $to , $subject , $message , $headers ) ; ?>
1.2 Sending an HTML email
When you send a message using PHP, then all the content will be treated as simple text. Even if you include HTML tags in the message, it will be displayed as simple text and HTML tags will not be formatted according to HTML syntax. PHP provides though an 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.
mail3.php
<?php // multiple recipients $to = 'tester@example.com' . ', ' ; // note the comma $to .= 'tester2@example.com' ; // subject $subject = 'Sending you a test email' ; // message $message = ' <html> <head> <title>Just testing this email, please do not take it too seriously</title> </head> <body> <h3><b>Reminder</b>: do not take this email too seriously</h3> <h2>This is a test email</h2> <h3>This is a test email</h3> <h4>This is a test email</h4> <h5>This is a test email</h5> <h6>This is a test email</h6> </body> </html> ' ; // To send HTML mail, the Content-type header must be set $headers = 'MIME- Version: 1.0' . "\r\n" ; $headers .= 'Content-type: text/ html; charset=iso-8859-1' . "\r\n" ; // Additional headers $headers .= 'To: Mary <mary@example .com>, Kelly <kelly@example.com>' . "\r\n" ; $headers .= 'From: Group of testers <testers@example.com>' . "\r\n" ; $headers .= 'Cc: testerarchive@exa mple.com' . "\r\n" ; $headers .= 'Bcc: testercheck@exam ple.com' . "\r\n" ; // Mail it mail( $to , $subject , $message , $headers ); ?>
2. Summary
In this example we learnt about emails and how to send different kind of e-mails in PHP.