PHP 邮件/验证脚本不起作用

发布于 2024-12-05 02:05:24 字数 1120 浏览 0 评论 0原文

如果输入了正确的电子邮件地址,则以下表单会显示消息“感谢您订阅新闻通讯”,但不会将结果发送到指定的 $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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

離殇 2024-12-12 02:05:24

摘自 mail() 手册页:

如果邮件被成功接受投递,则返回 TRUE,否则返回 FALSE。

需要注意的是,仅仅因为邮件被接受投递,并不意味着邮件实际上会到达预期目的地

所以我认为你的代码没问题,但你的邮件服务器有问题。

Excerpt from mail() manual page:

Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.

It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination.

So I suppose your code is fine but there is a problem with your mail server.

木槿暧夏七纪年 2024-12-12 02:05:24

您在本地服务器上进行开发吗?是否安装了 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.

撩心不撩汉 2024-12-12 02:05:24

您应该在 body close 标签之前添加代码,应该首先加载 DOM 来执行您的 js 代码
或者尝试

if(1==1){
    echo "<script>alert('Test');</script>";
}

如果警报不起作用,问题出在代码的位置

You should add the code before body close tag, The DOM should be loaded first to execute your js code
Or try

if(1==1){
    echo "<script>alert('Test');</script>";
}

If alert is not working the problem is the position of your code

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文