Search code examples
coldfusioncfmlcoldboxcfmail

Converting CFMAIL to script equivalent and using a query


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 <[email protected]>"
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="[email protected]",
        from="[email protected]",
        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!


Solution

  • I believe it would be:

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

    See: https://cfdocs.org/valuelist