我的网站上有一个表单,使用PHP脚本将消息通过电子邮件发送到我的Gmail。每次我测试它,它工作,我收到电子邮件。但我还有其他几个人在测试表格 - 而且电子邮件从未通过。我是否需要配置SMTP服务器?我已经阅读了一些文章 - 但对于如何使代码适应我当前的PHP文件而感到困惑。任何帮助非常感谢!
FORM.PHP
<? php
// Define some constants
define("RECIPIENT_NAME", "MY NAME");
define("RECIPIENT_EMAIL", "MY GMAIL ADDRESS");
define("EMAIL_SUBJECT", "Website Enquiry");
$full_name;
$email;
$subject;
$message;
$captcha;
if (isset($_POST['name'])) {
$name = $_POST['name'];
if ($name != filter_var($name, FILTER_SANITIZE_STRING)) {
echo "Name is not valid!";
exit; // or exit w/e you please
}
} else {
echo "Name field is required!";
exit; // or exit w/e you please
}
if (isset($_POST['email'])) {
$email = $_POST['email'];
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "Email is not valid!";
exit; // or exit w/e you please
}
} else {
echo "Email field is required!";
exit; // or exit w/e you please
}
if (isset($_POST['message'])) {
$message = $_POST['message'];
if ($message != filter_var($message, FILTER_SANITIZE_STRING)) {
echo "Message is not valid!";
exit; // or exit w/e you please
}
} else {
echo "Message field is required!";
exit; // or exit w/e you please
}
if (isset($_POST['g-recaptcha-response'])) {
$captcha = $_POST['g-recaptcha-response'];
}
if (!$captcha) {
echo 'error';
exit;
}
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?INSERT GOOGLE VERIFICATION KEY&response=".$captcha.
"&remoteip=".$_SERVER['REMOTE_ADDR']);
if ($response.success == false) {
echo 'fail';
// No need to exit() here, code ends after this anyway
} else {
// If all values exist, send the email
if ($name && $email && $message) {
$recipient = RECIPIENT_NAME.
" <".RECIPIENT_EMAIL.
">";
$headers = "From: ".$name.
" <".$email.
">";
$success = mail($recipient, EMAIL_SUBJECT, $message, $headers);
echo "success";
} else {
echo "Name, email and message are required!";
}
// No need to exit() here, code ends after this anyway
} ?>
/*HTML*/
<p><b>CONTACT FORM</b>
</p>
<form role="form" id="form" action="form.php" method="post">
<div class="form-group">
<label for="name">Your Name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Enter name">
<span class="help-block" style="display: none;">Please enter your name.</span>
<label for="email">Email address</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Enter email">
<span class="help-block" style="display: none;">Please enter a valid e-mail address.</span>
<label for="message">Message</label>
<textarea class="form-control" rows="7" id="message" name="message" placeholder="Enter message"></textarea>
<span class="help-block" style="display: none;">Please enter a message.</span>
</div>
<div class="form-group">
<div class="g-recaptcha" data-theme="dark" data-sitekey="INSERT DATA SITE KEY HERE"></div>
<span class="help-block" style="display: none;">Please check that you are not a robot.</span>
</div>
<button type="submit" id="Submit" data-loading-text="Sending..." class="btn btn-default">Send Message</button>
</form>
</div>
我解决了问题是$ email。如果用户使用某些电子邮件,如@ gmail.com或@ hotmail.com,则不会发送电子邮件。因此,我调整了我的PHP以使用我的虚拟主机电子邮件作为“FROM”地址 - 并在电子邮件的消息部分中发布了NAME,EMAIL和MESSAGE。我现在收到的电子邮件无论用户的电子邮件地址如何。这是修改后的PHP:
<?php
// Define some constants
define( "RECIPIENT_NAME", "INSERT NAME" );
define( "RECIPIENT_EMAIL", "INSERT MY GMAIL" );
define( "EMAIL_SUBJECT", "Website Enquiry" );
define( "SENT_FROM", "Pauls Website" );
define( "SENT_EMAIL", "INSERT MY WEBHOST EMAIL" );
$full_name;$email;$subject;$message;$captcha;
if(isset($_POST['name']))
{
$name=$_POST['name'];
if($name = htmlspecialchars($name, ENT_QUOTES));
if($name != filter_var($name, FILTER_SANITIZE_STRING))
{
echo "Name is not valid!";
exit; // or exit w/e you please
}
}
else
{
echo "Name field is required!";
exit; // or exit w/e you please
}
if(isset($_POST['email']))
{
$email=$_POST['email'];
if($email = htmlspecialchars($email, ENT_QUOTES));
if(! filter_var($email, FILTER_VALIDATE_EMAIL))
{
echo "Email is not valid!";
exit; // or exit w/e you please
}
}
else
{
echo "Email field is required!";
exit; // or exit w/e you please
}
if(isset($_POST['message']))
{
$message =$_POST['message'];
if($message = htmlspecialchars($message, ENT_QUOTES));
if($message != filter_var($message, FILTER_SANITIZE_STRING))
{
echo "Message is not valid!";
exit; // or exit w/e you please
}
}
else
{
echo "Message field is required!";
exit; // or exit w/e you please
}
$body = "<b>Name:</b> " .$name. "<br/><b>Email:</b> " .$email. "<br/><b>Message:</b> " .$_POST["message"];
if(isset($_POST['g-recaptcha-response']))
{
$captcha=$_POST['g-recaptcha-response'];
}
if(!$captcha)
{
echo 'error';
exit;
}
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?INSERT SECRET CODE&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
if($response.success==false)
{
echo 'fail';
// No need to exit() here, code ends after this anyway
}
else
{
// If all values exist, send the email
if ( $name && $email && $message )
{
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: " . SENT_FROM . " <" . SENT_EMAIL . ">";
$recipient = RECIPIENT_NAME . " <" . RECIPIENT_EMAIL . ">";
$success = mail( $recipient, EMAIL_SUBJECT, $body, $headers );
echo "success";
}
else
{
echo "Name, email and message are required!";
}
// No need to exit() here, code ends after this anyway
}
?>
我还添加了一些代码,以便在表单中接受撇号,并在收到的电子邮件中正确显示。只是试图找出如何实现nl2br(),以便消息包含任何新行。目前它将消息截断为一行,即使用户有多个段落的新行。