r Blastula smtp_server Gmail Login denied - r

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?

Related

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.

R blastula's send_stmp gives "MAIL failed: 451"

I'm trying to send emails through R's blastula package. My email program is Outlook/Office. This code
library(blastula)
smtp_send(
compose_email(body = "hello"),
to = "hello#hello.com",
from = "hello#hello.com",
credentials = creds(
user = "hello",
host = "smtp.office365.com",
port = 587,
use_ssl = F
))
gives
Error in curl_fetch_memory(smtp_server, handle = h) : MAIL failed: 451
I've done a bit of reading and it clearly has something to do with permissions being blocked, which I assume I can't change because I don't have admin privileges on my machine.
Is there anything I can do?
Thanks!

mailR task blocking R Server port

I've scheduled a weekly task from R Server that consists of generating a csv file and sending it via mailR package:
library(mailR,lib='~/packages')
sender <- "bla#gmail.com"
recipients <- c("bla1#gmail.com","bla2#hotmail.com")
email <- send.mail(from = sender,
to = recipients,
subject=paste0("Task, week ",min(total$week)),
body = "Good morning, \n\nI've attached last week file. \n\nRegards,",
attach.files =c(paste0("~/path/task_", substr(Sys.Date()-7,1,4)," week ",min(total$week),".csv")),
smtp = list(host.name = "smtp.gmail.com", port = 465, user.name = sender,passwd = "******", ssl = TRUE),
authenticate = T,
send = TRUE)
It works fine, but after its first execution, when I try to connect from R Server to Google Analytics API, BigQuery... via token authentication, next message is shown:
Error in httpuv::startServer(use$host, use$port, list(call = listen)) :
Failed to create server
It looks like this task is blocking a port. Any idea about how to avoid this?
Thanks in advance,

Sending an attachment from Shiny

I have created an application using Shiny and uploaded to the server that belongs to shinyapps.io; I have tested it and all is fine. My application creates via the server.R a text file that I would like to send to my email when a user finishes performing a task. I want to send that file to my email because I do not see a way in which to see the files that my shiny application outputs in the shinyapps.io admin tool. So bottomline, how can I send a file from a shiny application to my email?
For example if I have the following:
library(sendmailR)
datos<-read.table("data.txt")
to <- "<loretta#gmail.com>"
subject <- "Email Subject"
body <- "Email body."
mailControl=list(smtpServer="ASPMX.L.GOOGLE.COM")
sendmail(from="localhost",to=to,subject=subject,msg=body,control=mailControl)
attachmentObject <- mime_part(name=datos)
bodyWithAttachment <- list(body,attachmentObject)
sendmail(from="localhost",to=to,subject=subject,msg=bodyWithAttachment,control=mailControl)
I would like to know what I should put in the from part, I mean I have put it localhost, but I need to put the address of where the shiny application is running; from where I can get that?
Also when I run the above code, not in a Shiny environment, but as a script I got the following error after the sendmail part:
Error in wait_for(code) :
SMTP Error: 5.5.2 Syntax error. g22si4860678yhc.87 - gsmtp
any help would be great
This works for me for sending email from outlook using the mailR library:
library(mailR)
bodyMsg <- "Some message to be included in the body of the email"
send.mail(from = "sender#xyz.com", to = "receiver#xyz.com", subject = "Some topic",
body = bodyMsg , authenticate = TRUE, html = TRUE, send = TRUE, attach.files = file.path(folder, fileName),
smtp = list(host.name = "abcdef.xyz.com", port = 587, user.name = "sender#xyz.com", passwd = "password", tls = TRUE))}

Resources