Lately I’ve been working on some Symfony batch components to load/export data into and out of a database. If you are looking for a way to send emails from your Symfony batch scripts there are two ways.

1) Use An Action
It is possible to use a pre-defined action within a module for sending emails. You can, for example do this:

sfContext::getInstance()->getController()->sendEmail('module', 'mySendEmailAction');

Where you have a module called ‘module’ and an action within that called ‘mySendEmailAction’. You do however, run into issues when you want to pass paramaters across from your batch script to your action. I havent figured that one out yet - so if you know,  let us know!

The important thing to note with this option is that you will need to enable the is_internal setting in module.yml of your module.  The is_internal paramater allows you to restrict access to your action so that it only takes internal calls. To change this so that outside components (eg batch) can call your action, set is_internal to false. If you dont do this, you might get an error like this:

Uncaught exception ’sfConfigurationException’ with message ‘Action”mySendEmailAction” from module “module” cannot be called directly’ in /usr/share/php/symfony/controller/sfController.class.php:237

2) Directly use sfMail
The other, simpler option is to use sfMail directly. Within your Symfony batch script, just write your own custom method to send mail.

function sendEmail(){
  // class initialization
  $mail = new sfMail();
  $mail->initialize();
  $mail->setMailer('sendmail');
  $mail->setCharset('utf-8');
 
  // definition of the required parameters
  $mail->setSender('webmaster@my-company.com', 'Me');
  $mail->setFrom('info@my-company.com', 'My Company');
  $mail->addReplyTo('webmaster_copy@my-company.com');
  $mail->addAddress('test@test.com');
  $mail->setSubject('Those Symfony nerds are hot');
  $mail->setBody('Well.. they think they are');
  $mail->send();
}

Do you know of any other or better ways? Let us know! I’m not too sure how Symfony 1.1 tasks handels this, that will be another blog post in a few months time…