Error when using mailR package on Ubuntu machine - r

I have the following code:
text <- 'sample'
sender <- "***"
recipients <- c("***")
send.mail(from = sender,
to = recipients,
subject = text,
body = text,
smtp = list(host.name = "smtp.gmail.com", port = 465,
user.name = "***",
passwd = "***", ssl = TRUE),
authenticate = TRUE,
send = TRUE)
The text variable is in my case a value that is fetched from a database but this makes it easier. The code above works fine when I run it on my local machine. However when I run it on Linux Ubuntu the exact same code gives me the following error:
org.apache.commons.mail.EmailException: Sending the email to the following
server failed : smtp.gmail.com:465
at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1410)
at org.apache.commons.mail.Email.send(Email.java:1437)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl. java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces sorImpl.java:43)
Can anybody tell me what could be the reason that I get this error on Linux enviroment (and not when I run it local)?

Related

Connection reset by peer when trying to connect to Postgres via SSL using R

I'm trying to connect to a database via SSL using the code suggested here: https://github.com/ropensci/ssh/issues/13
I listed the dummy code below that shows how I enable the connection and try to query some data. The solution works great for 'smaller' queries.
However, when I try to get 'larger' data, the query fails and R gives back the following error:
System failure for: recv() from user (Connection reset by peer) follwed by a fetching error Failed to fetch row: SSL error: decryption failed or bad record mac (see output in code snipped)
Accordingly to the 1st error message, I suppose the error occurs served-sided ('reset by peer' --> What does "connection reset by peer" mean?).
Is that true or is there a way to fix this error on the client side (in R)?
ssh::ssh_read_key(file = ssh::ssh_home("id_rsa"), password = "rsa_password")
cmd <- "session <- ssh::ssh_connect('user#host:port');ssh::ssh_tunnel(session, port = 5432, target = '127.0.0.1:5432')"
pid <- sys::r_background(std_out = T, args = c("-e", cmd))
dbcon <-DBI::dbConnect(drv = RPostgres::Postgres(),
dbname = "db_name",
host = "127.0.0.1",
port = 5432,
user = "db_user",
password = "db_password",
base::list(sslmode="require"),
service = NULL)
# example of working query
res <- DBI::dbGetQuery(conn = dbcon, statement = "SELECT * FROM small_table") #
# example of non-working query (see R-otutput)
res <- DBI::dbGetQuery(conn = dbcon, statement = "SELECT * FROM large_table;") #
## R-output
# Tunneled 31897311 bytes...Fehler: System failure for: recv() from user (Connection reset by peer)
# Ausführung angehalten
# Fehler: Failed to fetch row: SSL error: decryption failed or bad record mac
# Warnmeldung:
# Disconnecting from unused ssh session. Please use ssh_disconnect()
"Connection reset by peer" means that whatever you have tried connecting to has responded in an RST flag, meaning that they have reset the connection.

Cannot find function src_dbi in R

I am trying to use R to access postgresql db on Heroku, and I found that I can use src_dbi from dplyr package.
I have dplyr properly installed, but when I try to call src_dbi I get an error message:
Error in src_dbi(db_con) : could not find function "src_dbi"
This happens right when I run:
db <- src_dbi(db_con)
after poviding the credentials:
config <- run("heroku", c("config:get", "postgres://xxxxxxxxxxxxxx
", "-a", "prjectAlpha"))
pg <- httr::parse_url(config$stdout)
dbConnect(RPostgres::Postgres(),
dbname = "xxxxxxxxxx",
host = "xxxxxxxxx.amazonaws.com",
port = 5432,
user = "xxxxxx",
password = "xxxxxxxxxxxxxxxxx",
sslmode = "require"
) -> db_con
The idea is to be able to download a table and re-upload it after making a few changes with R.
My solution to access Heroku Postgresql from R:
library(dbplyr) #in case you have an error, run: system("defaults write org.R-project.R force.LANG en_US.UTF-8") from Rails console, then restart R.
library(processx)
library(RPostgres)
library(httr)
library(tidyverse)
library(dplyr)
config <- run("heroku", c("config:get", "postgres://xxxxxxxxxxxxxx
", "-a", "prjectAlpha"))
pg <- httr::parse_url(config$stdout)
dbConnect(RPostgres::Postgres(),
dbname = "xxxxxxxxxx",
host = "xxxxxxxxx.amazonaws.com",
port = 5432,
user = "xxxxxx",
password = "xxxxxxxxxxxxxxxxx",
sslmode = "require"
) -> db_con
Once the connection is set up:
db <- src_dbi(db_con)
Once the connection is established, check for the available tables
db
Now time to retrive data
A lot of examples only show treatment directly from the collection, but one might just be interested to read the tables. I have a table called "weather_records"
weather_records_local_df <- tbl(db_con, "weather_records")
df <- collect(weather_records_local_df)
Then do what you want with the data. Hope it helps.

How to send a mail via SMTPS using the curl package in R?

I would like to send a mail using SMTPS in R. Currently, non of the available packages supports sending Mails via TLS (rmail & sendmaileR) or they have a hard to install Java dependency (mailr). I tried using curl and managed to send a mail using the following code snippet:
curl --url 'smtps://mail.server.com:465' --ssl-reqd --mail-from 'mail1#example.com' --mail-rcpt 'mail2#example.com' --upload-file mail.txt --user 'user:password'
Unfortunately, I could not translate that snippet into R using the brilliant curl package. While I managed to find all options, the curl statement crashes the R session every time. Furthermore, I was not able to add the mail.txt file to the request which I created in a temporary directory. Did someone manage sending mails using the curl package? Why does the program always crash? The goal should be to send mails on all platforms.
# input variables
to <- "mail1#example.com"
from <- Sys.getenv("MAIL_USER")
password <- Sys.getenv("MAIL_PASSWORD")
server <- Sys.getenv("MAIL_SERVER")
port <- 465
subject <- "Test Mail"
message <- c("Hi there!",
"This is a test message.",
"Cheers!")
# compose email body
header <- c(paste0('From: "', from, '" <', from, '>'),
paste0('To: "', to, '" <', to, '>'),
paste0('Subject: ', subject))
body <- c(header, "", message)
# create tmp file to save mail text
mail_file <- tempfile(pattern = "mail_", fileext = ".txt")
file_con <- file(mail_file)
writeLines(body, file_con)
close(file_con)
# define curl options
handle <- curl::new_handle()
curl::handle_setopt(handle = handle,
mail_from = from,
mail_rcpt = to,
use_ssl = TRUE,
port = port,
userpwd = paste(from, password, sep = ":"))
con <- curl::curl(url = server, handle = handle)
open(con, "r")
close(con)
# delete file
unlink(mail_file)

R: change port to connect with SFTP server

I have a connection with an ftp server with the following code:
url <- "ftp://MyServer"
userpwd <- "MyUser:MyPass"
filenames <- getURL(url, userpwd = userpwd, ftp.use.epsv = FALSE, dirlistonly = TRUE, port = 22)
filen <- "MyFile.csv"
rawdata <- getURL(paste(url, filen, sep = ""), userpwd = userpwd, crlf = TRUE)
The file will be moved to an SFTP server, so I need to change the input. This new SFTP server is accessed via port 22 instead of the standard port 21. At the moment the connection fails with the following error
Error in function (type, msg, asError = TRUE) :
Failed to connect to MyServer port 21: Connection refused
It takes the wrong port, but how do I tell R to choose port 22?
You need to specify the SFTP protocol in the URL, so the line
url <- "ftp://MyServer"
should become
url <- "sftp://MyServer"
getUrl will then use the SSH port (22).

Cannot acess to a local database via R

I am trying to access to a database HWSD.md which can be downloaded here : http://webarchive.iiasa.ac.at/Research/LUC/External-World-soil-database/HTML/HWSD_Data.html?sb=4
I have a linux machine and I tried this command line but I have a message error
require(MonetDB.R)
Mydb <- src_monetdb("*/HWSD.mdb")
Error in socketConnection(host = host, port = port, blocking = TRUE, open = "r+b", :
cannot open the connection
In addition: Warning message:
In socketConnection(host = host, port = port, blocking = TRUE, open = "r+b", :
localhost:50000 cannot be opened
Anyone can help me out with it?

Resources