[BUGFIX] Bug in Decorator (maybe).
Posted: Thu Jun 07, 2007 3:18 pm
I hacked together a script earlier today (well, PHPDN was down, I had to pass the time working instead). It took a CSV file, broken it up into an array to pass into Swift as replacements for the Decorator, and sent an email to all the people listed. It turned out though that there's a problem with the Decorator not finding email addresses in the keys of the replacements array if they're defined with upper case letters in them. Swift will happily send the email, but it's sent uncustomised.
The fix was easy, just strtolower() the email addresses as they were added to the array as keys. However, it might be worthwhile adding an option to the Decorator to not send an email if the customisation data isn't found in order to avoid people sending raw templates to their users. That didn't happen to me because I always send everything to a local test server, but I've been doing this a while, if I was a newbie it could have been nasty. I'm not sure if features like that are fitting with the Swift ethos though.
This is with Swift 3.2.5 on PHP 4.3
Code: Select all
<?php
require_once "lib/Swift.php";
require_once "lib/Swift/Connection/SMTP.php";
require_once "lib/Swift/Plugin/Decorator.php";
$csvFile = fopen("./contacts.csv","r"); //firstname, lastname, emailaddress in standard CSV formatting.
while($csv = fgetcsv($csvFile, 1024, ","))
{
$firstName = $csv[0];
$lastName = $csv[1];
$toAddress = $csv[2];
$replacements[$toAddress] = array("{first_name}" => $firstName, "{last_name}" => $lastName);
}
fclose($csvFile);
$swift =& new Swift(new Swift_Connection_SMTP("localhost"));
$swftAddr_from = new Swift_Address("onion@test.com", "onion@test.com");
$message =& new Swift_Message("Email Subject");
$swift->attachPlugin(new Swift_Plugin_Decorator($replacements), "decorator");
include "textTemplate.email.php"; //Defines email text content and puts it in $text_message
include "htmlTemplate.email.php"; //Defines email HTML content and puts it in $html_message, includes some embedded images.
$message->attach(new Swift_Message_Part($text_message));
$message->attach(new Swift_Message_Part($html_message, "text/html"));
foreach ($replacements as $toAddress => $data) {
if ($swift->send($message, $toAddress, $swftAddr_from)) { echo "<br>SENT ".$toAddress; } else { echo "<br>FAIL ".$toAddress; };
}
?>This is with Swift 3.2.5 on PHP 4.3