使用sendmailR包从R发送电子邮件

时间:2011-05-03 02:13:47

标签: macos email r

我正在尝试使用sendmailR包从R发送电子邮件。当我在我的电脑上运行它时,下面的代码工作正常,我收到了电子邮件。但是,当我使用我的macbook pro运行它时,它会因以下错误而失败:

library(sendmailR)
from <- sprintf("<sendmailR@%s>", Sys.info()[4])
to <- "<myemail@gmail.com>"
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

为什么这可以在PC上运行,而不是mac?我在两台机器上关闭了防火墙。

1 个答案:

答案 0 :(得分:26)

您是否可以通过命令行发送电子邮件?

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

$ echo “Test 123” | mail -s “Test” user@domain.com

查看/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)

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

  1. relay_password中创建/etc/postfix并添加一行(使用正确的登录名和密码):

    smtp.gmail.com login@gmail.com:password
    

    然后在终端,

    $ sudo postmap /etc/postfix/relay_password 
    

    更新Postfix查找表。

  2. /etc/postfix/certs或您喜欢的任何文件夹中添加证书,然后

    $ 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. 最后,只需重新加载Postfix流程即可。

    $ sudo postfix reload 
    

    start / stop的组合也适用。)

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

相关问题