I had a problem with the latest major version of SwiftMailer, where the decorator plugin will replace the placeholder in the message for the first email address in the list and then will use the same data for all of the following emails - regardless of the email address.
For example, if ...
$replacements[ test@test.com ] = array('{firstname}'=>'Jeff', '{age}'=>'32'); $replacements[ example@example.com ] = array('{firstname}'=>'Mary', '{age}'=>'86');
The first letter may say ... "Hi Jeff, you are 32." And then the second letter should say: "Hello, Mary, to you 86." But instead, the second letter is identical to the first. Any ideas? I appreciate any help with this ... Thanks.
This is my php code
foreach ($result as $user) { $replacements[$user['Email']] = array( '{FirstName}'=>$user['FirstName'], '{LastName}'=>$user['LastName'] ); } $mailer = Swift_Mailer::newInstance($transport); $decorator = new Swift_Plugins_DecoratorPlugin($replacements); $mailer->registerPlugin($decorator); $message = Swift_Message::newInstance() ->setSubject('Important notice for {FirstName}') ->setFrom(array(' john@doe.com ' => 'John Doe')) ->setBody( "Hello {FirstName}, we have reset your password to {LastName}\n" . "Please log in and change it at your earliest convenience." ) ; foreach ($result as $user) { $message->setTo($user['Email']); } // Create a message //$template = file_get_contents('../html/full_width.html'); //->setBody($template, 'text/html', 'utf-8'); //->addPart('Dear {FirstName} {LastName},This is testing mail.', 'text/plain', 'utf-8'); // Send the message // Pass a variable name to the send() method if (!$numSent=$mailer->send($message,$failures)) { echo "Failures:"; print_r($failures); } else printf("Sent %d messages\n", $numSent);
And this is the plugin code
<?php class Swift_Plugins_DecoratorPlugin implements Swift_Events_SendListener, Swift_Plugins_Decorator_Replacements { private $_replacements; private $_orginalBody; private $_originalHeaders = array(); private $_originalChildBodies = array(); private $_lastMessage; public function __construct($replacements) { if (!($replacements instanceof Swift_Plugins_Decorator_Replacements)) { $this->_replacements = (array) $replacements; } else { $this->_replacements = $replacements; } } public function beforeSendPerformed(Swift_Events_SendEvent $evt) { $message = $evt->getMessage(); $this->_restoreMessage($message); $to = array_keys($message->getTo()); $address = array_shift($to); if ($replacements = $this->getReplacementsFor($address)) { $body = $message->getBody(); $search = array_keys($replacements); $replace = array_values($replacements); $bodyReplaced = str_replace( $search, $replace, $body ); if ($body != $bodyReplaced) { $this->_originalBody = $body; $message->setBody($bodyReplaced); } foreach ($message->getHeaders()->getAll() as $header) { $body = $header->getFieldBodyModel(); $count = 0; if (is_array($body)) { $bodyReplaced = array(); foreach ($body as $key => $value) { $count1 = 0; $count2 = 0; $key = is_string($key) ? str_replace($search, $replace, $key, $count1) : $key; $value = is_string($value) ? str_replace($search, $replace, $value, $count2) : $value; $bodyReplaced[$key] = $value; if (!$count && ($count1 || $count2)) { $count = 1; } } } else { $bodyReplaced = str_replace($search, $replace, $body, $count); } if ($count) { $this->_originalHeaders[$header->getFieldName()] = $body; $header->setFieldBodyModel($bodyReplaced); } } $children = (array) $message->getChildren(); foreach ($children as $child) { list($type, ) = sscanf($child->getContentType(), '%[^/]/%s'); if ('text' == $type) { $body = $child->getBody(); $bodyReplaced = str_replace( $search, $replace, $body ); if ($body != $bodyReplaced) { $child->setBody($bodyReplaced); $this->_originalChildBodies[$child->getId()] = $body; } } } $this->_lastMessage = $message; } } public function getReplacementsFor($address) { if ($this->_replacements instanceof Swift_Plugins_Decorator_Replacements) { return $this->_replacements->getReplacementsFor($address); } else { return isset($this->_replacements[$address]) ? $this->_replacements[$address] : null ; } } public function sendPerformed(Swift_Events_SendEvent $evt) { $this->_restoreMessage($evt->getMessage()); }
another related plugin page
<?php interface Swift_Plugins_Decorator_Replacements { public function getReplacementsFor($address); }
Link: 1. quick page plugin page: http://swiftmailer.org/docs/plugins.html (using Decorator plugin)