ruby利用mail和sendmail发送邮件
Confach 发表于 September 13, 2007 4:50 pm
版权信息 :严禁转载, 若想推荐或收藏,请用链接的形式.
网址:http://www.36sign.com/blog/working/send-mail-with-email-and-sendmail-in-ruby.html
在linux上一般都带有mailx,mail或者sendmail,那么如何利用ruby来发送邮件呢?
实现起来并不太难.
思路
mail和sendmail都是linux下的命令,也就是说得让ruby调用这些命令,如何调用,那就是
ruby 代码
- IO.popen(sendmail_cmd)
如何利用mail或sendmail发送邮件
mail:
- mailx -s SUBJECT_CONTENT SEND_TO_EMAIL
sendmail:
- #sendmail -t -v
- To:someone@domain.com
- From:your address
- Subject: your subject
- your email text
- Ctrl_D
Ruby实现
Emailx:
- email_to=["to@domail.com"]
- cc_to=[""ccc@domail.comm"]
- file="attachment-path"
- sendmail_cmd="(uuencode #{file};"
- sendmail_cmd<<"cat body)"
- sendmail_cmd <<" |mailx -s \"hello\" #{email_to} "
- pipe=IO.popen(sendmail_cmd)
sendmail
- pipe=IO.popen("/usr/sbin/sendmail -t -v","w")
- pipe.puts "To: to@domain.com\n"
- pipe.puts "From:Admin<admin@domain.com>\n"
- pipe.puts "Subject: Subject here\n"
- pipe.puts
- pipe.puts "hello,this is email text!"
- pipe.close_write
问题
其实上面的实现很简单,不过有几个问题:
- 对于emailx,如何改变from address呢
- 对于sendmail,如何实现附件呢
1. Rodey 发表于 April 26,2009
用linux作服务器,用php写的网站,怎么样在php中实现发邮件?
谢谢!