Debian Codegraph @

R/email.R

raw
1 #' Send email
2 #'
3 #' Use the curl SMTP client to send an email. The `message` argument must be
4 #' properly formatted [RFC2822](https://www.rfc-editor.org/rfc/rfc2822) email message with From/To/Subject headers and CRLF
5 #' line breaks.
6 #'
7 #' @section Specifying the server, port, and protocol:
8 #'
9 #' The `smtp_server` argument takes a hostname, or an SMTP URL:
10 #'
11 #' \itemize{
12 #' \item `mail.example.com` - hostname only
13 #' \item `mail.example.com:587` - hostname and port
14 #' \item `smtp://mail.example.com` - protocol and hostname
15 #' \item `smtp://mail.example.com:587` - full SMTP URL
16 #' \item `smtps://mail.example.com:465` - full SMTPS URL
17 #' }
18 #'
19 #' By default, the port will be 25, unless `smtps://` is specified--then
20 #' the default will be 465 instead.
21 #'
22 #' For internet SMTP servers you probably need to pass a
23 #' [username](https://curl.se/libcurl/c/CURLOPT_USERNAME.html) and
24 #' [passwords](https://curl.se/libcurl/c/CURLOPT_PASSWORD.html) option.
25 #' For some servers you also need to pass a string with
26 #' [login_options](https://curl.se/libcurl/c/CURLOPT_LOGIN_OPTIONS.html)
27 #' for example `login_options="AUTH=NTLM"`.
28 #'
29 #' @section Encrypting connections via SMTPS or STARTTLS:
30 #'
31 #' There are two different ways in which SMTP can be encrypted: SMTPS servers
32 #' run on a port which only accepts encrypted connections, similar to HTTPS.
33 #' Alternatively, a regular insecure smtp connection can be "upgraded" to a
34 #' secure TLS connection using the STARTTLS command. It is important to know
35 #' which method your server expects.
36 #'
37 #' If your smtp server listens on port 465, then use a `smtps://hostname:465`
38 #' URL. The SMTPS protocol *guarantees* that TLS will be used to protect
39 #' all communications from the start.
40 #'
41 #' If your email server listens on port 25 or 587, use an `smtp://` URL in
42 #' combination with the `use_ssl` parameter to control if the connection
43 #' should be upgraded with STARTTLS. The default value `"try"` will
44 #' *opportunistically* try to upgrade to a secure connection if the server
45 #' supports it, and proceed as normal otherwise.
46 #'
47 #' @export
48 #' @param mail_rcpt one or more recipient email addresses. Do not include names,
49 #' these go into the `message` headers.
50 #' @param mail_from email address of the sender.
51 #' @param message either a string or connection with (properly formatted) email
52 #' message, including sender/recipient/subject headers. See example.
53 #' @param smtp_server hostname or address of the SMTP server, or, an
54 #' `smtp://` or `smtps://` URL. See "Specifying the server, port,
55 #' and protocol" below.
56 #' @param use_ssl Request to upgrade the connection to SSL using the STARTTLS command,
57 #' see [CURLOPT_USE_SSL](https://curl.se/libcurl/c/CURLOPT_USE_SSL.html)
58 #' for details. Default will try to SSL, proceed as normal otherwise.
59 #' @param verbose print output
60 #' @param ... other options passed to [handle_setopt()]. In most cases
61 #' you will need to set a `username` and `password` or `login_options`
62 #' to authenticate with the SMTP server, see details.
63 #' @examples \dontrun{# Set sender and recipients (email addresses only)
64 #' recipients <- readline("Enter your email address to receive test: ")
65 #' sender <- '[email protected]'
66 #'
67 #' # Full email message in RFC2822 format
68 #' message <- 'From: "R (curl package)" <[email protected]>
69 #' To: "Roger Recipient" <[email protected]>
70 #' Subject: Hello R user!
71 #'
72 #' Dear R user,
73 #'
74 #' I am sending this email using curl.'
75 #'
76 #' # Send the email
77 #' send_mail(sender, recipients, message, smtp_server = 'smtps://smtp.gmail.com',
78 #' username = 'curlpackage', password = 'qyyjddvphjsrbnlm')}
79 send_mail <- function(mail_from, mail_rcpt, message, smtp_server = 'smtp://localhost',
80 use_ssl = c("try", "no", "force"), verbose = TRUE, ...){
81 if(!grepl('://', smtp_server)) {
82 # no protocol was provided
83 smtp_server <- if (grepl(":465$", smtp_server)) {
84 paste0('smtps://', smtp_server)
85 } else {
86 paste0('smtp://', smtp_server)
87 }
88 }
89 if (!grepl('^smtps?://', smtp_server)) {
90 stop('smtp_server invalid; only smtp:// and smtps:// URLs are supported')
91 }
92
93 # Values from: curl_symbols('CURLUSESSL')
94 if(is.character(use_ssl)){
95 use_ssl <- switch(match.arg(use_ssl), no = 0, try = 1, force = 2)
96 }
97
98 if(is.character(message))
99 message <- charToRaw(paste(message, collapse = '\r\n'))
100 con <- if(is.raw(message)){
101 rawConnection(message)
102 } else if(inherits(message, "connection")){
103 if(!isOpen(message))
104 open(message, 'rb')
105 message
106 } else {
107 stop("Body must be a string, raw vector, or connection object")
108 }
109 on.exit(close(con))
110 total_bytes <- 0
111 h <- new_handle(upload = TRUE, readfunction = function(nbytes, ...) {
112 buf <- readBin(con, raw(), nbytes)
113 total_bytes <<- total_bytes + length(buf)
114 if(verbose){
115 if(length(buf)){
116 cat(sprintf("\rUploaded %.0f bytes...", total_bytes), file = stderr())
117 } else {
118 cat(sprintf("\rUploaded %.0f bytes... all done!\n", total_bytes), file = stderr())
119 }
120 }
121 return(buf)
122 }, mail_from = mail_from, mail_rcpt = mail_rcpt, use_ssl = use_ssl,
123 verbose = verbose, ...)
124 curl_fetch_memory(smtp_server, handle = h)
125 }