• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

用ruby获取Email邮箱标题并判断

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
class String
  def has_one_in?(string_Arr)
    has = false
    string_Arr.each { |word|  
      has= self.include?(word)
      if has
        break
      end
    }
    has
  end
end
def decode_subject(sub)
  if sub.include?("B?")
    encode_str = sub.match(/=\?(.*?)\?=/).to_s
    encode_str.scan(/=\?(.*?)\?(B\?)(.*?)\?=/)
    if $1 != nil
      encode = $1.to_s
      if encode=='utf8'
        encode='utf-8'
      end
      str = $3.to_s
      decode_str = Base64.decode64(str)
      convert = Iconv.new("utf-8", encode)
      convert1 = Iconv.new("GBK//IGNORE","UTF-8")
      begin
        decode_str = convert.iconv(decode_str)
      rescue
      end
      return sub.sub(/=\?.*?\?=/,decode_str)
    else
      return sub
    end
  else
    return sub
  end
end
require 'net/pop'
require 'iconv'
require "base64"
pop = Net::POP3.new('mail.******.com','110')
pop.start('username','password')
skip_keywords = ["Out of office","out of office","out of Office","Out of Office","Out Of Office","自动回复","自动答复","外出",
  "AutoReply","Autoreply","autoreply","Auto-Re","auto-re","Auto-re","Delayed","delayed"]
email_patern = %r{([0-9a-zA-Z]{1}[0-9a-zA-Z.-]{1,40}@[0-9a-zA-Z.-]+[-\w]*[0-9a-zA-Z]*\.+[a-zA-Z]{2,15})}i

File.open("test.txt","w+") do |file|
  mail_addr = []
  end_num = 0
  pop.mails.each do |e|  
    subj= e.header.split("\r\n").grep(/^Subject:/).to_s
    subj = decode_subject(subj)
    if !subj.has_one_in?(skip_keywords)
      email= e.pop.to_s.scan(email_patern)
      email = email.uniq
      email.each do |item|
        if !item.to_s.include?("relay.greensmtp.com") and !item.to_s.start_with?("3D")
          if !mail_addr.include?(item)
            mail_addr << item
            puts item.to_s + "|" + end_num.to_s
          end
        end
      end
    end
    end_num+=1 
  end
  file.puts mail_addr
end

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap