如何用RCurl/Curl包在R中通过SMTP协议发送邮件?
Sending Emails via SMTP with RCurl or curl in R (No RJava Needed)
Absolutely! You can skip mailR and its RJava dependency entirely—both RCurl and the modern curl package let you send emails directly over SMTP. Here are step-by-step examples for both tools:
Option 1: Using RCurl
RCurl leans on libcurl under the hood, so it fully supports SMTP with authentication and encryption. Start by defining your email and server settings:
library(RCurl) # Configure core email/SMTP details smtp_server <- "smtp.office365.com" # Example for Outlook/Office 365 smtp_port <- 587 username <- "your-email@example.com" password <- "your-password-or-app-specific-password" from <- "your-email@example.com" to <- "recipient@example.com" subject <- "Test Email from RCurl" body <- "Hello! This is a test email sent using RCurl via SMTP." # Build the full email content (including required headers) email_content <- paste0( "From: ", from, "\n", "To: ", to, "\n", "Subject: ", subject, "\n\n", body ) # Send the email curlPerform( url = paste0("smtp://", smtp_server, ":", smtp_port), mail.from = from, mail.rcpt = to, upload = TRUE, infile = textConnection(email_content), smtp.username = username, smtp.password = password, ssl.verifypeer = TRUE, # Keep this enabled for security unless troubleshooting ssl.verifyhost = TRUE, verbose = TRUE # Optional: adds debug logs to help diagnose issues )
Quick RCurl Notes:
- For Gmail, use
smtp.gmail.comon port 587, and you’ll need an app-specific password if two-factor authentication is turned on. - If you hit SSL errors, temporarily set
ssl.verifypeer = FALSE(but only for testing—this reduces security).
Option 2: Using the curl Package (Modern Alternative)
The curl package is a more intuitive wrapper around libcurl, with a dedicated helper for sending emails:
library(curl) # Define email content email_details <- list( from = "your-email@example.com", to = "recipient@example.com", subject = "Test Email from curl Package", body = "Hi there! This email was sent using the curl package in R." ) # Define SMTP server settings smtp_settings <- list( server = "smtp.gmail.com", port = 587, username = "your-email@gmail.com", password = "your-app-specific-password", use_ssl = "starttls" # Use "ssl" for port 465, "starttls" for 587 (most common) ) # Send the email curl_send_mail( from = email_details$from, to = email_details$to, subject = email_details$subject, body = email_details$body, smtp_server = smtp_settings$server, smtp_port = smtp_settings$port, smtp_user = smtp_settings$username, smtp_password = smtp_settings$password, use_ssl = smtp_settings$use_ssl )
Quick curl Package Notes:
- For HTML emails, pass a formatted HTML string as
bodyand addcontent_type = "text/html"to thecurl_send_mail()call. - This package handles SSL setup more smoothly than
RCurlfor most providers.
General Best Practices
- Always check your email provider’s official SMTP docs for the correct server address, port, and authentication rules.
- Never hardcode passwords in your script—use environment variables like
Sys.getenv("EMAIL_PASSWORD")instead for security. - If authentication fails, verify if your provider requires enabling "less secure apps" (being phased out by most) or using an app-specific password.
内容的提问来源于stack exchange,提问作者Jan
相关产品推荐
相关产品推荐

