PHP 电子邮件表单不起作用
我正在使用下面的表格。我正在使用 contact.php 页面显示表单并提交表单。该表格验证并显示电子邮件已发送,但当我检查电子邮件时它没有显示。
PHP:
<?php
function spamcheck($field)
{
//filter_var() sanitizes the e-mail
//address using FILTER_SANITIZE_EMAIL
$field=filter_var($field, FILTER_SANITIZE_EMAIL);
//filter_var() validates the e-mail
//address using FILTER_VALIDATE_EMAIL
if(filter_var($field, FILTER_VALIDATE_EMAIL))
{
return TRUE;
}
else
{
return FALSE;
}
}
if (isset($_REQUEST['email']))
{//if "email" is filled out, proceed
//check if the email address is invalid
$mailcheck = spamcheck($_REQUEST['email']);
if ($mailcheck==FALSE)
{
echo "Invalid input";
}
else
{//send email
$email = $_REQUEST['email'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
mail("[email protected]", "Subject: $subject",
$message, "From: $email" );
echo "Thank you for using our mail form";
}
}
else
{//if "email" is not filled out, display the form
echo "<form method='post' action='contact.php'>
Email: <input name='email' type='text' /><br />
Subject: <input name='subject' type='text' /><br />
Message:<br />
<textarea name='message' rows='15' cols='40'>
</textarea><br />
<input type='submit' />
</form>";
}
?>
I am using the form below. I am using the page, contact.php to display the form and submit the form. The form validates and says email sent but when i check the email it doesn't show.
PHP:
<?php
function spamcheck($field)
{
//filter_var() sanitizes the e-mail
//address using FILTER_SANITIZE_EMAIL
$field=filter_var($field, FILTER_SANITIZE_EMAIL);
//filter_var() validates the e-mail
//address using FILTER_VALIDATE_EMAIL
if(filter_var($field, FILTER_VALIDATE_EMAIL))
{
return TRUE;
}
else
{
return FALSE;
}
}
if (isset($_REQUEST['email']))
{//if "email" is filled out, proceed
//check if the email address is invalid
$mailcheck = spamcheck($_REQUEST['email']);
if ($mailcheck==FALSE)
{
echo "Invalid input";
}
else
{//send email
$email = $_REQUEST['email'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
mail("[email protected]", "Subject: $subject",
$message, "From: $email" );
echo "Thank you for using our mail form";
}
}
else
{//if "email" is not filled out, display the form
echo "<form method='post' action='contact.php'>
Email: <input name='email' type='text' /><br />
Subject: <input name='subject' type='text' /><br />
Message:<br />
<textarea name='message' rows='15' cols='40'>
</textarea><br />
<input type='submit' />
</form>";
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在您的表单中,当显示“谢谢”消息时,这意味着邮件功能已运行,而不是成功。
php 邮件函数在成功时返回 true,否则返回 false。
您能否尝试以下操作:
php.ini 中是否允许使用邮件功能?
In your form when the 'thank you' message displays it means the mail function has been run, not that it succeeded.
the php mail function returns true when it succeeds and othwerwise false.
Could you please try something like:
also is the mail function allowed in the php.ini?
检查你的 php 日志。也许它确实失败了,因为它没有在您的应用程序或 PHP 中配置要在尝试发送邮件时使用的 SMTP 客户端。
Check your php log. Maybe it does fail because it does not have a SMTP client configured neither in your app or in PHP to be used when trying to send mails.
尝试从您联系人中的地址“发送”邮件。您确定您的电子邮件帐户接受这些欺骗性电子邮件吗? Gmail 会将那些来自不在您的联系人列表中的地址的 php 电子邮件发送到垃圾邮件。
尝试在不同的电子邮件帐户上进行测试。值得一试。
您可以运行的另一个测试是使用警报来检索 mail() 返回,以检查它是 true 还是 false。
Try to send mail "from" an address you have on your contacts. Are you sure your email account accepts these spoofed emails? Gmail sends those php emails from addresses not in your contact list to spam.
Try to test on a different email account. It's worth the shot.
Another test you can run is using an alert to retrieve the mail() return, to check whether it comes true or false..