cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
NigelP Post Prodigy
Post Prodigy

Loop around a collection of email addresses and use Office365.SendMail to send an email to each email address in the collection

Hi

 

I have a collection of email address and I want to send out an email to each email address in the collection with a time delay of 30 seconds between each email being sent.

 

I can see how to get the 30 seconds delay using a timer, but I am struggling to send out an email to each email address in the collection.

 

I tried using ForAll(emailCollection,.......

but i cannot use Set() or UpdateContext() inside a ForAll construct.

 

So using pseudo code :-

Loop around all email addresses

send email 

wait 30 seconds

end loop

 

I could use Components to send the email and wait 30 seconds, but i still need to give the component each email address in the collection.

 

Anybody any ideas ?

 

Thanks

 

Nigel

3 REPLIES 3
WarrenBelz Community Champion
Community Champion

Re: Loop around a collection of email addresses and use Office365.SendMail to send an email to each email address in the collection

Hi @NigelP ,
I would firstly get a collection with some numbers

Clear(colEmailSend);
ForAll(
   emailCollection, 
   Collect(
      colEmailSend,
      Last(
         FirstN(
            AddColumns(
               emailCollection,
               "IDNo",
               CountRows(
                  colEmailSend
               )+1
            ), 
            CountRows(colEmailSend)+1
         )
      )    
   )
);

Note that the below comes from this post from the Dynamic People, so I have not tested it, but it looks like it will do the job for you.
Do a CountRows(colEmailSend) to get the number of mails to send

UpdateContext({vEndLoop: ​CountRows(colEmailSend)})

Add a button to trigger the flow with OnSelect

UpdateContext({vRunNos: 0});
UpdateContext({vTimerStart: true})​

Add a Timer control and set Timer.Duration to 30000 (30 seconds) - with Timer.Start

vTimerStart

and Timer.Repeat to

If(vRunNos >= vEndLoop – 1, false, true)​

and Timer.OnTimerEnd to

Office365Outlook.SendEmailV2(
   Lookup(colEmailSend,
      IDNo = vRunNos,
      EmailAddressFieldName
   ),
   TitleStuff,
   BodyStuff
);
UpdateContext({vRunNos: vRunNos + 1});
If(vRunNos >= vEndLoop – 1, UpdateContext({vStartTimer: false}))

I will leave you to tidy up any code as I free-typed it

 

Please click Accept as solution if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it Thumbs Up.

 

NigelP Post Prodigy
Post Prodigy

Re: Loop around a collection of email addresses and use Office365.SendMail to send an email to each email address in the collection

Thanks @WarrenBelz - very useful.  In your example can I tell if a Office365.SendMail has failed ?

eg Set(boolFailed,Office365.SendMAil(colEmailSend..... )

WarrenBelz Community Champion
Community Champion

Re: Loop around a collection of email addresses and use Office365.SendMail to send an email to each email address in the collection

Hi @NigelP ,

As you are not using a Flow to do the email, you cannot return a value in the same way as setting a variable to the Flow return.

If you are asking about a debugging exercise, look in your Sent Items in Outlook.

You may also consider doing a IsMatch(YourEmailAddress,Email) ForAll to see if they are all valid. If they are and the code works, it should always work.

 

Please click Accept as solution if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it Thumbs Up.

Helpful resources

Announcements
MBAS Gallery 2020

MBAS Gallery 2020

Watch Microsoft Business Applications Summit sessions on-demand.

firstImage

New Ranks and Rank Icons released on April 21!

The time has come: We are finally able to share more details on the brand-new ranks coming to the Power Apps Community!

Top Solution Authors
Top Kudoed Authors
Users online (7,675)