---
title: "发送ip地址和指定文件到某邮箱"
author: "clark1231"
date: "2012-12-03T15:39:33Z"
tags: ["Mail", "Ruby"]
categories: ["编程语言"]
slug: "ip"
draft: false
lastmod: "2012-12-22T19:31:47Z"
id: 1740310
url: "https://caok1231.iteye.com/blog/1740310"
---

```
#!/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
```