course when you register on learnphalcon
The examples on this site are currently tested to work on Phalcon V3.4 and Phalcon Devtools V3.2 Some issues may arise when using later versions.

Please get in touch or post a comment below the post if you encounter a problem.

Now that Swiftmailer is installed and set up we need to get it working. The advantage of using a package like Swiftmailer is that there is good documentation available and that they have lots of examples. In order to send an email you need need an email server. Although an email server is a relatively straightforward application in terms the functionality it provides, running an email server can be tricky. Dealing with security and spam is constant challenge. Where years ago organisations ran and maintained their own email servers, in recent times this tends to be outsourced - often to Microsoft or Google. The following transport will demonstrate how to use a gmail account to send an email notification. Traditionally, simple mail was transported on port 25 but modern systems require tls or ssl encryption which use ports 587 an 463.

If you send emails a lot during the execution of your application there are two things you'll need to consider.

Setting up a mailer object to run as a service

Setting up an email to run as a service means you won't be instantiating new objects every time you want to send an email. You can just use the "service" objects that persist in the dependency injection container. The point of a service is to be available all the time as it is used regularly and should be callable from anywhere in your application. Using this approach will lead to improved performance, better design and reduced repetition of code.

Using a queuing system such as Beanstalk

If you need to be able to notify a large group of users then sending out emails inside a loop will definitely have a significant impact on systems response time. A queuing mechanism such as Beanstalk allows the sending of emails to be processed in a queue and run in the background. This means that user will not have to hang around waiting for that code to complete. Errors can also be handled in such a way as to allow the application to continue processing the emails. Using a third party bulk emailing service may also be an option worth considering in this situation.

For now we will simply demonstrate how to use the Swiftmailer library to send out a simple message using gmail as a transport. 

Sending an email requires an email address, subject and body but also you need to have a transport email service. i.e. the email address that your email will come from.

Copy the following code into app/controllers taking care to change the change the parts in red.

class EmailController extends ControllerBase
    public function sendEmailAction($toAddress, $subject, $body)
		$transport = new Swift_SmtpTransport('smtp.gmail.com',587,'tls');
		$transport->setStreamOptions(array('ssl' => array('allow_self_signed' => true, 'verify_peer' => false)));
		$mailer = new Swift_Mailer($transport);
		$message = new Swift_Message($subject);
		$message->setFrom(['youremail.goeshere@gmail.com' => 'Yourname Goeshere']);
		$message->setTo([$toAddress, $toAddress => $toAddress]);
		$result = $mailer->send($message);
		if ($result>0) {
			$this->flash->notice("Email sent sucessfully");
			$this->dispatcher->forward(["controller" => "member","action" => "search"]);
		else {
			$this->flash->notice("Email not sent sucessfully");
			$this->dispatcher->forward(["controller" => "member","action" => "search"]);
		$this->dispatcher->forward(["controller" => "member","action" => "search"]);

	public function testMailAction()
		$this->sendEmailAction('addresse@theirAddress.com','Hi from Swift', 'The body of the message');

You can test this by visiting http://localhost/yourApp/email/testMail

As stated above, this approach to using email is probably not suited to a production system but it will get you started quickly.

In order to enable gmail to be used as a transport in this way you need to change a setting on your gmail account to allow "less secure apps" send emails. The following article describes how to do this