Brennan Hoeting
Brennan Hoeting

Reputation: 1213

Laravel 4: Checking if an email is real during registration

I need to check if the email the user enters during registration is valid/real, meaning they can't just enter whatever they want as long as it meets the proper email format, I need to actually be able to send mail to it.

I also don't want to do email confirmation, though, where the user would have to click a link from an email to register. I just want it to verify on the spot.

Upvotes: 0

Views: 5870

Answers (3)

Aleksey Pikulik
Aleksey Pikulik

Reputation: 31

In case of Laravel (you've mentioned it as a tag for the question) I suggest you to use such a Laravel's native feature as Validation: Laravel 3 / Laravel 4. Below is how I use it in POST controller:

// Rules for form validation
$rules = array(
        'email'    => array('required', 'email', 'unique:users,email'), // Email is required and should satisfy E-mail format and it should be unique for table users.
);

// Form validation
$validation = Validator::make(Input::all(), $rules);

if($validation->fails()) {
    // Flash validator with input values and errors to some controller
        Former::withErrors($validation);
        return Redirect::to_action('some.controller')
            ->with_input()
            ->with_errors($validation);
    }

}

In general PHP usage case you can check it using PHP's native function like this:

filter_var($email, FILTER_VALIDATE_EMAIL)

Upvotes: 0

Pier-Luc Gendreau
Pier-Luc Gendreau

Reputation: 13814

You can do all the validation you want, but in the end the best and only way is to send an email with a verification link.

Also, because there is a huge amount of valid and odd emails that you would think aren't, the idea is generally to have fairly loose validation. Take a look here for all sorts of interesting emails.

Upvotes: 1

emarref
emarref

Reputation: 1311

You will need to do a DNS lookup on the MX record for the domain, then make sure that mx domain is contactable. I think that is about as close as you can get.

You might also consider letting a 3rd party do the validation for you - e.g. implement Google OAuth as a method of registration for your site.

Upvotes: 0

Related Questions