我可以获得一些电子邮件帮助吗?
I've added an email form to a bootstrap site and the emails are being sent and received but the content is stripped. The emails arrive with no message, name, email address etc. I'm an absolute newbie, so I'm assuming there is a problem with the script. Will you guys please be kind enough to look at this script and tell me what I'm missing? Thanks.
<?php
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>'Email sent!'
);
$name = @trim(stripslashes($_POST['name']));
$email = @trim(stripslashes($_POST['email']));
$subject = @trim(stripslashes($_POST['subject']));
$message = @trim(stripslashes($_POST['message']));
$email_from = $email;
$email_to = 'david@myemail.com';
$body = 'Name: ' . $name . "
" . 'Email: ' . $email . "
" . 'Subject: ' . $subject . "
" . 'Message: ' . $message;
$success = @mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
echo json_encode($status);
die;
Here's the HTML
<div class="col-sm-8">
<h4>Contact Form</h4><br><br>
<div class="status alert alert-success" style="display: none"></div>
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendemail.php" role="form">
<div class="row">
<div class="col-sm-5">
<div class="form-group">
<input type="text" class="form-control" required placeholder="First Name">
</div>
<div class="form-group">
<input type="text" class="form-control" required placeholder="Last Name">
</div>
<div class="form-group">
<input type="text" class="form-control" required placeholder="Email address">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary btn-lg">Send Message</button>
</div>
</div>
<div class="col-sm-7">
<textarea name="message" id="message" required class="form-control" rows="8" placeholder="Message"></textarea>
</div>
</div>
</form>
</div><!--/.col-sm-8-->
First things first, you need to escape your php tag after the die statement, otherwise nothing will work as php thinks you have crazy syntax errors (the html) because you never closed php.
Anyway, I carefully substituted your php with a form I have on another website and it seems to be sending email just fine. Which leads me to believe the problem is largely in your markup.
Looking closely at your markup I see that only your form and textarea elements have a name attribute. @Sean is correct in that each input needs to have the name attribute set or you will not be able to access it with PHP.
When you write the statement $name = $_POST['name'];
you are saying "take the value from an input element with a name attribute equal to 'name' and store its value in the php variable $name
". The reason your email function is working but not passing form data is because your inputs are missing the required name attributes. If you do a
var_dump $name; die;
you will see there is no value inside of $name. Therefore, the mail() function isn't going to work either.
So compare
<input type="text" class="form-control" required placeholder="First Name">
<input type="text" class="form-control" required placeholder="Last Name">
<input type="text" class="form-control" required placeholder="Email address">
with
<input name="first_name" type="text" class="form-control" required placeholder="First Name">
<input name="last_name" type="text" class="form-control" required placeholder="Last Name">
<input name="email_address" type="text" class="form-control" required placeholder="Email address">
Now that we've given our inputs name attributes and php has a way of referencing them, we can say:
$name = @trim(stripslashes($_POST['first_name']));
$email = @trim(stripslashes($_POST['last_name']));
$subject = @trim(stripslashes($_POST['email_address']));
Take a moment to ensure each of the $_POST calls have their corresponding input's name attribute. Doesn't matter what you name them, as long as they match.
Once you do that, you should be seeing the correct values being passed in the email. A couple other small takeaways are to remove the @ symbol before your functions as @Raptor mentioned. These will not break your script, but they leave you blind in the event that there is an error. The @ sign means "suppress error messages", so it works basically the same without them:
$name = trim(stripslashes($_POST['first_name']));
$email = trim(stripslashes($_POST['last_name']));
$subject = trim(stripslashes($_POST['email_address']));
What would be better is to carefully handle the error, something like this
try {
$success = mail($to, $company_name, $body, 'From: <'.$email.'>');
} catch (Exception $e) {
echo 'Exception: ' . $e->getMessage();
}
But that's another subject.
Also as @Raptor alluded to, you're not really getting any benefit from your $status array because you are not updating it to reflect the result of whether or not the email is being sent. Since it's not necessary to make this script work, I'd say leave that part out for now.
Also consider using php's heredoc function for creating the email's body contents. It adds readability in both your source code as well as the generated email message. Now you can see how simple it actually is to use the php mail function. Here is a stripped down version of what your php is doing, so you can see the main handles.
<?php
// handle form submission
$emailSubject = 'Entry from your contact form';
$to = 'david@myemail.com';
$name = $_POST['full_name'];
$phone = $_POST['phone_number'];
$email = $_POST['email'];
$comments = $_POST['comments'];
$body = <<<EOD
<br><hr><br>
Full Name: $name <br>
Phone: $phone <br>
Email: $email <br>
Message from user: $comments <br>
EOD;
$headers = "From: $email
";
$headers .= "Content-type: text/html
";
$success = mail($to, $emailSubject, $body, $headers);
?>
Hopefully this helps you fix your problem, let me know if I missed anything. Good luck!