Customize message (with different languages)

Swift Mailer is a fantastic library for sending email with php. Discuss this library or ask any questions about it here.

Moderators: Chris Corbyn, General Moderators

Post Reply
Glamorous
Forum Newbie
Posts: 1
Joined: Fri Mar 20, 2009 5:43 am

Customize message (with different languages)

Post by Glamorous »

Hi,

I'am new here and was send from the Kohana Community. I use the implemented version of Swift in my installation (this is version 3.3.2)

Now for a customer I need to develop an application that could send emails to a 1000 people. So I thought I could use the batchSend for this.

But now the tricky-part:

- Every reciever must recieve his mail in his own language
- In the message there are some custom variables such as: name, link to participate, ...

I thought the Decoratorplugin was here on the correct place but I don't know if I can modify the total message for every user?
How do you see this problem and maybe a solution? The PHP code is in a Controller-class where I call the SWIFT-methods so I don't know where I should override the Decorartorplugin?

thanks on beyond!
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Customize message (with different languages)

Post by Chris Corbyn »

If you need to modify the entire message for each user (i.e send it in a different language) then the Decorator is not what you're looking for. Though for your variables {name} etc, it can handle that.

I'd suggest storing each language variation in separate template files, including the {name} and other placeholders that the decorator will use.

I'm making assumptions about your model here, but it would go something like this:

Code: Select all

//Sort out the replacements
$replacements = array();
foreach ($users as $user) {
  $replacements[$user->getEmail()] = array('{name}' => $user->getName());
}
 
//Add the Decorator plugin
$mailer->registerPlugin(new Swift_Plugins_DecoratorPlugin($replacements));
 
//Work through the recipients, selecting the best template based on language
foreach ($users as $user) {
  $strings = $this->_getStrings('some-email-template-name', $user->getLanguage());
  
  $message = new Swift_Message($strings->get('subject'), $strings->get('body'));
  $message->setTo($user->getEmail(), $user->getName());
  $message->setFrom('your@address.tld');
  
  $mailer->send($message);
}
The _getStrings() method is something I just made up for brevity. Basically it will have to find the language strings by whatever means best fits your application (files on disk, rows in a database...).

Note: You can certainly optimize things by doing this in a logical order, sending all messages in one language first, then the next and so-on... that way you can re-use the same message more effectively for each language.

I'm trying to come up with a more elegant way to handle message customization. Personally I'd prefer a view component that is passed to $message rather than a plugin. I haven't got the perfect solution just yet though.
Post Reply