1

我想在 R 中使用 SMTPS 发送邮件。目前,没有可用的包支持通过 TLS ( rmail& sendmaileR) 发送邮件,或者它们很难安装 Java 依赖项 ( mailr)。我尝试使用 curl 并设法使用以下代码段发送邮件:

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'

不幸的是,我无法使用辉煌curl包将那个片段翻译成 R。虽然我设法找到了所有选项,但 curl 语句每次都会使 R 会话崩溃。此外,我无法将mail.txt文件添加到我在临时目录中创建的请求中。有人使用 curl 包管理发送邮件吗?为什么程序总是崩溃?目标应该是在所有平台上发送邮件。

# 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)
4

0 回答 0