ITeye博客

发送ip地址和指定文件到某邮箱

clark1231 · 2012-12-03 15:39 · ruby · 编程语言 · Ruby · Mail · Markdown

#!/usr/bin/env ruby
#
# ARGV[0] - msg
# ARGV[1] - mailto
# ARGV[2] - filename

require 'open-uri'
require 'rubygems'
require 'action_mailer'

ActionMailer::Base.smtp_settings = {
  :address              => 'smtp.163.com',
  :port                 => 25,
  :domain               => '163.com',
  :user_name            => 'xxfromxx@163.com',
  :password             => 'xxxxxxx',
  :authentication       => 'login',
  :enable_starttls_auto => true
}

class Notifer < ActionMailer::Base
  default :from => 'xxfromxx@163.com'

  def mailip(ip, msg, mailto, file)
    attachments["#{file}"] = File.read("#{file}")
    mail :to => mailto, :subject => "#{msg} ip is #{ip}" do |format|
      format.text { render :text => ip}
    end.deliver
  end
end

open("http://checkip.dyn.com") do |f|
  ip = f.read.slice /[0-9]+(\.[0-9]+){3}/
  filename = "the_file_what_you_want_to_send.xxx"
  Notifer.mailip ip, ARGV[0], ARGV[1] || 'xxtoxx@163.com', ARGV[2] || filename
end

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。