Reputation: 10129
My website is HTML5. Consequently, my files are .html
. I have a contact.html
file that I would like to use to send a message from, using PHP. I don't have much experience with PHP (so if anyone could recommend a better alternative, non-.NET way of sending email, please let me know).
My initial thought was to include my PHP code inside my HTML file (whether or not this is possible or even recommended, I don't know). I've done this once before, and I believe I remember having a form
tag that somewhere in its attributes specified the .php
file that I used to send the email.
Something like <form someattribute="sendmail.php"> ... </form>
.
QUESTION: Given what I THINK I should do (above), is this the best approach (specifying the PHP file inside my form tag), or do you recommend a better way to send email from a raw .html
file?
Upvotes: 0
Views: 1329
Reputation: 6230
http://php.net/manual/en/function.mail.php
mail.html
<form action="mail.php" method="post">
To <input type="text" name="to"/><br/>
Subject <input type="text" name="subject"/><br/>
Message <textarea name="message"></textarea><br/>
<input type="submit" value="Send"/>
</form>
mail.php
<?php
mail($_POST["to"] , $_POST["subject"], $_POST["message"]);
header("Location: mail.html"); //redirect the user
?>
Upvotes: 2
Reputation: 3581
You cannot do that only with HTML. If you stick to PHP solution, try
<?php
if(isset($_POST['send'])) //check the submit button was pressed
{
//get variables from POST array. Remember we specified POST method
$to = $_POST['to'];
$subject = $_POST['subject'];
$message = $_POST['message'];
//set up headers
$headers = 'From: [email protected]' . "\r\n" .
'Reply-To: [email protected]' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
//send the email and save the result
$result = mail($to, $subject, $message, $headers);
//was it sent?
if($result)
{
echo "Successfuly sent the email";
}
else
{
echo "An error has occured";
}
}
?>
<hr>
<form method="POST">
To: <input type="text" name="to"> <br>
Subject: <input type="text" name="subject"> <br>
Text: <textarea name="message"></textarea><br>
<input type="submit" value="Send" name="send">
</form>
You do not need to specify where the form points to because it is the same file. Otherwise it would be
<form action="somefile.php" method="POST">
Altough you have to specify the method POST, otherwise all the data will be sent through GET by default
PHP has a mail function that is used to send the email http://php.net/manual/en/function.mail.php
Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.
We check if the email is sent or not and print a corresponding message. Then, regardless of the result, we print out the message form.
Upvotes: 5
Reputation: 72
If you're just trying to send the form info via e-mail it's fairly simple.
<form action="sendmail.php">
just need to make sure you're coding your php file correctly.
Upvotes: 2
Reputation: 2269
You can easily send the mail by posting the data into a php file. Just need to write some codein that php file and in form user action='phpfilename.php'. Thats it.
Upvotes: 2
Reputation: 3235
HTML is only client side, and is just markup so it cannot send an email. You should have a form that posts to a PHP page, as you suggest, and that PHP page sends the email.
http://www.w3schools.com/php/php_forms.asp http://www.w3schools.com/php/php_mail.asp
Upvotes: 1