How to send email from a non-gmail account in R - r

I am developing an application in R and it needs to send the email to specific people in the organization.
sender <- "xxxx.yyyy#gmail.com"
recipients <- c("yyyy.xxxx#zzz.com")
send.mail(from = sender,
to = recipients,
subject = "Test mail from Rstudio",
body = "Test email body",
smtp = list(host.name = "smtp.gmail.com", port = 465,
user.name = "xxxx.yyyy#gmail.com",
passwd = "******", ssl = TRUE),
authenticate = TRUE,
send = TRUE)
This code works well when the sender sends a mail from gmail account. What can be done to send a email from a non gmail account (Organization email account)? Is it possible?
Thanks in advance...

if your app runs in a linux box, and the mail subsystem is properly configured:
system("echo 'test body' | mail -s 'test subject' -r sender_addr#foo.com dest_addr#bar.com")

Related

r Blastula smtp_server Gmail Login denied

I am running into an issue with blastula email and SMTP Gmail.
When I run the following code on my local RStudio, it runs perfectly well and the email is sent.
library(blastula)
# To store my credentials with pwd in a file
create_smtp_creds_file(
file = "gmail_creds",
user = "myemail#gmail.com",
host = "smtp.gmail.com",
port = 465,
use_ssl = TRUE
)
email <- blastula::render_email("RMarkdown_Template.Rmd")
email %>%
smtp_send(
to = c("myother#email.com"),
from = c("Your Newsletter" = "myemail#gmail.com"),
subject = "Daily Newsletter",
credentials = creds_file("gmail_creds")
)
But when I run this exact same code on my RStudio on an ec2 instance, I get the following error message and the email is not sent:
Error in curl_fetch_memory(smtp_server, handle = h) : Login denied
FYI I activated the 'Less secure app' on the Gmail side.
Any idea where this problem come from?

Sending office365 emails through R / Shiny

I'm trying to send emails using R, which will eventually be deployed in a Shiny application. I've got an Office 365 account which is able to send email if I log into it but using the SMTP server I'm not getting any connection so far.
I've tried packages mailR & emayili but I'm not getting anywhere for some reason. I've used our local mail server before and that worked but that won't work for a shiny deployment.
According to Outlook I'm supposed to use:
Server name: smtp.office365.com
Port: 587
Encryption method: STARTTLS
But I can't seem to find a package that can do STARTTLS encryption. For other mail providers I see the advice to turn on "less secure apps" but that's not an option for us.
Example code :
library(mailR)
send.mail(from = "Me#companydomain.com",
to = c("you#companydomain.com"),
subject = "Test",
body = "TEST",
smtp = list(host.name = "smtp.office365.com",
port = 587,
user.name = "Me#companydomain.com",
passwd = "SuperSecretPassword",
tls = TRUE),
authenticate = TRUE,
send = TRUE,
debug = TRUE)
# Another option, with emayili:
library(emayili)
smtp <- server(host = "smtp.office365.com",
port = 587,
username = "Me#companydomain.com",
password = "SuperSecretPassword")
email <- envelope(from = "Me#companydomain.com",
to = "you#companydomain.com",
subject = "This is a plain text message!",
text = "Hello!")
smtp(email, verbose = TRUE)
I really hope someone figured out a package that can use STARTTLS or how to encrypt in that way using one of these packages!
Best Regards,
Bob

Can't Authenticate using mailR::send.mail

Code is as follows, The email, password and host name are correct
library(mailR)
library(rJava)
sender <- "sender#email"
recipients <- c("recipient#email")
send.mail(from = sender,
to = recipients,
subject="Subject of the email",
body = "Body of the email",
smtp = list(host.name = "smtp.office365.com", port = 587,
user.name="sender#email", passwd="password", ssl=F,tls = TRUE),
authenticate = T,
send = TRUE,
debug = T)
The debug produces
DEBUG SMTP: protocolConnect login, host=smtp.office365.com, user=sender#email, password=<non-null>
DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM XOAUTH2
DEBUG SMTP: Using mechanism LOGIN
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN failed
This is to replace some code using RDCOMClient as the package is failing to install.

How to send mail using MailR package, when your account has 2 step verification

I am trying to send email from R using MailR package. This works awesome when I try on my personal gmail account. But when I try to run same with my corporate mail id, it doesn't work.
One problem I found is because of 2 step authentication. I have removed 2 step authentication for my personal mail account but unable to for my corporate account. Is there any way I can include a password or authentication in mailR package code?
My code:
library(rJava)
library(mailR)
send.mail(from = "emailid",
to = "emailid",
subject = "Subject of the email",
body = "Please find the attached doc having cluster report",
html = TRUE,
smtp = list(host.name = "smtp.gmail.com",port = 465, user.name="emailid", passwd="*******",ssl = TRUE),
attach.files ="./Prod tracker-new Jul28-Aug2.csv",
authenticate = TRUE,
send = TRUE)

how do you send email from R

I want to send emails from R. This is what I have so far:
library(sendmailR)
from <- "eamil#example.com"
to <- "email2#example.com"
subject <- "Performance Result"
body <- "This is the result of the test:"
mailControl=list(smtpServer="snmpt server address")
sendmail(from=from,to=to,subject=subject,msg=body,control=mailControl)
When I execute this script, my R session hangs. Any ideas what might be happening?
If you need to be able to use an smtp server with authentication you can use the mailR package.
For example using gmail's smtp server:
library(mailR)
sender <- "SENDER#gmail.com"
recipients <- c("RECIPIENT#gmail.com")
send.mail(from = sender,
to = recipients,
subject = "Subject of the email",
body = "Body of the email",
smtp = list(host.name = "smtp.gmail.com", port = 465,
user.name = "YOURUSERNAME#gmail.com",
passwd = "YOURPASSWORD", ssl = TRUE),
authenticate = TRUE,
send = TRUE)
I just tried it out, and it worked for me.
My only differences were I used <> for the from and to:
from = "<email1#dal.ca>"
to = "<email2#gmail.com>"
and my mail control was different, I used
control=list(smtpServer="ASPMX.L.GOOGLE.COM"))
Sorry for bumping up this thread. If you want to send email from R using Microsoft outlook, below is the way to go using the RDCOMClient package. I myself spent a lot of time trying to find an answer on this. I thought it would be useful to have this solution too in this thread for users.
Full credit to #agstudy who provided the original solution in this link - Sending email in R via outlook
library (RDCOMClient)
OutApp <- COMCreate("Outlook.Application")
outMail = OutApp$CreateItem(0)
outMail[["To"]] = "test#test.com"
outMail[["subject"]] = "Test Subject"
outMail[["body"]] = "Body of email"
outMail$Send()
library(mailR)
sender <- "abc#gmail.com"
recipients <- c("bcd#gmail.com","xyz#gmail.com")
send.mail(
from = sender,
to = recipients,
subject="Cash_Collected_Bank_transfer",
Sys.Date(),
"{}", body = Summary1, encoding = "utf-8", smtp =
list(host.name = "smtp.gmail.com", port = 465,
user.name="abc#gmail.com", passwd="abc#1234", ssl=TRUE),
authenticate = TRUE, send = TRUE ,
attach.files = c(path2), html = TRUE , inline = TRUE )
There is a new package called emayili with two very interesting promises:
works on all manner of SMTP servers
has minimal dependencies (or dependencies which are easily satisfied)
It seems early stages but promising nonetheless. Sending email is as simple as:
devtools::install_github("datawookie/emayili")
library(emayili)
library(dplyr)
email <- envelope() %>%
from("alice#yahoo.com") %>%
to("bob#google.com") %>%
subject("This is a plain text message!") %>%
body("Hello!")
smtp <- server(host = "smtp.gmail.com",
port = 465,
username = "bob#gmail.com",
password = "bd40ef6d4a9413de9c1318a65cbae5d7")
smtp(email, verbose = TRUE)
There are two ways to send an email via Gmail, anonymized or authenticated. Here is the code for anonymized:
library(mailR)
send.mail(from = "sender#gmail.com",
to = c("Recipient 1 <recipient1#gmail.com>", "recipient2#gmail.com"),
cc = c("CC Recipient <cc.recipient#gmail.com>"),
bcc = c("BCC Recipient <bcc.recipient#gmail.com>"),
subject = "Subject of the email",
body = "Body of the email",
smtp = list(host.name = "aspmx.l.google.com", port = 25),
authenticate = FALSE,
send = TRUE)
Make sure the recipient emails are Gmail too. It most likely goes to the spam folder in the Gmail account so make sure to mark it "not spammed".
You can find more info here.
I found the simplest way in Ubuntu is to run the one liner Terminal command in R.
No need for password.
try(system("mutt -s 'Run is complete.' youremail#anymail.com < /dev/null", intern = TRUE))
You will need to install mutt in terminal before this.
If you prefer an in-house solution with your server, you can call the linux sendmail.
EMAIL <- myEmail#gmail.com
cmd <- 'subject="Info server";body="This is an email"'
cmd <- paste("echo -e \"Subject:${subject}\n${body}\" | /usr/sbin/sendmail -t \"", EMAIL, "\"")
system(cmd)

Resources