Networking, Programming and Graphics Tutorials

How to Send Email from a PHP Script [3/3]

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:                    
Networking, Programming and Graphics Tutorials - How to Send Email from a PHP Script [3/3] - Networking, Programming and Graphics Tutorials

Need a specific tutorial? Do not hesitate and submit a request!
Related Tags: email send button php script  how to send email php  send email to many with php  send email to many php  php email send value in message  send email to many people with php  send content value+php email function  sending photo via php email  php email "on specific date"  general email troubleshooting