Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending an email from R using the sendmailR package

Tags:

email

macos

r

I am trying to send an email from R, using the sendmailR package. The code below works fine when I run it on my PC, and I recieve the email. However, when I run it with my macbook pro, it fails with the following error:

library(sendmailR)
from <- sprintf("<sendmailR@%s>", Sys.info()[4])
to <- "<[email protected]>"
subject <- "TEST"
sendmail(from, to, subject, body,
    control=list(smtpServer="ASPMX.L.GOOGLE.COM"))

Error in socketConnection(host = server, port = port, blocking = TRUE) : 
  cannot open the connection
In addition: Warning message:
In socketConnection(host = server, port = port, blocking = TRUE) :
  ASPMX.L.GOOGLE.COM:25 cannot be opened

Any ideas as to why this would work on a PC, but not a mac? I turned the firewall off on both machines.

like image 625
Zach Avatar asked May 03 '11 02:05

Zach


1 Answers

Are you able to send email via the command-line?

So, first of all, fire up a Terminal and then

$ echo “Test 123” | mail -s “Test” [email protected]

Look into /var/log/mail.log, or better use

$ tail -f /var/log/mail.log 

in a different window while you send your email. If you see something like

... setting up TLS connection to smtp.gmail.com[xxx.xx.xxx.xxx]:587
... Trusted TLS connection established to smtp.gmail.com[xxx.xx.xxx.xxx]:587:\
    TLSv1 with cipher RC4-MD5 (128/128 bits)

then you succeeded. Otherwise, it means you have to configure you mailing system. I use postfix with Gmail for two years now, and I never had have problem with it. Basically, you need to grab the Equifax certificates, Equifax_Secure_CA.pem from here: http://www.geotrust.com/resources/root-certificates/. (They were using Thawtee certificates before but they changed last year.) Then, assuming you used Gmail,

  1. Create relay_password in /etc/postfix and put a single line like this (with your correct login and password):

    smtp.gmail.com [email protected]:password
    

    then in a Terminal,

    $ sudo postmap /etc/postfix/relay_password 
    

    to update Postfix lookup table.

  2. Add the certificates in /etc/postfix/certs, or any folder you like, then

    $ sudo c_rehash /etc/postfix/certs/ 
    

    (i.e., rehash the certificates with Openssl).

  3. Edit /etc/postfix/main.cf so that it includes the following lines (adjust the paths if needed):

    relayhost = smtp.gmail.com:587
    smtp_sasl_auth_enable = yes
    smtp_sasl_password_maps = hash:/etc/postfix/relay_password
    smtp_sasl_security_options = noanonymous
    smtp_tls_security_level = may
    smtp_tls_CApath = /etc/postfix/certs
    smtp_tls_session_cache_database = btree:/etc/postfix/smtp_scache
    smtp_tls_session_cache_timeout = 3600s
    smtp_tls_loglevel = 1
    tls_random_source = dev:/dev/urandom
    
  4. Finally, just reload the Postfix process, with e.g.

    $ sudo postfix reload 
    

    (a combination of start/stop works too).

You can choose a different port for the SMTP, e.g. 465. It’s still possible to use SASL without TLS (the above steps are basically the same), but in both case the main problem is that your login informations are available in a plan text file... Also, should you want to use your MobileMe account, just replace the Gmail SMTP server with smtp.me.com.

like image 174
chl Avatar answered Oct 13 '22 15:10

chl