0

I have a data frame of names and mails, and I would i like to create a loop where R sends a mail to all of these people but with their respective names using the RDCOMClient library. The data frame is

df <- data.frame("Name" = c("Name1", "Name2"), "Mail" = c("mail1@mail.com", "mail2@mail.com"))

As I have just used Python in an exam project, I created loops several times with mulitple variables, such as:

for i,j in zip(df[1], df[2])

My code would ideally be something like


for (mail in df$Mail, name in df$Name) {

  outApp <- COMCreate("Outlook.Application")
  outMail = outApp$CreateItem(0)

  outMail[["To"]] = mail
  outMail[["subject"]] = "Subject"
  outMail[["body"]] = paste(" Dear", name, "\n \n bla bla bla.")

  outMail$Send()

  Sys.sleep(0.5)

  if (mail == df$Mail[-1]) cat("Done!")
}

This, however, gives an error.

The reason I would like to use a loop is for two reasons:

  1. I would like R to make a pause before starting the next iteration (I don't know if this is really necessary but if my data frame has over a hundred mails it would propably be better - I could be wrong here).
  2. I would like R to print the message "Done!" when it is done.

If you have other suggestions (I have seen several recommendations on the lapply, sapply, etc. packages) they are very welcom!

I hope there's someone out there who knows just what to do.

Thanks in advance,

Emil

  • I cannot reproduce your question cause I'm not on windows. But generally in R, because it's primarily a functional languange, we abstract code into functions and then apply these functions to list/vectors/data.frames etc. So one strategy that is likely to be more efficient is to replace your for-loop with a function and then apply this function to your data.frame using, for example, the package `purrr`. https://purrr.tidyverse.org/reference/index.html – FilipW May 27 '19 at 09:14
  • In a nutshell ```outMail[["To"]]``` and ```outMail[["body"]]``` has to be fed a string with an email address and the body of the mail (where the iterating part is the name) simultaneously. Do you have a suggestion to how I can do this using the purrr library? – Emil Nyboe Blicher May 27 '19 at 09:38
  • What kind of error are you getting? – FilipW May 27 '19 at 11:16

1 Answers1

0

I cannot reproduce your question cause I am not on windows but here is an example using the package mailR.

library(mailR)
library(purrr)
df <- tibble("Name" = c("Name1", "Name2"), "Mail" = c("mail1@mail.com", "mail2@mail.com"))

mail_fun <- function(name, mail){
send.mail(from = "sender@gmail.com",
          to = mail,
          subject = "Subject of the email",
          body = paste(" Dear", name, "\n \n bla bla bla."),
          smtp = list(host.name = "aspmx.l.google.com", port = 25),
          authenticate = FALSE,
          send = TRUE)

  Sys.sleep(0.5)

  print("Done!")

}

mail_fun("filip", "filip.wastberg@ferrologic.se")

map2(df$Name, df$Mail, ~mail_fun(name = .x, mail = .y))

This should get you an overall idea how to put your code in a function and then use purrr to iterate over a data.frame.

FilipW
  • 1,412
  • 1
  • 13
  • 25
  • 1
    Thank you! I see the logic in there. Now I just have to find out how to use mailR with Outlook. – Emil Nyboe Blicher May 27 '19 at 11:55
  • With the help of your function, I could still use the RDCOMClient package. Thank you very much! – Emil Nyboe Blicher May 27 '19 at 12:02
  • That's great. If you have future questions for Stack overflow I strongly recommend that you read through this: https://community.rstudio.com/t/faq-whats-a-reproducible-example-reprex-and-how-do-i-do-one/5219 You will find that the rstats-community is even more helpful if you provide a "reprex" :) – FilipW May 27 '19 at 12:05
  • I will take a look at that for future questions/comments. Thank you! – Emil Nyboe Blicher May 28 '19 at 14:30