PHP 邮件/验证脚本不起作用
如果输入了正确的电子邮件地址,则以下表单会显示消息“感谢您订阅新闻通讯”,但不会将结果发送到指定的 $to
地址。另外,如果电子邮件无效,则不会显示“请输入有效的电子邮件”消息。有人可以帮助 php 菜鸟解决这些错误吗?谢谢
<?php
$email = $_POST['email'];
$to = "[email protected]";
$subject = "ADD THIS EMAIL ADDRESS TO THE MAILING LIST";
$body = "\n\n";
$url = 'http://10.0.1.3/~143b1';
if (! filter_var($email, FILTER_VALIDATE_EMAIL))
{
echo '<script> alert("PLEASE ENTER A VALID EMAIL ADDRESS") </script>';
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
}
else
{
if (mail($to, $subject, $body))
{
echo '<script> alert("THANK YOU FOR SUBSCRIBING TO THE NEWSLETTER") </script>';
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
}
else
{
echo '<script> alert("THERE WAS AN UNEXPECTED ERROR. PLEASE TRY AGAIN LATER") </script>';
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
}
}
?>
The following form gives the message "thank you for subscribing to the newsletter" if a correct email address is entered but it does not send the results to the designated $to
address. Also If the email is invalid the 'please enter a valid email' message does not show. Could someone please help a php noob with these errors? Thanks
<?php
$email = $_POST['email'];
$to = "[email protected]";
$subject = "ADD THIS EMAIL ADDRESS TO THE MAILING LIST";
$body = "\n\n";
$url = 'http://10.0.1.3/~143b1';
if (! filter_var($email, FILTER_VALIDATE_EMAIL))
{
echo '<script> alert("PLEASE ENTER A VALID EMAIL ADDRESS") </script>';
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
}
else
{
if (mail($to, $subject, $body))
{
echo '<script> alert("THANK YOU FOR SUBSCRIBING TO THE NEWSLETTER") </script>';
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
}
else
{
echo '<script> alert("THERE WAS AN UNEXPECTED ERROR. PLEASE TRY AGAIN LATER") </script>';
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
}
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
摘自 mail() 手册页:
所以我认为你的代码没问题,但你的邮件服务器有问题。
Excerpt from mail() manual page:
So I suppose your code is fine but there is a problem with your mail server.
您在本地服务器上进行开发吗?是否安装了 sendmail 或某种 SMTP 服务?
您还没有列出脚本标记的脚本类型,这可能会导致某些浏览器不执行 Javascript。当您运行 PHP 时,页面实际上会回显什么内容?这应该可以告诉您问题出在哪里。
Are you developing on a local server? Is sendmail or some kind of SMTP service installed?
You also haven't listed a script type for your script tags which might cause some browsers not to execute the Javascript. What actually gets echoed to the page when you run the PHP? That should give you an indication of where the problem is.
您应该在 body close 标签之前添加代码,应该首先加载 DOM 来执行您的 js 代码
或者尝试
如果警报不起作用,问题出在代码的位置
You should add the code before body close tag, The DOM should be loaded first to execute your js code
Or try
If alert is not working the problem is the position of your code