Search code examples
htmlrpdfr-markdownsendmailr

How to email an R markdown report in any format (PDF, HTML, WORD) when it is generated?


I have an R shiny application that generates an R markdown report in any format depending on what the user clicks. I want to email this report to myself every time it is generated I seem to not find much about this online. I wondering if anyone has any idea how to start this


Solution

  • You could try the mailR package. From the mailR github documentation, you can send an email and use attach.files to attach the relevant report.

    library(mailR)
    send.mail(from = "[email protected]",
              to = c("[email protected]", "[email protected]"),
              subject = "Subject of the email",
              body = "Body of the email",
              smtp = list(host.name = "smtp.gmail.com", port = 465, ssl = TRUE,
                          user.name = "gmail_username", passwd = "password"),
              authenticate = TRUE,
              send = TRUE,
              attach.files = c("./download.log"),
              file.names = c("Download log.log"),
              file.descriptions = c("Description for download log"))
    

    sendmailR can achieve a similar result but the attachment is added to the body of the email using mime_part().

    library(sendmailR)
    from <- '[email protected]'
    to   <- '[email protected]'
    subject <- 'Email Subject'
    body <- list('Email body text.',
                 mime_part(x = 'pathToAttachment', y = 'nameOfAttachment'))
    sendmail(from, to, subject, msg = body,
             control = list(smtpServer='ASPMX.L.GOOGLE.COM'))