2011-05-03 119 views
14

我试图使用sendmailR软件包从R发送电子邮件。下面的代码在我的电脑上运行时工作正常,我收到了电子邮件。然而,当我跟我的MacBook Pro运行它,它失败,出现以下错误:使用sendmailR软件包从R发送电子邮件

library(sendmailR) 
from <- sprintf("<[email protected]%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 

任何想法,为什么会在电脑上工作,但不是Mac?我在两台机器上都关闭了防火墙。

+0

你有没有试过不同的端口,即587? – wkmor1 2011-05-03 02:28:31

+0

@ wkmor1我试图在sendmailR控制参数中设置端口587,但它似乎仍然在25以上通信。任何想法? – Zach 2011-05-03 02:39:06

回答

26

你能通过命令行发送电子邮件吗?

所以,首先,启动终端,然后

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

查找到/var/log/mail.log,或更好地利用

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

在不同的窗口,而您发送电子邮件。如果你看到类似

... 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) 

那么你成功了。否则,这意味着你必须配置你的邮件系统。我在Gmail上使用postfix已有两年了,而且我从来没有遇到任何问题。基本上,您需要从这里获取Equifax证书Equifax_Secure_CA.pemhttp://www.geotrust.com/resources/root-certificates/。 (他们之前使用Thawtee证书,但他们去年改变了。)然后,假设你使用Gmail时,

  1. 创建/etc/postfixrelay_password,把一行像这样(与你的正确的用户名和密码):

    smtp.gmail.com [email protected]:password 
    

    然后在终端,

    $ sudo postmap /etc/postfix/relay_password 
    

    更新Postfix的查找表。

  2. 添加证书中​​,或任何文件夹你喜欢,然后

    $ sudo c_rehash /etc/postfix/certs/ 
    

    (即,重新散列使用OpenSSL的证书)。

  3. 编辑/etc/postfix/main.cf以便它包括以下各行(如果需要调整的路径):

    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. 最后,只是重新加载后缀过程中,用例如

    $ sudo postfix reload 
    

    (的start/stop作品的组合太)。

您可以为SMTP选择不同的端口,例如, 465. 仍然可以使用SASL而不使用TLS(上述步骤基本相同),但在这两种情况下主要问题在于您的登录信息在计划文本文件中可用。此外,如果您想使用MobileMe帐户,只需用smtp.me.com替换Gmail SMTP服务器即可。

+3

+1感谢您提供非常详细的说明 – 2014-02-11 05:28:11