Can't Authenticate using mailR::send.mail - r

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.

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

How to send email from a non-gmail account in 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")

Send mail with gmail and R

I am simply trying to send an email with R through my gmail account using the mailR package but it does not seem to work.
I get this error:
Error in .jcall("RJavaTools", "Ljava/lang/Object;", "invokeMethod", cl, :
org.apache.commons.mail.EmailException: Sending the email to the following server failed : aspmx.l.google.com:25
Below is the code with anonymized gmail adresses.
install.packages("mailR")
library(mailR)
sender <- "sender#gmail.com" # Replace with a valid address
recipients <- c("receiver1#gmail.com") # Replace with one or more valid addresses
email <- send.mail(from = sender,
to = recipients,
subject="Subject of the email",
body = "Body of the email",
smtp = list(host.name = "aspmx.l.google.com", port = 25),
authenticate = FALSE,
send = FALSE)
email$send() # execute to send email
I finally managed to make it work ! You have to authorize the app on Google. This link really helped me: https://medium.com/airbnb-engineering/using-googlesheets-and-mailr-packages-in-r-to-automate-reporting-c09579e0377f

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)

Resources