笔者近来正在使用ruby编写数据检查的脚本,由于需要将检查结果发送给指定的管理员,所以就写了一个专门发送email的脚本。
定有很多不足之处,望各位多提宝贵意见!
可以指定发送内容,
也可以指定发送文件,系统会自动将文件中的内容读取,然后发送
e.g. :(例如将文件保存为sendemail.rb)
ruby sendemail.rb subject ok
ruby sendemail.rb subject "" filename.txt system3@163.com
定有很多不足之处,望各位多提宝贵意见!
可以指定发送内容,
也可以指定发送文件,系统会自动将文件中的内容读取,然后发送
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