send_mail: Send email

View source: R/email.R

send_mailR Documentation

Send email


Use the curl SMTP client to send an email. The message argument must be properly formatted RFC2822 email message with From/To/Subject headers and CRLF line breaks.


  smtp_server = "smtp://localhost",
  use_ssl = c("try", "no", "force"),
  verbose = TRUE,



email address of the sender.


one or more recipient email addresses. Do not include names, these go into the message headers.


either a string or connection with (properly formatted) email message, including sender/recipient/subject headers. See example.


hostname or address of the SMTP server, or, an smtp:// or smtps:// URL. See "Specifying the server, port, and protocol" below.


Request to upgrade the connection to SSL using the STARTTLS command, see CURLOPT_USE_SSL for details. Default will try to SSL, proceed as normal otherwise.


print output


other options passed to handle_setopt. In most cases you will need to set a username and password to authenticate with the SMTP server.

Specifying the server, port, and protocol

The smtp_server argument takes a hostname, or an SMTP URL:

  • - hostname only

  • - hostname and port

  • smtp:// - protocol and hostname

  • smtp:// - full SMTP URL

  • smtps:// - full SMTPS URL

By default, the port will be 25, unless smtps:// is specified–then the default will be 465 instead.

Encrypting connections via SMTPS or STARTTLS

There are two different ways in which SMTP can be encrypted: SMTPS servers run on a port which only accepts encrypted connections, similar to HTTPS. Alternatively, a regular insecure smtp connection can be "upgraded" to a secure TLS connection using the STARTTLS command. It is important to know which method your server expects.

If your smtp server listens on port 465, then use a smtps://hostname:465 URL. The SMTPS protocol guarantees that TLS will be used to protect all communications from the start.

If your email server listens on port 25 or 587, use an smtp:// URL in combination with the use_ssl parameter to control if the connection should be upgraded with STARTTLS. The default value "try" will opportunistically try to upgrade to a secure connection if the server supports it, and proceed as normal otherwise.


## Not run: # Set sender and recipients (email addresses only)
recipients <- readline("Enter your email address to receive test: ")
sender <- ''

# Full email message in RFC2822 format
message <- 'From: "R (curl package)" <>
To: "Roger Recipient" <>
Subject: Hello R user!

Dear R user,

I am sending this email using curl.'

# Send the email
send_mail(sender, recipients, message, smtp_server = 'smtps://',
  username = 'curlpackage', password  = 'qyyjddvphjsrbnlm')
## End(Not run)

curl documentation built on Aug. 14, 2023, 5:09 p.m.