Question

I am making an app that allow to register to a newsletter and then manage it. There is the normal validation if the field is empty, if is email type, unique ecc.. But how can i check if the email really exists? If someone enter a fake mail, that mail is entered in the database, and this can mess up the database. And there are other annoyance. Is possible to check if the mail really exists?

Otherwise it is possible to automatically delete an unregistered user after 10 days?

Was it helpful?

Solution

You can create a rule to check if the e-mail format is valid:

$validator = Validator::make(
    Input::all(),
    array('email' => 'required|email')
);

if ($validator->fails())
{
    return "This is not a valid e-mail";
}

But this will only check for the format (name@domain.tld), because there is no way to know if an e-mail address really exists.

Well... this is not entirely true, there is a way: send an e-mail to that address and if you don't get an error message, the address exists, probably.

The best way to do what you need is, when a user creates an account in your system, send him/her a confirmation e-mail, with a link where he/she should click to validate that address. If the account is not validated for x days you delete it. This is a question that might help you on that: Laravel 4: how to make confirmation email?.

About your last question: create an Artisan Command that checks for unvalidated accounts and delete them. Take a look at the docs, it's easy, Laravel does almost all the job for you. And you can run this command once a day using cron or task manager:

php /var/www/your-site-dir/artisan check:accounts

EDIT:

This question will show you how to create a command: Creating and using Laravel 4 commands

And in the method fire() you should do something like:

public function fire()
{
    $dt = Carbon\Carbon::now();

    User::where('activated', false)
            ->where('created_at', '<', $dt->subDays(10))
            ->delete();
}

OTHER TIPS

There are some APIs you can use to verify the validity and existence of emails, but I am not sure how good they are. The free ones usually limit you to something like 10 an hour which probably would not be nearly enough for you, depending on how busy your site is.

Check out http://verify-email.org/register/levels.html

If you are worried about the sheer amount of emails being entered, you could probably log $_SERVER['REMOTE_ADDR'], which would be the client's IP address in the DB on emails and check to make sure that is unique as well before you save additional records to the table.

As far as how to actually validate the existence and validity of the entered email yourself, I believe you'd have to use fsockopen and issue smtp commands to servers. I did find this php class which attempts to do what we are talking about http://www.webdigi.co.uk/blog/wp-content/uploads/2009/01/smtpvalidateclassphp.txt

I doubt it works, but should get you a nice starting point.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top