How do I make an application form in PHP that sends an e-mail using the mail()
function?
views:
64answers:
3
+2
A:
PHP mailing at its simplest.
<?php
$message = "This is my e-mail message.";
mail('[email protected]', '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.
ceejayoz
2010-02-16 17:27:42
+1
A:
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("[email protected]", "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>
Jonathan Sampson
2010-02-16 17:35:02
+1
A:
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.
Ziells
2010-02-16 18:27:31