Page 1 of 1

[BUGFIX] Bug in Decorator (maybe).

Posted: Thu Jun 07, 2007 3:18 pm
by onion2k
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.

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; };
    }

?>
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

Posted: Thu Jun 07, 2007 4:05 pm
by Chris Corbyn
It's based on array keys so as you noticed it is case sensitive. I would definitely consider this a bug yes, because syntactically email addresses are NOT case sensitive, so I'll fix that, thanks :)

As for not sending if there's no match... That's a tough one. It's certainly not a bug, that's what I'd expect to happen. It's difficult to change the behaviour after people have started using it in their projects although in fairness, I couldn't see much reason for sending a template to someone! Perhaps if I add it as an optional setting?

Code: Select all

$plugin->setSendToBadRecipients(false);
The default behaviour for now would need to remain as it is I think, but I could make the default change at the next minor version (3.3).

Posted: Thu Jun 07, 2007 5:10 pm
by John Cartwright
Might be interested in using array_change_case() for fixing the so called "bug"

Posted: Fri Jun 08, 2007 12:58 am
by Chris Corbyn
Jcart wrote:Might be interested in using array_change_case() for fixing the so called "bug"
Thanks :)

Posted: Sun Jun 10, 2007 11:23 am
by Chris Corbyn
The case sensitivity issue has been attended to in subversion. A release will be made tomorrow. The idea about not sending if there are no replacements will be implemeted, but I'm waiting until 3.3.x when I can make a few changes to the plugin API ($e->eventCancelled property and $e->setEventCancelled(true)).