`
hellosoft
  • 浏览: 54009 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

send email by Ruby

阅读更多
笔者近来正在使用ruby编写数据检查的脚本,由于需要将检查结果发送给指定的管理员,所以就写了一个专门发送email的脚本。
定有很多不足之处,望各位多提宝贵意见!

可以指定发送内容,
也可以指定发送文件,系统会自动将文件中的内容读取,然后发送

require "net/smtp"

# params : 
#   ARGV[0] = subject
#   ARGV[1] = content
#   ARGV[2] = filename
#   ARGV[3] = to

def sendemail(subject,content,to=nil)
	from = "info@163.com"
	to = ["system1@163.com","system2@163.com"] if to.nil?
	sendmessage = "Subject: "+subject +"\n\n"+content
	smtp = Net::SMTP.start("mail.163.com",25)
	smtp.send_message sendmessage,from,to
	smtp.finish
end
def sendemail_file(subject,filename,to)
	content = ""
	File.open(filename) do |file|
	  file.each_line {|line| content += "#{line}\n" }
	end
	sendemail(subject,content,to)
end


subject = ARGV[0] || "system autoly send"
content = ARGV[1] || ""
filename = ARGV[2] || ""
to = ARGV[3] 

if content.to_s == "" and filename.to_s!=""
	sendemail_file(subject,filename,to)
else
  content = "Nothing" if content.to_s == ""
  sendemail(subject,content,to)
end



e.g.例如将文件保存为sendemail.rb)
ruby sendemail.rb subject ok
ruby sendemail.rb subject "" filename.txt system3@163.com
分享到:
评论
3 楼 liyu212 2009-06-18  
/ruby/1.8/net/smtp.rb:679:in `check_response': 550 Mailbox not found (Net::SMTPFatalError)

如何处理对方邮件地址不存在的错误,使程序继续运行,不退出
2 楼 hellosoft 2008-06-23  
实在有些不好意思,因为时间太久了,我不记得了。呵呵,你自己查一下吧,如果还是解决不了,我再查,好不?
1 楼 Jmlover 2008-04-05  
密码是怎么设置的?

在doc中看到,说
smtp = Net::SMTP.start('smtp.163.com', 25, 'xxxx@163.com',
                  'xxxx', 'xxxxpassword', :login)
最后一个参数怎么设置? 好像设置为:login,登录的时候好像不能成功!

相关推荐

Global site tag (gtag.js) - Google Analytics