1

只是对这个 PHP 电子邮件表单有一点帮助,我在这里检查了其他问题,但还没有找到解决方案。

我的网页上有一个非常简单的 PHP 电子邮件订阅表单,但它不发送邮件。认为这可能是我的 php.ini 的问题,但这一切似乎都很好。只是想让有人看看这里的代码,看看我是不是傻!

<?php
$email_to = "subscribe@roomsby.com";
$success_message = "Thank you for subscribing to Roomsby.com. We will get back to you with details of our launch very soon!"; 
$site_name = "www.roomsby.com"; 

$email = trim($_POST['email']);
$submitted = $_POST['submitted'];

if(isset($submitted)){
    if($email === '' || $email === 'Enter your email address' ) {
        $email_empty = true;
        $error = true;
    } elseif (!preg_match("/^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$/i", $email)){
        $email_unvalid = true;
        $error = true;  
    }
}

if(isset($error)){
        echo '<span class="error_notice"><ul>';
        if($email_empty){
            echo '<li>Please enter your email address</li>';
        } elseif ($email_unvalid) {
            echo '<li>Please enter a valid email address</li>'; 
        } else {
            echo '<li>An error has occurred while sending your message. Please try again later.</li>';
        }
        echo "</ul></span>";
}

if(!isset($error)){
        $subject = 'Newsletter Submission';
        $body = "Email: $email";
        $headers = 'From: ' . $site_name . ' <' . $emailTo . '> ' . "\r\n";
        $headers .= 'Reply-To: ' . $email;
        mail($email_to, $subject, $body, $headers);

        echo '<span class="success_notice">' . $success_message . '</span>';
    }
?>
4

1 回答 1

1

isset()True只要变量不是就会返回NULL,这意味着即使变量是Falseisset()也会返回True

所以,考虑到这一点 - 试试这个版本:

$error = FALSE; # assume no errors.

if(isset($submitted)){
    if($email === '' || $email === 'Enter your email address' ) {
        $email_empty = true;
        $error = TRUE;
    } elseif (!preg_match("/^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$/i", $email)){
        $email_unvalid = true;
        $error = true;  
    }
}

if ($error) {

   echo '<span class="error_notice"><ul>';
        if($email_empty){
            echo '<li>Please enter your email address</li>';
        } elseif ($email_unvalid) {
            echo '<li>Please enter a valid email address</li>'; 
        } else {
            echo '<li>An error has occurred while sending your message. Please try again later.</li>';
        }
        echo "</ul></span>";
} else {

        $subject = 'Newsletter Submission';
        $body = "Email: $email";
        $headers = 'From: ' . $site_name . ' <' . $emailTo . '> ' . "\r\n";
        $headers .= 'Reply-To: ' . $email;
        mail($email_to, $subject, $body, $headers);

        echo '<span class="success_notice">' . $success_message . '</span>';
}
于 2013-06-05T10:23:13.767 回答