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

后端开发   发布日期:2025年06月19日   浏览次数:105
  1. class String
  2. def has_one_in?(string_Arr)
  3. has = false
  4. string_Arr.each { |word|
  5. has= self.include?(word)
  6. if has
  7. break
  8. end
  9. }
  10. has
  11. end
  12. end
  13. def decode_subject(sub)
  14. if sub.include?("B?")
  15. encode_str = sub.match(/=\?(.*?)\?=/).to_s
  16. encode_str.scan(/=\?(.*?)\?(B\?)(.*?)\?=/)
  17. if $1 != nil
  18. encode = $1.to_s
  19. if encode=='utf8'
  20. encode='utf-8'
  21. end
  22. str = $3.to_s
  23. decode_str = Base64.decode64(str)
  24. convert = Iconv.new("utf-8", encode)
  25. convert1 = Iconv.new("GBK//IGNORE","UTF-8")
  26. begin
  27. decode_str = convert.iconv(decode_str)
  28. rescue
  29. end
  30. return sub.sub(/=\?.*?\?=/,decode_str)
  31. else
  32. return sub
  33. end
  34. else
  35. return sub
  36. end
  37. end
  38. require 'net/pop'
  39. require 'iconv'
  40. require "base64"
  41. pop = Net::POP3.new('mail.******.com','110')
  42. pop.start('username','password')
  43. skip_keywords = ["Out of office","out of office","out of Office","Out of Office","Out Of Office","自动回复","自动答复","外出",
  44. "AutoReply","Autoreply","autoreply","Auto-Re","auto-re","Auto-re","Delayed","delayed"]
  45. 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
  46. File.open("test.txt","w+") do |file|
  47. mail_addr = []
  48. end_num = 0
  49. pop.mails.each do |e|
  50. subj= e.header.split("\r\n").grep(/^Subject:/).to_s
  51. subj = decode_subject(subj)
  52. if !subj.has_one_in?(skip_keywords)
  53. email= e.pop.to_s.scan(email_patern)
  54. email = email.uniq
  55. email.each do |item|
  56. if !item.to_s.include?("relay.greensmtp.com") and !item.to_s.start_with?("3D")
  57. if !mail_addr.include?(item)
  58. mail_addr << item
  59. puts item.to_s + "|" + end_num.to_s
  60. end
  61. end
  62. end
  63. end
  64. end_num+=1
  65. end
  66. file.puts mail_addr
  67. end

以上就是用ruby获取Email邮箱标题并判断的详细内容,更多关于用ruby获取Email邮箱标题并判断的资料请关注九品源码其它相关文章!