send email by Ruby

本文介绍了一款使用Ruby编写的简单邮件发送脚本,该脚本可根据提供的参数发送纯文本内容或从文件中读取内容并发送。适用于系统维护人员进行自动化任务通知。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

笔者近来正在使用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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值