Forum for discussing general topics related to Couch.
4 posts Page 1 of 1
I am trying to build code to send a news flash to external subscribers to our site. I have their email addresses in a variable called bcc_list and the news flash in a variable called messagehtml. I cannot send the update to them in one go, since I would like to include an unsubscribe link into the news flash e-mail. As a consequence each e-mail must be personalised for the unsubscribe link and so I need to loop over the e-mail addresses.

Here's my code so far to achieve this:

Code: Select all
<cms:php>
   $emaillist=explode(',',<cms:show bcc_list />);
   foreach ($emaillist as $email) {
      <cms:send_mail to=$email from='me@example.com' subject="<cms:show messagesubject />" html='1'>
         <cms:show messagehtml />
         <...some code to generate the unsubscribe link and add it to the e-mail...>
      </cms:send_mail>
   }
</cms:php>

My problem is, how can I include the email address as stored in $email in the couch tag in the php code?
The code as shown above does not work. Also, first setting the email address with cms:set and then using it in send_mail using cms:show does not seem to work. Any ideas? All help appreciated!
"I have never tried that before, so I think I should definitely be able to do that" - Pippi Longstocking
Following post discusses how raw PHP code interacts with Couch code -
viewtopic.php?f=2&t=10763

Once you understand the sequence in which both are executed, perhaps it'd be easier for you to spot where your code is going wrong.

Hope this helps.
Adding to @KK's reply -
Perhaps, you'll find cms:each tag a working replacement for the php code. Seriously, why mess with php when there are native tags for that?
Thanks for the suggestions and links. With it, I managed to rewrite my code to the following: see below -- and it works --

Code: Select all
<cms:each bcc_list sep=','>
   <cms:php>
      // generate unsubscribe link here
      global $CTX;
      $CTX->set( 'unsubscribelink', $unsubscribelink, 'global' );
   </cms:php>
   <cms:send_mail to="<cms:show item />" from='me@example.com' subject="<cms:show messagesubject />" html='1' >                     
      <cms:show messagehtml />
      <a href="<cms:show unsubscribelink />">Unsubscribe</a></span></body></html>
   </cms:send_mail>
</cms:each>
"I have never tried that before, so I think I should definitely be able to do that" - Pippi Longstocking
4 posts Page 1 of 1