Search code examples
routlooksendmailr

Is there a way to read a text file and send it as the body of an email using R. I want to send the email using an outlook account


I know that this is possible with python. However, I want to know if there is a way to do this with R as well. Code snippets will be appreciated.


Solution

  • There are diverse ways, see this overview for example. SMTP settings for Outlook and Office365 can be found here.

    Fill your credentials in the following snippet and it should work straight away:

    install.packages("remotes")
    library(remotes)
    remotes::install_github("datawookie/emayili")
    
    install.packages("magrittr")
    
    library(magrittr) 
    library(dplyr)  
    library(emayili)
    
    msg <- envelope() %>%
      from("[email protected]") %>%
      to("[email protected]") %>%
      subject("Test email subject") %>%
      attachment("./yourFile.txt") %>%
      text("Test email body")
    
    smtp <- server(host = "smtp.office365.com",
               port = 587,
               username = "username",
               password = "password")
    
    smtp(msg, verbose = TRUE)