How to use the PHP mail function

You can use the PHP mail() function to send an email with PHP. The simplest way to do this is to send a text email. This is one way to handle sending you the results when a visitor to your website fills out a form.

Below is the code for the basic email function. We can take the script and actually use a form on our website to set the variables in the script above to send an email.

<?php
//if "email" variable is filled out, send email
if (isset($_REQUEST['email'])) {

//Email information
$admin_email = "someone@example.com";
$email = $_REQUEST['email'];
$subject = $_REQUEST['subject'];
$comment = $_REQUEST['comment'];

//send email
mail($admin_email, "$subject", $comment, "From:" . $email);

//Email response
echo "Thank you for contacting us!";
}

//if "email" variable is not filled out, display the form
else {
?>

<form method="post">
Email: <input name="email" type="text" /><br />
Subject: <input name="subject" type="text" /><br />
Message:<br />
<textarea name="comment" rows="15" cols="40"></textarea><br />
<input type="submit" value="Submit" />
</form>

<?php
}
?>

  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

PHP 5.4 vs 5.3 differences : What to watch out for

Each major release of any framework can make someone begin to second-guess what is still...

Parsing PHP code within HTML pages

The easiest way to parse or run PHP code within an HTML page is to simply change the extension...

Change/Select PHP version

If you require a different version of PHP than which we run by default you can select which...

Ruby on Rails

For information about Ruby on Rails, visit their website.