Block Disposable Emails with Symfony, Doctrine and MongoDB

If you want your users to use their real email addresses when registering to your app, there are a few ways to do this. The most effective way to do this is sending a confirmation email and after activating their accounts. And also, if you want to make some validations before sending activation email you can use MX records validation or host validation of domain with using Symfony’s email validator.

But even if the given email is valid and confirmation email reaches the user, you may also want your users to use their real email addresses. One way to increase the possibility of real email addresses is to block disposable email providers. In this post I will try to explain how to block disposable email addresses on your forms with Symfony.

What we need

  • Symfony
  • FoS User Bundle
  • Doctrine ODM
  • DoctrineFixturesBundle

The steps we will follow

  1. Create Doctrine documents
  2. Load disposable domains to MongoDB
  3. Create a custom email validator
  4. Define our custom email validator as service (we need this to connect MongoDB from validator)
  5. Add our validation rule to our form

1. Create Doctrine Documents

We will use MongoDB to check if user’s email domain is in our list. So, we need to create the Document.

And create getters and setters with running this command:

2. Load Disposable Email Domains to MongoDB

We will use DoctrineFixturesBundle to load our domain data. If you don’t have this bundle or don’t know about it, you can read about it here.

We will first get the domain list from this gist and load it to our disposable_email_domains collection in MongoDB.

3. Create Custom Email Validator

You can learn more about custom validators in Symfony here. We will split user’s email address from “@” symbol and check if the domain is in our list. Since we have our list on MongoDB, we need to use Doctrine. So, to provide Doctrine dependency to our custom validator we need to define our validator as service. I will explain this next step.

Let’s create our Custom Email Constraint

And here’s our validator

4. Define our custom email validator as service

As I mentioned before, custom validators don’t support dependencies. To provide our Doctrine dependency we need to define our validator as service.

5. Add disposable email blocker to forms

Finally, add custom email validator to all forms you ask for email address. Here’s how to apply it to my registration form. It only has email and password fields.

The trick is to add our custom validator’s namespace and add the validator to email address field’s annotation

And again, create getters and setters:

Regex Password Strength Chek

We always need our users to create strength passwords. To check their password if it’s strength with regular expressions is very easy.

Here are the explanations,

  • (?=.*[A-Z]) check for uppercase letters
  • (?=.*[!@#$&*]) check for special characters
  • (?=.*[0-9]) check for digits
  • (?=.*[a-z]) check for lowercase letters
  • {8,50} length must be at least 8 characters and max 50 characters long

If you don’t want to check length, change {8,50} to asterisk character. Or if you don’t want some parts of the regex, just remove it. Others will still work.

Inspired from:

Change Symfony Form Theme including FOSUserBundle’s

I’m new to Symfony and I was playing with FOSUserBundle for user management. It makes everything about user management like login, register, profile pages and password reset operations really easy. But the forms are not looking good. I implemented FOSUserBundle’s forms into my project’s theme but it was still looking bad. I even tried to create every input with hand in a static twig layout but it was a total disaster. It’s obviously the worst option. So after digging in documents I got it’s really easy to make Symfony forms have a better look.

Symfony has built-in support for Bootstrap 3 and Foundation Framework. You have other customization options but I prefer to change form theme application-wide. Just add the code below to your app/config/config.yml and your forms will be generated compatible with Bootstrap 3.

Look here for other options and layout-specific customizations:

AWS, DigitalOcean User Data for Apache, PHP, MongoDB, Git

User Data

I’m using user data scripts for both my AWS servers and DigitalOceal servers. Even if I only need to install a few thing, I use user data not to lose time for installing process. Here is the user data that I mostly use.

Install PHP – MongoDB Driver Ubuntu 14.04 LTS

Details for AWS

Details for DigitalOcean

Using MongoDB Embedded Documents in Symfony with Doctrine

I’m using embedded documents to handle team accounts. We have a main team account information and also team members’ information in a sub-array of same document. We aim to log users in with using company’s username/password or their own username/password.

You need to be familiar with those to understand the concept

  • MongoDB
  • Embedded Documents
  • Symfony
  • Doctrine ODM


Here is an example user document structure,


We will use Doctrine MongoDB Bundle to use MongoDB with Symfony. So if you have not installed it follow steps here:

User Document Class

We need to create a document class which describes our MongoDB document. This will hold our data for our operations. And we also use automatically generated get() and set() methods for the values which we have in our document class. In short, create your MongoDB document as a PHP class with needed defining (annotations) for data types, or other options.

TeamMembers Embedded Document Class

After creating our main document’s class and point the embedded document class, we need to create TeamMembers class as same as our User class.

Getters and Setters

To generate get and set methods for our document classes, run this command in your Symfony directory.

This will update our src/MyProjectBundle/Document/TeamMembers.php and src/MyProjectBundle/Document/User.php classes with generated get and set methods. Annotations will also be generated with methods.

Here is a few examples for getter and setter methods,


To create a new document with its embedded document, we need to create new object for each one and use set() methods to set their values. After that we need to combine them with add method.

Create new document

Get document

Full controller


Example might not work as expected. Use it as draft.

Hello world!

Sorun şu ki,
Teknoloji yazılarının ömrü yeni versiyonu çıktığında doluyor.
Yemek yazılarının ömrü senden sonraki müşterinin yaşadığı deneyimle doluyor.

Bugün hiç kimse MongoDB 2.6’nın kurulum notlarını aramıyor. Yarın hiç kimsenin 3.2’yi aramayacağı gibi.

Haliyle, teknoloji veya deneyim yazarak işe yaramak zor.

Çözüm şu olabilir,
Her şeyi versiyonlayarak mümkün mertebe geriye dönerek güncelleme yaparsak, hem hızlı tükenen ve değişen bilgiyi, hem de deneyimi işe yarar halde tutabiliriz. Bunu yapabildiğimiz hibrit yapılı wiki-blog mantığı, algısı ve mimarisine ihtiyacımız var.

Velhasıl, denemeye değer.