Sending e-mails with PHP mail() function

Date: December 5th, 2006
Author: Ivory Morhuld

You have seen html form whose send e-mails based on content entered in the form and now you want the same form on your web site. Ok, no problem.

Let”s start with html file which containing the form where user writes his message and send by pressing the [Send] button.

Please note that we have one input field named “send” with value parameter set to 1. This field we will use later when sending form to server to check that the form is truly sent.

Also in our example we using POST method for sending the data and for action of form we use the same file with form – $_SERVER[''PHP_SELF'']

  1. <html>
  2. <head>
  3. <title>Sending e-mails with PHP mail() function</title>
  4. </head>
  5. <body>
  6. <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  7.  <input type="text" name="from_name">
  8.  <input type="text" name="from_email">
  9.  <textarea name="message"></text>
  10.  <input type="hidden" name="Send" value="1">
  11.  <input type="submit" value="Send">
  12. </form>
  13. </body>
  14. </html>

You have to know that when in your php.ini register_globals = Off and you receiving data with POST method you have to use $_POST[‘var_name’] instead $var_name!

  1. <?php
  2. $send=$_POST['send'];
  3. $from_name=$_POST['from_name'];
  4. $from_email=$_POST['from_email'];
  5. $message=$_POST['message'];
  6. ?>

PHP mail() function offer some parameters. Function will return true on success and false on fail. Parameter $headers is not obligate. The main format of mail() function is:

  1. <?php
  2. $send_mail = mail($to, $subject, $message, $headers);
  3. ?>

All of this we can put in one PHP function for more functionality and clear code.
Variable $to is used for specifying address on which we want to send e-mails.
Variable $subject is used for specifying subject of e-mails.
Variable $headers is used for specifying headers of e-mails.[Not required]

  1. <?php
  2. function send_mail($from_name, $from_email, $message) {
  3.  
  4.  // on which e-mail must send messages
  5.  $to = "contact@mysite.com";
  6.  
  7.  // subject of the message
  8.  $subject = "Message from Mysite.com";
  9.  
  10.  // headers for setting From: and Reply-To:
  11.  $headers .= "From: $from_name <$from_email>\n";
  12.  $headers .= "Reply-To: $from_name <$from_email>\n";
  13.  
  14.  // try to send mail
  15.  $send_mail = mail($to, $subject, $message, $headers);
  16.  
  17.  // check and return result
  18.  if ($send_mail) {
  19.   return true;
  20.  } else {
  21.   return false;
  22.  }
  23. }
  24. ?>

What happening when form is sent to web server? Here we need to check that the $send variable is set to 1 and insert PHP code which execute some e-mail function. And now let’s see how will look final code of our e-mail form

  1. <?php
  2. // our e-mail function
  3. function send_mail($from_name, $from_email, $message) {
  4.  $to = "contact@mysite.com";
  5.  $subject = "Message from Mysite.com";
  6.  $headers .= "From: $from_name <$from_email>\n";
  7.  $headers .= "Reply-To: $from_name <$from_email>\n";
  8.  $send_mail = mail($to, $subject, $message, $headers);
  9.  if ($send_mail) {
  10.   return true;
  11.  } else {
  12.   return false;
  13.  }
  14. }
  15.  
  16. // check that the form is truly sent
  17. if ($_POST['send'] == 1) {
  18.  // get data from the form
  19.  $from_name=$_POST['from_name'];
  20.  $from_email=$_POST['from_email'];
  21.  $message=$_POST['message'];
  22.  
  23.  // send e-mail
  24.  $try_mail = send_mail($from_name, $from_email, $message);
  25.  
  26.  // output the result
  27.  if ($try_mail) {
  28.   echo "Your e-mail message was sent!";
  29.  } else {
  30.   echo "Your e-mail message was not sent!";
  31.  }
  32. }
  33. ?>
  34. <html>
  35. <head>
  36. <title>Sending e-mails with PHP mail() function</title>
  37. </head>
  38. <body>
  39. <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  40.     <input type="text" name="from_name">
  41.     <input type="text" name="from_email">
  42.     <textarea name="message"></text>
  43.     <input type="hidden" name="send" value="1">
  44.     <input type="submit" value="Send">
  45. </form>
  46. </body>
  47. </html>
VN:F [1.9.18_1163]
Rating: 0.0/10 (0 votes cast)

11 Responses to “Sending e-mails with PHP mail() function”

Leave a Reply