1

So I am converting a legacy app to ColdBox MVC and trying to convert tags to script. In the past I would have just done

<cfmail
query="getRecipients"
type="text/html"
from="Me <me@me.org>"
to="#getRecipients.email#"
subject="Hello everybody from me">
<p>Hey #getRecipients.name#, how are you?</p>
</cfmail>

and I was done. Mails would have been sent to whoever got returned from the getRecipients query. But now it seems like I have to

var oMail = mailService
    .newMail(
        to="test1@test.com",
        from="me@me.org",
        subject="Hello everybody from me",
        bodyTokens={ name=name} 
        );

    oMail.setBody("
Hey @name@, how are you?

    ");

    var results = mailService.send( oMail );                                
}

Which itself seems overly wordy, but at least it works, when sending to a single recipient or a comma-separated list. But whatever I do, I cannot get it to send to a queried list of recipients.

I have tried to=getRecipients and gotten an error that the value of "to" is not a string. I tried to="getRecipients" and while I did not get an error, mail was not sent (or even queued up for sending). Various other attempts also either errored or failed silently.

I read the SO discussion here which seems to indicate that using a query in script-based mailings can't really be done. But that discussion was from 2012 -- surely that is no longer the case, right? Surely one can replicate this very fundamental feature in script, right? What am I missing? I have done a good deal of research on this and every example I have found sends only to a single hard-coded address.

I would appreciate any advice or suggestions on this. Thanks very much for your help!

daltec
  • 447
  • 1
  • 5
  • 15
  • My suggestion is this..., I've never been a fan of using only tags or only scripts everywhere. I use tags where tags work well, I use scripts where scripts work well. If you're at converting all tags to scripts you're going to have even more work when you get at converting queries, especially if they have cfifs in there. – Alex Baban Jul 31 '18 at 16:37
  • Thanks Alex, that is probably what I will end up doing, at least with email-related services. I'm just spending way too much time on something that will take a few minutes with good ol' cfmail. – daltec Jul 31 '18 at 19:14

1 Answers1

2

I believe it would be:

var oMail = mailService
    .newMail(
        to=valueList(getRecipients.email),
        from="me@me.org",
        subject="Hello everybody from me",
        bodyTokens={ name=name} 
        );

See: https://cfdocs.org/valuelist

Redtopia
  • 4,947
  • 7
  • 45
  • 68
  • Thanks Redtopia, that is one of the variations that fails silently. I do not get an error or any feedback that I can see in the console, but the email itself never gets queued up (or sent, needless to say). – daltec Jul 31 '18 at 18:14
  • 1
    My apologies Redtopia, your answer DID work! The page calling the mail function had been cached. Clearing the cache and re-running caused the email to sent to 7 emails from our database. Many thanks, and sorry for the mix-up!!! – daltec Jul 31 '18 at 19:46