Azure / Microsoft365R

R SDK for interacting with Microsoft 365 APIs
Other
308 stars 42 forks source link

How to send emails from multiple email accounts in a local R session #133

Closed katharinescott closed 1 year ago

katharinescott commented 1 year ago

Thanks for this great package. I've successfully used it to send emails from my work outlook account, and now would like to use it to programmatically coordinate a study that I am working on, which requires me to send emails from multiple business accounts. I'm struggling to understand how to set up authentication to make this happen. So far I have:

library(Microsoft365R)
#> Warning: package 'Microsoft365R' was built under R version 4.1.2

# this sends an email from <videostudy@psych.wisc.edu>
email <- get_business_outlook()
#> Loading Microsoft Graph login for default tenant

email$create_email(
  body = "body", 
  subject = "subject",
  to ="katharinescott93@gmail.com"
)$send()

Later on in this same script I would like to send an email from kscott4@wisc.edu, another email I have a username and password for. Do you have any recommendations on how to do this or pointers to relevant portions of the documentation? The Azure universe is a little overwhelming and I'm not sure where to look.

Created on 2022-09-09 by the reprex package (v2.0.1)

sformel-usgs commented 1 year ago

Have you tried making multiple R objects, one for each outlook account connection? Something like this:

#default user
email <- get_business_outlook()

#other email address
email2 <- get_business_outlook(shared_mbox_email = "kscott4@wisc.edu")

Then you could reference the appropriate connection at different parts of your script.

hongooi73 commented 1 year ago

What @sformel-usgs said. You would need to have a separate outlook object for each account that you want to send email from.

Authenticating them all might be tricky though. The default is to authenticate via your browser, which might assume you want to sign in to the same account each time. If you're having problems, reply here.

Chicago-Dave commented 1 year ago

Have you tried making multiple R objects, one for each outlook account connection? Something like this:

#default user
email <- get_business_outlook()

#other email address
email2 <- get_business_outlook(shared_mbox_email = "kscott4@wisc.edu")

Then you could reference the appropriate connection at different parts of your script.

Wish there was more documentation available to cover tactics like this. Thanks

sformel-usgs commented 1 year ago

I should add that I never got this working. I had one round of testing with the IT folks, but we never seemed to be able to get the functions to use a specific email address. For example, if I ran get_business_outlook(shared_mbox_email = "kscott4@wisc.edu") I would receive an error in my browser that it needed admin approval. The IT folks couldn't figure out what needed to be adjusted. If I just ran get_business_outlook(), it authenticated without a problem.

I would be interested to know if other folks can pull this off. That would help me understand if it's our IT setup, or something in the code.

Chicago-Dave commented 12 months ago

I use it successfully -- the email address I am using is for our office and it has shared access, not another individual account.