第36个标记 » ruby利用mail和sendmail发送邮件

     喜马拉雅的天空

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 代码
  1. IO.popen(sendmail_cmd)  


如何利用mail或sendmail发送邮件

 mail:

  1. mailx -s SUBJECT_CONTENT SEND_TO_EMAIL  

sendmail:

  1. #sendmail -t -v  
  2. To:someone@domain.com  
  3. From:your address  
  4. Subject: your subject  
  5. your email text  
  6. Ctrl_D  

Ruby实现
 
  Emailx:

  1. email_to=["to@domail.com"]  
  2. cc_to=[""ccc@domail.comm"] 
  3. file="attachment-path" 
  4.   sendmail_cmd="(uuencode  #{file};"  
  5.   sendmail_cmd<<"cat body)" 
  6.   sendmail_cmd <<" |mailx -s \"hello\" #{email_to} "  
  7.   pipe=IO.popen(sendmail_cmd)  

     sendmail

  1. pipe=IO.popen("/usr/sbin/sendmail -t -v","w")  
  2. pipe.puts "To: to@domain.com\n"  
  3. pipe.puts "From:Admin<admin@domain.com>\n"  
  4. pipe.puts "Subject: Subject here\n"  
  5. pipe.puts   
  6. pipe.puts "hello,this is email text!"  
  7.   
  8. pipe.close_write  

问题
 其实上面的实现很简单,不过有几个问题:

  • 对于emailx,如何改变from address呢
  • 对于sendmail,如何实现附件呢



1个评论 »

1.   Rodey 发表于 April 26,2009

用linux作服务器,用php写的网站,怎么样在php中实现发邮件?
谢谢!

本文评论的RSS · TrackBack URI

发表评论