1

I would like to send a mail using SMTPS in R. Currently, non of the available packages supports sending Mails via TLS (rmail & sendmaileR) or they have a hard to install Java dependency (mailr). I tried using curl and managed to send a mail using the following code snippet:

curl --url 'smtps://mail.server.com:465' --ssl-reqd --mail-from 'mail1@example.com' --mail-rcpt 'mail2@example.com' --upload-file mail.txt --user 'user:password'

Unfortunately, I could not translate that snippet into R using the brilliant curl package. While I managed to find all options, the curl statement crashes the R session every time. Furthermore, I was not able to add the mail.txt file to the request which I created in a temporary directory. Did someone manage sending mails using the curl package? Why does the program always crash? The goal should be to send mails on all platforms.

# input variables
to <- "mail1@example.com"
from <- Sys.getenv("MAIL_USER")
password <- Sys.getenv("MAIL_PASSWORD")
server <- Sys.getenv("MAIL_SERVER")
port <- 465
subject <- "Test Mail"
message <- c("Hi there!",
             "This is a test message.",
             "Cheers!")

# compose email body
header <- c(paste0('From: "', from, '" <', from, '>'),
            paste0('To: "', to, '" <', to, '>'),
            paste0('Subject: ', subject))
body <- c(header, "", message)

# create tmp file to save mail text
mail_file <- tempfile(pattern = "mail_", fileext = ".txt")
file_con <- file(mail_file)
writeLines(body, file_con)
close(file_con)

# define curl options
handle <- curl::new_handle()
curl::handle_setopt(handle = handle,
                    mail_from = from,
                    mail_rcpt = to,
                    use_ssl = TRUE,
                    port = port,
                    userpwd = paste(from, password, sep = ":"))
con <- curl::curl(url = server, handle = handle)
open(con, "r")
close(con)

# delete file
unlink(mail_file)
Jan
  • 186
  • 8
  • What does "crash" mean? – IRTFM Mar 09 '19 at 21:38
  • The session just freezes and I've to close it by killing the process. I'm on Ubuntu 18.10 using R 3.5.2. – Jan Mar 09 '19 at 21:42
  • When I execute `Sys.getenv("MAIL_USER")` (also on Ubuntu and R 3.5) I get `""`. You are not telling us enough to assist further since we cannot tell what values you might have in your system. – IRTFM Mar 09 '19 at 21:46
  • The system variables include the required login information for the mail server and are set in the .Renviron file. They equal the credentials and settings used in the curl statement. – Jan Mar 09 '19 at 21:54

0 Answers0