PHP mail()函数将仅发送到特定域

时间:2019-03-22 17:44:39

标签: php email

以下代码段返回电子邮件已发送,并且确实将其发送到域@ qub.ac.uk上的任何电子邮件地址,但是不适用于任何其他电子邮件域(即@homail)。 com/@gmail.com等)。有没有人遇到过类似的问题?

<?php
include '../dbConnection.php';

$email = "";
$email_err = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {

    // Validate email
    if (empty(trim($_POST["email"]))) {
        $email_err = "Please enter an email address.";
    } elseif (!filter_var(trim($_POST["email"]), FILTER_VALIDATE_EMAIL)) {
        $email_err = "Invalid email format";
    } else {
        $email = $_POST["email"];
        $to = "$email";
        $subject = 'Added to system';
        $header = 'From: example@hotmail.com' . "\r\n" .
                'Reply-To: example@hotmail.com' . "\r\n" .
                'X-Mailer: PHP/' . phpversion();
        $message = 'You have been added to the system';
        if (mail($to, $subject, $message, $header)) {
            echo "Email Sent";
        } else {
            echo "Failure";
        }
    }
}
?>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Sign Up</title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
        <style type="text/css">
            body{ font: 14px sans-serif; }
            .wrapper{ width: 350px; padding: 20px; }
        </style>
    </head>
    <body>
        <div class="wrapper">
            <h2>Add a New Admin</h2>
            <p>Please enter the email address of the new admin</p>
            <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
                <div class="form-group <?php echo (!empty($email_err)) ? 'has-error' : ''; ?>">
                    <label>Email Address</label>
                    <input type="text" name="email" class="form-control" value="<?php echo $email; ?>">
                    <span class="help-block"><?php echo $email_err; ?></span>
                </div>
                <div class="form-group">
                    <input type="submit" class="btn btn-primary" value="Submit">
                    <input type="reset" class="btn btn-default" value="Reset">
                </div>
            </form>
        </div>    
    </body>
</html>

0 个答案:

没有答案