Go to Google Apps Script and start a new blank project.
{% gist 7799698 %}
After you save script, run it and authorize the script to access your Gmail account, then add triggers to call script.