Sending Confirmation Emails with Phalcon and Swift

Posted by Unknown on Monday, January 27, 2014

Today, sending emails is considered the basic functionality of any web application. Usually, an email is sent to notify the user of some kind of activity that has taken place on the website, such as when he registers the account, updates information, or when new friends have been found. In this short tutorial I’ll show you how to send emails via Gmail quickly from within a Phalcon sample application. You can download the full source code from GitHub. We'll be building functionality which lets us send a confirmation email, and reacts to the click of said confirmation email.


Step 1: Install Phalcon, Phalcon Tools


Installing Phalcon is easy if you just follow the instructions on their website. Don't forget to install the Phalcon tools as well!


If you're using Vagrant, you can also install the whole set into your box by following this tutorial.


Step 2: Initial setup of our application


We’ve successfully set up the development environment. Now we proceed to create the skeleton of our application. Open terminal (in Linux & Mac) or command prompt (cmd in Windows) and type following in the folder where you keep your web projects:



phalcon project sendmail

This will produce the following file and folder structure:


sendmail project structure


Try it out. Open your web browser and type in http://127.0.0.1/sendmail/, or the address of the virtual host you gave to your project. Provided everything is running fine, as in the image below, continue to step 3.


enter image description here


Step 3: Initial configuration


There are a few things you need to configure at first:



  1. Database connection (app/config/config.php)

  2. Model, view, and controller directories

  3. Base application URL

  4. Mail server configuration


All configurations are stored in the config.php file as per the image below:


config.php


Enter configuration settings as in the image above, only replace credentials with your own.


Regarding "mail", here I've left the driver set to smtp. I set the host to use smtp.gmail.com which is Gmail’s SMTP server. I set the port to 465 via SSL, and from to my email address, basically the same email address we'll be using to actually send the emails.


Step 4: Database schema and configuration


The following schema shows 2 tables which we'll be using:



CREATE TABLE IF NOT EXISTS `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(40) NOT NULL,
`email` varchar(40) NOT NULL,
`password` char(60) NOT NULL,
`mustChangePassword` char(1) DEFAULT NULL,
`fullName` varchar(50) NOT NULL,
`active` char(1) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `email_confirmations` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`usersId` int(10) unsigned NOT NULL,
`code` char(32) NOT NULL,
`createdAt` int(10) unsigned NOT NULL,
`modifiedAt` int(10) unsigned DEFAULT NULL,
`confirmed` char(1) DEFAULT 'N',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;


I leave out the details of how to create a database on purpose – it's assumed you know how to do this. Create a sample database with your favorite tool, and execute the above code against it.


A note about relationships here:



  • The model “users” has many “email_confirmations”.

  • The model "email_confirmations" belongs to "users"


Keep this in mind for later.


Step 5: Models and validation


A model is a class that extends Phalcon\Mvc\Model. It must be placed in the models directory. A model file must contain a single class; its class name should be in camel case notation and should correspond to the database table name. If, instead, you want to have a model User and a database table "users" you need to use the getSource method to configure this.


Truncated by Planet PHP, read more at the original (another 9185 bytes)




more

{ 0 comments... » Sending Confirmation Emails with Phalcon and Swift read them below or add one }

Post a Comment

Popular Posts