用ruby擷取Email郵箱標題並判斷

來源:互聯網
上載者:User
class String  def has_one_in?(string_Arr)    has = false    string_Arr.each { |word|        has= self.include?(word)      if has        break      end    }    has  endenddef 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  endendrequire '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})}iFile.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_addrend
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.