How to send an email from a Task (console command) using SwiftMailer in Symfony 1.4

What a lot of developers don't know about the old Symfony 1.4 framework, is that Swift Mailer, exactly like its successors, is already included in the project and even injects itself automatically in all the controllers of the project. However, when you work inside a task (a console command), although other tools are available, like Doctrine, SwiftMailer is not available directly.

To make Swift Mailer accesible from a Task, you will need to include the swift_required.php file that includes all the required classes to work with it. You can include it with require_once like this:

require_once dirname(__FILE__) . '/../../symfony-1.4.27/lib/vendor/swiftmailer/swift_required.php';

After including this in your task, you will be able to send an email as usual with Swiftmailer. In case you don't know how, we'll show you a full example of a functional command that basically sends an email to the desired receiver.

Creating the task to send email

Create the SendEmailTask.class.php file inside the project/lib/task directory and add the following code as content of the file:

<?php

// 1. Include SwiftMailer with the swift_required.php file, otherwise the namespaces of SwiftMailer won't be available.
//    Symfony 1.4 includes Swift Mailer inside the Symfony code.
require_once dirname(__FILE__) . '/../../symfony-1.4.27/lib/vendor/swiftmailer/swift_required.php';

/**
 * Command to send a test email from Symfony 1.4 using Swift Mailer.
 * 
 * You can run the command as:
 *  
 *      'php symfony ourcodeworld:send-email'
 * 
 * @author Carlos Delgado <dev@ourcodeworld.com>
 */
class SendEmailTask extends sfBaseTask {

    public function configure()
    {
        $this->namespace = 'ourcodeworld';
        $this->name = 'send-email';
        $this->briefDescription = 'Sends a test email';
        $this->detailedDescription = <<<EOF
Sends an email with SwiftMailer (integrated version of Symfony 1.4)
EOF;
    }

    public function execute($arguments = array(), $options = array()) {
        $email = "cheese@cheese.ch";
        $password = "ch33s3";

        // 2. Prepare transport, change the smtp server adress and configuration
        $transport = \Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')
            ->setUsername($email)
            ->setPassword($password);

        // 3. Create the mailer
        $mailer = \Swift_Mailer::newInstance($transport);

        // 4. Create the message instance
        $message = \Swift_Message::newInstance("Test Email")
            ->setFrom(array($email => 'Subject Email'))
            ->setTo(array(
                "targetemail@domain.com" => "targetemail@domain.com"
            ))
            ->setBody("This is the content of my Email !", 'text/plain');

        // 5. Send email !
        $mailer->send($message);
    }
}

After saving the file, you may run the command from your terminal with php symfony ourcodeworld:send-mail.

Happy coding !

This could interest you

Become a more social person