In my projects I do the following..
<?php
class sfSwiftMailerActions extends sfActions
{
public function preExecute()
{
$mailVars = sfSwiftMailerVars::getInstance()->getAll();
foreach ($mailVars as $key => $value)
{
$this->$key = $value;
}
}
public function sendEmail($module, $action)
{
sfConfig::set('symfony.view.' . $module . '_' . $action . '_layout', false);
$body =
sfContext::getInstance()->getController()->getPresentationFor($module,
$action);
$mailVars = sfSwiftMailerVars::getInstance();
$message = $mailVars->has('message') ?
$mailVars->get('message'):new Swift_Message(null, null, 'text/html');
$swift = $mailVars->has('swift') ? $mailVars->get('swift'):new
Swift(new Swift_Connection_Sendmail(Swift_Connection_Sendmail::AUTO_DETECT));
$message->setSubject($mailVars->get('subject'));
$message->setBody($body);
$swift->send($message, $mailVars->get('recipients'),
$mailVars->get('from'));
$swift->disconnect();
$mailVars->clear();
}
public function __set($key, $value)
{
sfSwiftMailerVars::getInstance()->set($key, $value);
return parent::__set($key, $value);
}
}
<?php
class sfSwiftMailerVars extends sfParameterHolder
{
static $instance = null;
public static function getInstance()
{
if (!self::$instance)
{
self::$instance = new sfSwiftMailerVars();
}
return self::$instance;
}
}
With that setup, I extend all my module actions from sfSwiftMailerActions
and I call $this->sendEmail('module', 'action'); The module which is
responsible for holding the e-mails also extends from the same class and the
preExecute() exists so that any variables set inside of the module which
invokes the email will be available to the email action/template as well. I
accomplish this by overriding __set() of sfActions. I realize this is an
absolutely horrid hack but it works for my project. I am not sure if we
could implement something like this cleanly without it being in the core.
Here is an example.
class contact_usActions extends sfSwiftMailerActions
{
public function executeIndex()
{
$this->send_to = '[EMAIL PROTECTED]';
$this->sendEmail('mailer', 'contact_us');
}
}
class mailerActions extends sfSwiftMailerActions
{
public function executeContact_us()
{
$this->subject = 'test';
$this->recipients = $this->send_to;
}
}
contact_usSuccess.php
---------
Subject: <?php echo $subject; ?>
If you don't specify a variable in your mailer action named 'message' and
'swift' that contain instances of Swift_Message and Swift they will just be
automatically created for you with some defaults.
- Jon
On Mon, Sep 15, 2008 at 7:09 AM, Nicolas Perriault <[EMAIL PROTECTED]>wrote:
>
> Hi devs,
>
> As you may know, I'm in charge of providing a standard way to send
> emails within symfony 1.2. I'd like to get your feedback and ideas
> about the way the SwiftMailer library goodness should be integrated.
> Currently I've just played with Swift and nothing has really been
> started by my side, so everything is still possible =)
>
> To me, the main important thing if to be able to use a different
> mailer dependending of the environment: maybe in dev mode you doesn't
> want to send real email, but just want to generate them and store them
> on your local filesystem (or anywhere you want), as the sfEmailPlugin
> does. That would be possible using the factories.yml file, like this:
>
> prod:
> mailer:
> class:
> sfMailerSmtp:
> server: foo.bar.tld
> port: 25
> username: foo
> password: bar
> default_sender: Toto Mailer <[EMAIL PROTECTED]>
> all:
> mailer:
> class:
> sfMailerLocalFilesystem:
> path: %SF_LOG_DIR%/emails
>
> My main concern here is that it will be hard to bundle it as a plugin,
> because I do not know any way to hook/override the factories
> configuration in symfony within a plugin context... Maybe a smart
> approach would be to provide base abstract classes in the symfony
> core, and the plugin would provide a Swift based implementation? I
> don't know, but it looks like if not possible, we'll must implement
> this feature in the core...
>
> I was also thinking about creating dedicated message classes (as Swift
> already does) but with native symfony-based mail contents generation
> available, to ease mail objects reuse. Maybe something like this
> (rough draft):
>
> $email = new sfEmail();
> // then
> $email->setHtmlContentResolver(sfEmail::FROM_ACTION, array('mymodule',
> 'myaction'));
> // or
> $email->setTextContentResolver(sfEmail::FROM_PARTIAL,
> array('mymodule', 'mypartial'));
> // or
> $email->setTextContentResolver(sfEmail::FROM_COMPONENT,
> array('mymodule', 'mycomponent'));
> // or
> $email->setTextContentResolver(sfEmail::FROM_STRING, 'hello world');
> // and maybe
> $email->setContentResolver('text/html', sfEmail::FROM_STRING, 'hello
> world');
> // why not
> $email->setContentResolver('application/pdf', sfEmail::FROM_FILE,
> '/tmp/toto.pdf');
> // there could be also sfEmail::FROM_CALLBACK and so on
> // then
> $email->send();
>
> It's a bit verbose, eh? Maybe we can also use dedicated classes for
> email templates, eg. sfEmailTemplateAction, sfEmailTemplateComponent,
> etc., but maybe it's a bit bloated by design for the purpose of just
> sending emails? Swift has native support fr template, maybe just
> extending it with these sfEmail templates classes could be a solution?
>
> For custom reuseable emails, we should be able to easily create
> dedicated email classes:
>
> class myEmail extends sfEmail
> {
> public function configure()
> {
> // configure the email here, eg.
> $this->setSubject('Hello there');
> }
> }
>
> These classes would be stored in a new lib/email directory by default,
> maybe with a stub BaseEmail class available for user level generic
> implementation purpose: sfEmail < BaseEmail < myEmail.
>
> At the end, I would like to be able do do things like that:
>
> class fooActions extends sfActions
> {
> public function executeBuy($request)
> {
> if ($request->isMethod('post))
> {
> $email = new myEmail();
> $email->addTo('[EMAIL PROTECTED]');
> $email->setVars(array($request->getAttribute('stuff')));
> $email->send();
> }
> }
> }
>
> So here are my actual thoughts. What are your ideas and suggestions?
>
> ++
>
> --
> Nicolas Perriault
> http://prendreuncafe.com - http://symfonians.net - http://sensiolabs.com
> Phone: +33 660 92 08 67
>
> >
>
--
Jonathan H. Wage
Open Source Software Developer & Evangelist
http://www.jwage.com
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"symfony developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/symfony-devs?hl=en
-~----------~----~----~----~------~----~------~--~---