How do I make an application form in PHP that sends an e-mail using the mail()
function?
PHP mailing at its simplest.
<?php
$message = "This is my e-mail message.";
mail('recipient@example.com', 'Message subject', $message);
?>
The PEAR packages Mail
and Mail_Mime
have a lot of useful features for sending mail, as well. Take the user's input, build a message, and send it along. Beyond that, we'll need to know what you're having trouble with.
A very simple contact form may look like this. Note that this is simply for a demonstration. I wouldn't suggest using this as-is:
<?php
// If our form has been submitted
if ($_POST["submit"]) {
// Gather up some values
$name = $_POST["username"];
$msg = $_POST["message"];
$errors = false;
// If the name and message aren't empty
if (!empty($name) && !empty($msg)) {
// Email them to ourselves
mail("me@mydomain.com", "Website Email", "{$name} says: {$msg}");
} else { // If they are empty
// Set an error message
$errors = "Please fill out the form.";
}
// If we have errors as this point, show them
if ($errors) print "<p>".$errors."</p>";
}
?>
<form method="post">
<p>Name: <input type="text" name="username" /></p>
<p>Message: <textarea name="message"></textarea></p>
<p><input type="submit" name="submit" /></p>
</form>
Tak a look at the http://swiftmailer.org/, using mail() function without proper data escaping could be very vulnerable for attacks and custom headers injection.
精彩评论