Nerds who love the symfony-project
29 Aug
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…
10 Responses for "Symfony Tip: Sending emails with Symfony batch"
this topic is connected with cron
tip there “How to Use Symfony and Cron” http://www.lampjunkie.com/2008/04/how-to-use-symfony-and-cron/
it is possible to call other module/function from bath file? not only email function?
>>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!
You can insert variables into the request instance in the batch and then retrieve them back from the request object inside the action.
eg: In your batch file:
sfContext::getRequest()->setParameter(’key’,'value’);
and then in the action:
$var = $this->getRequestParameter(’key);
You can even pass an array in as a value if you need to pass a number of parameters. eg:
sfContext::getRequest()->setParameter(’values’,array(
‘var1′ = ’something’,
‘var2′ = ’something,
));
To pass parameters from your batch script to a mail action (in method 1) try:
$request = sfContext::getInstance()->getRequest();
$request->setParameter(’parameter1′, $value1);
$request->setParameter(’parameter2′, $value2);
just figured this out and seems to be working for me in limited testing so far.
Hi Guys,
It seems that the sfMail class doesn’t allow you to start TLS … which would make sending via email gmail smtp impossible.
Like to hear about your recommendations on how to get around this. Urgently awaiting your thoughts.
Cheers,
Tom
Tom, I’d suggest it would be advantageous for a number of reasons to use your SMTP server local to your server for sending email anyway.
If you wanted to track emails being sent via a GMAIL account you could always BCC all outgoing email to it.
Thanks bigm, very helpful indeed.
Tom,
You might be in luck my good man, I was browsing the 1.1 doco this fine afternoon and found the following, my thoughts immediately turned to your comment of course, such is out dedication to our readers here at symfonynerds!
http://www.symfony-project.org/cookbook/1_1/en/email#Using%20Google%20Mail%20SMTP%20servers
Wow, very cool. Mail relay here we come
Рекомендую: Дизель-генераторы АД30 - ад 30.
Специализированный автоцентр skoda, ликвидируя дефекты любых категорий сложности автомашин применяет компьютерное оснащение. Работники осуществят спланированные услуги.
Leave a reply