Sending Confirmation Emails with Phalcon and Swift

Share this article

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, 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:


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 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:

          `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`)

    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`)

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.

We will now let Phalcon Tools generate the code for us. Using the command line and moving to your project’s directory, execute the following command:

    phalcon model users
    phalcon model email_confirmations

Let’s check out sample generated class Users for table users, and upgrade it accordingly:

enter image description here

In the code above, we test if the login field validates against the rule Email. Additionally, we ensure that login is unique. Take note of the return statement which checks if validation has returned any validation messages.

Let’s check out the generated class EmailConfirmations:


The following code represents a very simple skeleton and I will only explain how afterCreate() and initialize() should look like – the rest of the code is available in the repository linked at the beginning of this article.

  1. The initialize() method is only called once during the request. In this method, Phalcon is to automatically generate a hasMany relation.
        public function initialize(){
            $this->belongsTo('usersId', 'Users', 'id', array(
                'alias' => 'user'

The first parameter indicates the field of the local model EmailConfirmations in the relationship; the second indicates the name of the referenced model (Users) and the third the field name in the referenced model. The alias is there to create an alternative name for the connection between these two models, so we can reference an email confirmation’s user just by calling $emailConfirmationInstance->user.

  1. The afterCreate() method sends a confirmation email to a user after account creation. Notice we’re using abstracted functionality to send the email – the email service resides in our dependency injector and does this separately from the model itself.
            public function afterCreate(){
                    array($this->user->email => $this->user->username),
                    'Please confirm your email',
                    array( 'confirmUrl' => '/confirm/' . $this->code.'/'. $this->user->email)

afterCreate() and beforeValidationOnUpdate() are Events – see the docs for more information on those.

Step 6: Controller and Library

The following code presents a very simple Controller skeleton and I’ll explain what view() and create() actions should look like, the rest of the code is available in the source.

    class SessionController extends ControllerBase{
            public function indexAction() {}
            public function signupAction(){}
            public function loginAction() {}
            public function confirmEmailAction(){}

The signupAction() is receiving data from the form and storing it the table, while confirmEmailAction() confirms the account after signup. You can see the full file at SessionController.php.

This part of the application is implemented in the “Mail” component.

        require_once __DIR__ . '/../../vendor/Swift/swift_required.php';
        class Mail extends Phalcon\Mvc\User\Component

            protected $_transport;

            public function getTemplate($name, $params){}
            public function send($to, $subject, $name, $params){}  

Here, I’m using the Swift mail library. Install it via composer, or just include it in the vendor folder like on the linked GitHub repo (see here). This class extends the Phalcon\Mvc\User\Component, which, while not required, helps us fetch it via the DI container. Now, we register this class in the services container:

        //Mail service uses Gmail;
        $di->set('mail', function(){
                return new Mail();

The function getTemplate defines the template to be used with the email (see source for details).

    public function send($to, $subject, $name, $params)
        $mailSettings = $this->config->mail;
        $template = $this->getTemplate($name, $params);
        // Create the message
        $message = Swift_Message::newInstance()
                  $mailSettings->fromEmail => $mailSettings->fromName
              ->setBody($template, 'text/html');
              if (!$this->_transport) {
                $this->_transport = Swift_SmtpTransport::newInstance(
              // Create the Mailer using your created Transport
            $mailer = Swift_Mailer::newInstance($this->_transport);
            return $mailer->send($message);

Step 7: view

I use scaffolding based on Twitter Bootstrap library. Even though scaffolding is a temporary solution it doesn’t mean it can’t look nice!

Keep in mind that when using scaffolding with bootstrap, Phalcon will only generate HTML tags with appropriate HTML classes – it is up to you to include the static resources like images and CSS, so navigate to your project root and edit the file app\views\index.volt by adding the stylesheet line:

        <title>Phalcon PHP Framework</title>
        {{stylesheet_link ('http//',false)}} 
        {{stylesheet_link ('css/css.css')}} 
            {{ content() }}

This particular example uses the Volt templating engine. Volt is an ultra-fast and designer friendly templating language written in C for PHP. It provides you with a set of helpers to write views in an easy way. More information is available in the official docs.

Next, create a signup form. A typical view would incorporate usage of the Phalcon\Tag component, a convienient helper class that aids creation of forms, links and other html elements:

form signup

Time to try it out. Open your web brower, visit the app, and go to [app url]/signup.

signup form

… and then check that user’s email account to verify that the email was sent successfully:

confirm account

Perfect! Our email was sent! Click confirm to activate the account, and that’s it!


And that’s all there is to sending email messages with Phalcon and Gmail. You can of course customize your email messages as much as you’d like to make the mail more appealing.

For more information on Phalcon check out the online documentation. Thanks for reading – leave your feedback and comments below.

Frequently Asked Questions (FAQs) about Sending Confirmation Emails with Phalcon and Swift

What is the role of Phalcon in sending confirmation emails?

Phalcon is a high-performance PHP framework that is used to build web applications. In the context of sending confirmation emails, Phalcon provides a robust and efficient platform for creating the backend logic that triggers these emails. It allows developers to set up conditions under which confirmation emails should be sent, such as after a user has successfully registered on a website or made a transaction. Phalcon’s speed and efficiency make it an excellent choice for handling such tasks.

How does Swift contribute to the process of sending confirmation emails?

Swift is a powerful and intuitive programming language developed by Apple. Swift Mailer, which is a component of Swift, is used in the process of sending emails. It is a flexible and elegant object-oriented library for sending emails from PHP applications. Swift Mailer integrates seamlessly with the Phalcon framework, allowing developers to send emails with attachments, HTML bodies, and multiple recipients, making it a versatile tool for sending confirmation emails.

What is a SWIFT confirmation document?

A SWIFT confirmation document is a form of proof that a transaction has been completed successfully. It is a message sent by the SWIFT international payment network confirming the details of the transaction, including the amount transferred, the date of the transfer, and the parties involved. This document is often used in international banking and finance to verify transactions.

How does a SWIFT MT103 document differ from a SWIFT confirmation document?

A SWIFT MT103 document is a specific type of SWIFT confirmation document. It is a standard format of Business Identifier Codes (BIC) and it is unique identification code for both financial and non-financial institutions. The MT103 message enables one bank to instruct another bank to credit the account of one of their customers, debiting the account held by the sending institution with the receiving bank to balance everything out.

What is the difference between a SWIFT confirmation and a bank confirmation?

A SWIFT confirmation is a document that verifies a transaction has been completed successfully through the SWIFT international payment network. On the other hand, a bank confirmation is a document issued by a bank confirming that a particular customer has a certain amount of funds in their account. While both are forms of financial confirmation, they serve different purposes and are used in different contexts.

How can I integrate Phalcon and Swift in my web application?

Integrating Phalcon and Swift in your web application involves a few steps. First, you need to install both Phalcon and Swift Mailer in your development environment. Once installed, you can use Phalcon’s MVC architecture to create the logic for sending emails. Swift Mailer is then used to actually send the emails, with Phalcon triggering the sending process based on the conditions you’ve set up.

Can I send HTML emails using Phalcon and Swift?

Yes, you can send HTML emails using Phalcon and Swift. Swift Mailer supports the sending of emails with HTML content. This means you can create visually appealing emails with images, links, and formatted text. Phalcon can be used to generate the HTML content dynamically, allowing for personalized emails for each recipient.

Can I send confirmation emails with attachments using Phalcon and Swift?

Yes, you can send confirmation emails with attachments using Phalcon and Swift. Swift Mailer allows for the inclusion of attachments in emails. This can be useful in situations where you need to send additional documents or files to the recipient along with the confirmation email.

How secure is the process of sending confirmation emails with Phalcon and Swift?

The process of sending confirmation emails with Phalcon and Swift is secure. Phalcon is known for its security features, including protection against common web exploits. Swift Mailer also provides several security features, such as secure SMTP authentication and the ability to encrypt emails. However, as with any web application, the overall security will also depend on other factors such as server configuration and the use of secure coding practices.

Can I send confirmation emails to multiple recipients using Phalcon and Swift?

Yes, you can send confirmation emails to multiple recipients using Phalcon and Swift. Swift Mailer supports the sending of emails to multiple recipients, either as part of the ‘To’ field or using ‘CC’ and ‘BCC’. Phalcon can be used to manage the list of recipients, allowing for dynamic and flexible sending of confirmation emails.

Thien Tran DuyThien Tran Duy
View Author

I'm a professional web developer from Vietnam with B.S Physics and Engineering. I'm a Phalcon enthusiast as Opensource. In free time, I was support on forum Phalcon also my forum and contribution core Phalcon.

confirm accountconfirmatione-mailemailframeworkgmailphalconphalcon frameworkPHP
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week
Loading form