I'm trying to use CFmail in a CFScript CFC. I want CFMail to iterate through a query, and change values based on the query contents. It does this just fine, as long as the query object only has 1 row. If it has multiple rows (e.g. 5 rows), it will send 5 emails, but each email will contain the values from the first query row. I've tried a few different things. See below:
Query Obj:
Name | Email | Number
----------------------------------
John | john@foo.com | 12
Bill | bill@bar.com | 42
Ann | ann@bat.com | 100
CFScript:
var mailerService = new mail();
mailerService.setQuery(nameEmailNumberQuery);
mailerService.setTo('eterps@sendtomefortesting.com');
mailerService.setFrom( 'noReply@example.com' );
mailerService.setSubject('Hi');
mailerService.setFailto('fail@foo.com');
mailerService.setType('html');
mailerService.setSpoolenable(true);
savecontent variable="mailBody"{
WriteOutput(
"Hello " & Name & ". Your number is: " & Number & "!"
);
}
mailerService.send(body=mailBody & tmpVariable);
Using the above code, I get three emails. Each email says, "Hello John. Your number is: 12!"
I've also tried:
WriteOutput(
"Hello "
& mailerService.getQuery().Name
& ". Your number is: "
& mailerService.getQuery().Number
& "!"
);
and:
WriteOutput(
"Hello "
& mailerService.getQuery().Name[mailerService.getQuery.CurrentRow]
& ". Your number is: "
& mailerService.getQuery().Number[mailerService.getQuery.CurrentRow]
& "!"
);
EDIT: A couple more things I've tried (One as suggested by @invertedSpear)
Using the Query literal in the email body:
WriteOutput(
"Hello "
& nameEmailNumberQuery.Name
& ". Your number is: "
& nameEmailNumberQuery.Number
& "!"
);
Trying to use an incremented counter:
var counter = 1;
...
WriteOutput(
"Hello "
& mailerService.getQuery().Name[counter]
& ". Your number is: "
& mailerService.getQuery().Number[counter]
& "!" & evaluate('counter = counter++')
);
With the same result each time - 3 emails, all with 'John' and '12'. Dumping mailerService.getQuery().CurrentRow
results in '1' for each email. I am using Coldfusion 9.0.1 on Windows Server 2008 R3.