问题
Here's my updated code trying to get my form to send me the email. I added the server php requests around the form, though the code doesn't seem to be reading them. My code is being displayed as text on the site after pushing for the test. I remember once, I called the php in the head away from the form markup but can't remember that syntax.
Here's the code I'm trying to use:
<?php
if ($_POST["email"]<>'') {
$ToEmail = 'chaseoutt@gmail.com';
$EmailSubject = 'Site contact form ';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "Name: ".$_POST["name"]."<br>";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."<br>";
$MESSAGE_BODY .= "URL: ".$_POST["url"]."<br>";
$MESSAGE_BODY .= "Comment: ".nl2br($_POST["comment"])."<br>";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
?>
Your message was sent
<?php
} else {
?>
<p style="font-style:italic; font-size:12px; font-weigh: normal; margin-top: -89px; margin-left: 33px;">Contact me written in a different language.</p> <img src="http://www.cameroncashwell.com/imgs/pointing-left.png" style="float: right; margin-right: 140px; margin-top: -89px;">
<div class="form-div">
<form id="fvujq-form1" style="font-size:22px; color:#333;" method="post" action="">
<div class="form-row"><span class="label">Name *</span><input type="text" name="name" /></div>
<div class="form-row"><span class="label">Email *</span><input type="text" name="email" /></div>
<div class="form-row"><span class="label">URL</span><input type="text" name="url" /></div>
<div class="form-row"><span class="label">Comment *</span><textarea name="comment"></textarea></div>
<div class="form-row"><input class="submit" type="submit" value="Submit"></div>
</form>
</div>
<?php
};
?>
Wheres my error?
回答1:
Email is not sent by JavaScript code in the client; it is sent from the server. When the user hits submit, and all the client side validations have passed, the form data is POST'ed to the server. The form element's "action" attribute specifies what URL on the server should receive the POST'ed form data; i.e. something like action="send_email.php" or something like that.
How the email is actually generated, on the server is entirely dependent on the server technology in use, e.g. PHP, or JSP, or whatever.
So two things are missing in your code above:
- the value for action= in the form, and
- the server-side code (PHP file, or whatever) that would receive the data and actually send out an email (corresponding to #1)
回答2:
And to add to @smendola's answer...
Once you have sorted the server-side email sending you might still not be getting the email you're expecting. Some email hosts (quite a few actually) check the validity of signatures of the sending server. If that fails (i.e. no signature or only self-signed signatures in place) the email host might reject the email as spam.
One way to avoid this is to send the form data via SMTP rather than PHP's native send() function. Easiest solution would be PHPMAILER. For example you could send the stuff via an Gmail account.
来源:https://stackoverflow.com/questions/8467486/how-to-make-my-contact-form-send-me-email