仅允许来自特定域/子域的电子邮件注册并发送验证邮件

时间:2014-01-23 01:58:42

标签: javascript jquery html css forms

有人可以告诉我如何只允许来自特定域/子域的电子邮件在表单中提交电子邮件。提交后,我希望将电子邮件发送到用户的电子邮件和我自己的电子邮件。我不确定如何整合它,所以这主要是我需要帮助的。

1 个答案:

答案 0 :(得分:1)

要设置仅接受某些域的电子邮件地址的表单,您可以执行以下两项操作。

第一种是使用HTML5输入type="email"属性,该属性可用于确保支持HTML5的任何浏览器都可以验证电子邮件至少是有效的。

HTML文件:

<form id="contact-form">
    <!-- Other inputs here -->
    <input name="user-email" type="email" id="user-email" placeholder="johnny.appleseed@gmail.com" required>
    <!-- make sure this name="" attribute matches what you ask for in the PHP -->
    <button type="submit" id="form-submit">Submit!</button>
</form>

然后您还可以使用JavaScript来检查域名。一旦您知道电子邮件有效且符合您的约束条件,您将需要设置一个AJAX POST,将数据发送到服务器以处理发送电子邮件。这部分可以这样完成:

JavaScript文件:

$('#form-submit').on('click', function(e) {
    e.preventDefault(); // prevent the button from refreshing the page
    var userEmail = $('#user-email');

    if (userEmail.val().indexOf('.com') !== -1) { // validation
        // xyz has .com in it
    } else if (userEmail.val().indexOf('.org') !== -1) { // validation
        // xyz has .org in it      
    } else {
        $('#submission-info').text('Whoops, we don't send emails, to anything other than .org or .com, please enter a different email and try again.');
    }
    // this doesn't mean .com or .org is at the end
    // you may want to check that by using a regular expression if necessary        


    var formData = $('#contact-form').serialize(); // This gets the <form id="contact-form"> element's values and serializes it into a string.

    $.ajax({
        url: 'mail.php', // make sure this file is either in the same directory or the path is changed
        type: 'POST',
        data: formData
    }).done(function(response) {
        // do stuff to to show the user that the form was submitted
        $('#submission-info').text('Success, your information has been sent to us (and the NSA of course) and we will reply to you as soon as possible.');
    }).fail(function(response, error) {
        // tell the user what happened that caused the form submission to fail
        $('#submission-info').text('Oh no, something happened. Maybe try again');
    });

});

有关序列化的详细信息,请查看jQuery's API page

然后在mail.php中,您可以通过执行以下操作在PHP中处理它:

<?php
$firstname = $_POST['firstName'];
$lastname = $_POST['lastName'];
$email = $_POST['email'];
$comment = $_POST['comment'];

$formcontent = "From: $firstname $lastname \n Email address: $email \n Message: $message";
$recipient = "john.doe@gmail.com"; // your email goes here instead
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
?>

如果您使用PHPMailer之类的东西,那么您可以做各种各样的事情,它会更直观,更清晰,并提供更多您可以配置的选项。

对于mail.php文件,使用PHPMailer看起来像这样:

<?php

require_once("class.phpmailer.php"); // this path should also be set up properly

if ($_POST) { // If something was sent by submitting the form.

    $name = htmlspecialchars($_POST["user-name"]);
    $email = htmlspecialchars($_POST["user-email"]);
    $message = htmlspecialchars($_POST["comment"]);
    $subject = "Contact form";
    $to = "john.doe@gmail.com"; // your email here
    $userBcc = $_POST["user-bcc"]; // You can BCC the user's email

    $mail = new PHPMailer();
    $mail->From = "xyz@xyzs.com"; // make sure to change this to something else
    $mail->FromName = "XYZ XYZ"; // change this too.
    $mail->AddReplyTo($email, $name);
    if($userBcc == true) {
        $mail->addBCC($email);
    }

    $mail->Subject = $subject;
    $mail->Body    = $message;

    if(!$mail->Send()) {
        echo "Message could not be sent. <p>";
        echo "Mailer Error: " . $mail->ErrorInfo;
        exit;
    }
    echo "Message has been sent";
}
?>

所以现在你应该有一个有效的表格。我建议稍微测试一下,只需发送电子邮件给自己并在你实现之前做测试提交。您可能还希望实施某种形式的反垃圾邮件,具体取决于使用该网站的受众。原谅我,如果我忘了什么或者不清楚,我会编辑帖子并尝试详细/修复我能做的任何事情。

祝你好运,让我知道它是怎么回事!