Networking, Programming and Graphics - Tutorials
ONLINEHOWTO.net Tutorials Category

Sending e-mails with PHP mail() function

Type: Code Networking, Programming and Graphics - Tutorials
Networking, Programming and Graphics - Tutorials
Level: Beginner Networking, Programming and Graphics - Tutorials 
Networking, Programming and Graphics - Tutorials
Date: 2006-Dec-05
Networking, Programming and Graphics - Tutorials
Visited: 20984 times
Networking, Programming and Graphics - Tutorials
Rating: Networking, Programming and Graphics - Tutorials
Networking, Programming and Graphics - Tutorials
Published: 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']
<html>
<head>
<title>Sending e-mails with PHP mail() function</title>
</head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <input type="text" name="from_name">
    <input type="text" name="from_email">
    <textarea name="message"></text>
    <input type="hidden" name="Send" value="1">
    <input type="submit" value="Send">
</form>
</body>
</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!
<?php
$send
=$_POST['send'];
$from_name=$_POST['from_name'];
$from_email=$_POST['from_email'];
$message=$_POST['message'];
?>
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:
<?php
$send_mail 
mail($to$subject$message$headers);
?>
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]
<?php
function send_mail($from_name$from_email$message) {

    
// on which e-mail must send messages
    
$to "contact@mysite.com";

    
// subject of the message
    
$subject "Message from Mysite.com";

    
// headers for setting From: and Reply-To:
    
$headers .= "From: $from_name <$from_email>\n";
    
$headers .= "Reply-To: $from_name <$from_email>\n";

    
// try to send mail
    
$send_mail mail($to$subject$message$headers);

    
// check and return result
    
if ($send_mail) {
        return 
true;
    } else {
        return 
false;
    }
}
?>
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
<?php
// our e-mail function
function send_mail($from_name$from_email$message) {
    
$to "contact@mysite.com";
    
$subject "Message from Mysite.com";
    
$headers .= "From: $from_name <$from_email>\n";
    
$headers .= "Reply-To: $from_name <$from_email>\n";
    
$send_mail mail($to$subject$message$headers);
    if (
$send_mail) {
        return 
true;
    } else {
        return 
false;
    }
}

// check that the form is truly sent
if ($_POST['send'] == 1) {
    
// get data from the form
    
$from_name=$_POST['from_name'];
    
$from_email=$_POST['from_email'];
    
$message=$_POST['message'];

    
// send e-mail
    
$try_mail send_mail($from_name$from_email$message);

    
// output the result
    
if ($try_mail) {
        echo 
"Your e-mail message was sent!";
    } else {
        echo 
"Your e-mail message was not sent!";
    }
}
?>
<html>
<head>
<title>Sending e-mails with PHP mail() function</title>
</head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <input type="text" name="from_name">
    <input type="text" name="from_email">
    <textarea name="message"></text>
    <input type="hidden" name="send" value="1">
    <input type="submit" value="Send">
</form>
</body>
</html>
Rate this tutorial:                    
Post Comment

    • ( prafulsavaliya86 [ at ] gmail . com ),

      posted on 2009-Jul-12 | 11:28:09 PM
      hello i am trying to reach their
    • ( danphiekiel [ at ] yahoo . com . ph ),

      posted on 2009-Jul-20 | 10:47:47 PM
      simple
    • ( johnday [ at ] markaet . com ),

      posted on 2010-Feb-19 | 10:28:39 AM
      Testin if it works...?

    • posted on 2010-Feb-19 | 12:25:21 PM
      Is there a problem with the script?
    • ( and1_g_o [ at ] yahoo . com ),

      posted on 2010-Apr-16 | 10:03:58 AM
      have problem sending mail to yahoo mail.. with others its worked

    • posted on 2010-Apr-17 | 02:28:48 AM
      This would be because yahoo blocks the traffic from your server.
Need a specific tutorial? Do not hesitate and submit a request!
Your e-mail: