PHP Mail

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

PHP mail() function

Description

The mail() function is used to send a mail.

Version

(PHP 4 and above)

Syntax

mail(to, subject, message, extra headers, additional parameters)

Parameter

Name

Description

Required /
Optional

Type

to

Mail address to which you want to send mail 

Required

String

subject

Subject of the mail 

Required

String

message

Message to be sent with the mail. Each line of the message should be separated with a LF ( ). Lines should not be larger than 70 characters.

Required

String

extra headers

Additional headers like from, CC, BCC. If more than one additional headers are used, they must be separated with CRLF (Carriage return line feed), i.e. new line. 

Optional

string

additional parameters

Additional parameters like -  the envelope sender address when using sendmail with the -f sendmail option, can be used using this parameter. 

Optional

string

Return value

Returns true if the mail is successfully sent, otherwise, it returns false. 

Value Type : boolean

Example :

  1. $to  = 'mail@w3resource.com';
    $subject = 'Demo mail ';  

  2. $message = 'This is a demo mail. Please reply to make sure the mail communication is okay.';  

  3. mail($to, $subject, $message);  

  4. ?>   

Send a simple mail using mail() function

Browse view of the form for sending simple mail in php

send-simple-mail-php

Code

  1.    
  2.  
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4. <html xmlns="http://www.w3.org/1999/xhtml"><head>  
  5. <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type" /><title>send mail in   
  6. phptitle>  
  7. <style type="text/css">  
  8. li {list-style-type: none;  
  9. font-size: 16pt;  
  10. }  
  11. .mail {  
  12. margin: auto;  
  13. padding-top: 10px;  
  14. padding-bottom: 10px;  
  15. width: 400px;  
  16. background : #D8F1F8;  
  17. border: 1px solid silver;  
  18. }  
  19. .mail h2 {  
  20. margin-left: 38px;  
  21. }  
  22. input {  
  23. font-size: 20pt;  
  24. }  
  25. input:focus, textarea:focus{  
  26. background-color: lightyellow;  
  27. }  
  28. input submit {  
  29. font-size: 12pt;  
  30. }  
  31. style>  
  32. head>  
  33. <body>  
  34. <div class="mail">  
  35. <h2>Mail ush2>  
  36. <ul>  
  37. <li><form name="mail" method="POST" action="send.php">li>  
  38. <li>To:li>  
  39. <li><input type="text" name="to" />li>  
  40. <li>Subject:li>  
  41. <li><input type="text" name="subject" />li>  
  42. <li>Message:li>  
  43. <li><input type="text" name="Message" />li>  
  44. <li> li>  
  45. <li><input type="submit" name="submit" value="Send"/>li>  
  46. <li> li>  
  47. form>  
  48. ul>  
  49. div>  
  50. body>html>  

Code of the file for handling form (code above) data :

  1. $to = $_POST['to'] ;      
  2. $message = $_POST['Message'] ;  
  3. mail( $to"Mail"$message,  );  
  4. header( "Location: http://localhost/php/simple-mail.php" );  
  5. ?>  

Send mail with extra headers using mail() function

Browser view of the form for sending mail with extra headers in php 

simple-mail-php-extra-headers

Code

  1.  
  2. "-//W3C//DTD XHTML 1.0 Transitional//EN"  
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4. <html  
  5. xmlns="http://www.w3.org/1999/xhtml"><head>  
  6. <meta content="text/html; charset=ISO-8859-1"  
  7. http-equiv="content-type" /><title>send mail in  
  8. phptitle>  
  9. <style type="text/css">  
  10. li {list-style-type: none;  
  11. font-size: 16pt;  
  12. }  
  13. .mail {  
  14. margin: auto;  
  15. padding-top: 10px;  
  16. padding-bottom: 10px;  
  17. width: 400px;  
  18. background : #D8F1F8;  
  19. border: 1px solid silver;  
  20. }  
  21. .mail h2 {  
  22. margin-left: 38px;  
  23. }  
  24. input {  
  25. font-size: 20pt;<  
  26. }  
  27. input:focus, textarea:focus{  
  28. background-color: lightyellow;  
  29. }  
  30. input submit {  
  31. font-size: 12pt;  
  32. }  
  33. style>  
  34. head>  
  35. <body>  
  36. <div class="mail">  
  37. <h2>Mail ush2>  
  38. <ul>  
  39. <li><form name="mail" method="POST"  
  40. action="send-extra.php">li>  
  41. <li>To:li>  
  42. <li><input type="text" name="to"  
  43. />li>  
  44. <li>Message:li>  
  45. <li><input type="text" name="Message"  
  46. />li>  
  47. <li>From:li>  
  48. <li><input type="text" name="from"  
  49. />li>  
  50. <li>CC:li>  
  51. <li><input type="text" name="cc"  
  52. />li>  
  53. <li> li>  
  54. <li><input type="submit" name="submit"  
  55. value="Send"/>li>  
  56. <li> li>  
  57. form>  
  58. ul>  
  59. div>  
  60. body>html>  

Code of the file for handling form (code above) data :

  1.   $to = $_POST['to'] ;      
  2.   $message = $_POST['Message'] ;  
  3.   $from = $_POST['from'];  
  4.   $cc = $_POST['cc'];  
  5.   $headers =  "From: ".$_POST['from']." " .  
  6. "CC: ".$_POST['cc'];  
  7.   mail( $to"Mail"$message$headers);  
  8.   header( "Location: http://localhost/php/simple-mail.php" );  
  9. ?>  

We encourage you to replace the value of the parameters used in the above example and test it.

Send mail in PHP with additional parameters

You can pass additional parameters as command line options to the program configured to be used while sending mail. Those said configurations are defined by the sendmail_path configuration. One example of using additional parameter is setting the envelope sender address with -f option.

  1. mail('mail@w3resource.com''Demo mail''Testing mail communication', null,  
  2.    '-f admin@w3resource.com');  
  3. ?>   

We encourage you to replace the value of the parameters used in the above example and with your's and test it.