问题
I'm trying to create a simple email form for a client and cannot get the <textarea>
to send as a message. I've tried everything I have found so far and nothing is working properly...
Here is my code:
<h4>Email Al</h4>
<table>
<form name="contactForm" id="contact" action="send_form_email.php" method="post">
<tr>
<td>
<label>Name:</label>
<input type="text" name="name">
</td>
<td>
<label>Email Address:</label>
<input type="email" name="email">
</td>
</tr>
<tr>
<td colspan="2">
<label>Website (if Available):</label>
<input type="text" name="website">
</td>
</tr>
<tr>
<td colspan="2">
<label>Your Message:</label></br>
<textarea for="" name="information" width="100%" rows="10" form="contactForm"></textarea>
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Send Message">
</td>
</tr>
</form>
</table>
That is the form in the index.php file, below is the email send-email.php
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$website = $_POST['website'];
$message = $_POST['information'];
$email_from = 'myemail@gmail.com';
$email_subject = $name . " has sent you a message!";
$email_body = "You have received a new message from " . $name . " via your website!\n".
"Name:\n $name\n".
"Email:\n $email\n".
"Website:\n $website\n".
"Message:\n $message\n".
$to = "clientemail@gmail.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
mail($to,$email_subject,$email_body,$headers);
function IsInjected($str)
{
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}
if(IsInjected($visitor_email))
{
echo "Bad email value!";
exit;
}
?>
<?php
header("Location: /formsubmitted.php"); /* Redirect browser */
exit();
?>
If anyone has any clue why this is malfunctioning I would love to know as well! This is one of the first message/email php functions I've created myself as I usually develop for wordpress, so I would normally just install a plugin for my client.
回答1:
That's because of this line: (Consult my footnote)
"Message:\n $message\n".
You have a dot instead of a (closing) semi-colon ;
Edit: You need to remove form="contactForm"
from <textarea>
. Upon testing, it did not show in mail when used with it.
and error reporting would have told you about it.
- http://php.net/manual/en/function.error-reporting.php
Being:
Notice: Undefined index: information (line 8)
and
Notice: Undefined variable: visitor_email (line 26 and 55)
In both:
$headers .= "Reply-To: $visitor_email \r\n";
and
if(IsInjected($visitor_email))
$visitor_email
is undefined in your code. You should have used $email
instead for all instances of $visitor_email
.
Then you have for=""
which doesn't appear to be valid syntax in your <textarea...
That for=""
is ambiguous.
Ref: https://developer.mozilla.org/en/docs/Web/HTML/Element/textarea
You should also check for empty fields with a conditional !empty()
.
- http://php.net/manual/en/function.empty.php
Footnote:
After more intensive testing, and in regards to the dot that I mentioned above, I must state the following:
Strangely enough, PHP sees it as being valid syntax, as there is a semi-colon in and included at the end here $to = "clientemail@gmail.com";
What that will do is still send mail, BUT also include that in the email, which is clearly not included in the $email_body
variable body.
Therefore, the first part of my answer was right to a certain extent, the form="contactForm"
being the major culprit here.
- You may have meant to use an ID
id="contactForm"
or a classclass="contactForm"
. Both are valid to be used and will include it in your mail.
However, you won't be able to use name="contactForm"
since your <form>
holds that named attribute.
- Both the
name
andid
attributes are (meant to be) unique.
回答2:
Remove form="contactForm"
from <textarea>
. I removed it. And, it started working.
Replace
<textarea for="" name="information" width="100%" rows="10" form="contactForm"></textarea>
To
<textarea for="" name="information" width="100%" rows="10"></textarea>
Or,
According to HTML5, You can use, form
id's
in input
. So, in place of form="contactForm"
use form="contact"
.
Change
<textarea for="" name="information" width="100%" rows="10" form="contactForm"></textarea>
To
<textarea for="" name="information" width="100%" rows="10" form="contact"></textarea>
For more info, check textarea - attributes
来源:https://stackoverflow.com/questions/34829704/textarea-not-sending-via-email