Guest User

Untitled

a guest
Jul 20th, 2018
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1.  
  2. require 'net/smtp'
  3. require 'erb'
  4.  
  5. module Pixelcop
  6. module Net
  7.  
  8. class EmailAddr
  9.  
  10. attr_accessor :email, :name
  11.  
  12. def initialize(email, name = nil)
  13. @email = email
  14. @name = name
  15. end
  16.  
  17. def to_s
  18. if name.nil? then
  19. return @email
  20. end
  21. return "%s <%s>" % [@name, @email]
  22. end
  23.  
  24. end
  25.  
  26. class SMTP
  27.  
  28. attr_accessor :server, :port, :user, :pass, :auth_type
  29.  
  30. def initialize(server, user = nil, pass = nil, port = 25, auth_type = :login)
  31. @server = server
  32. @port = port
  33. @user = user
  34. @pass = pass
  35. @auth_type = auth_type
  36. end
  37.  
  38. # to and cc can be either Strings or Arrays
  39. def send(to, subject, body, cc = nil, from = nil)
  40.  
  41. hostname = `hostname`.strip
  42. from = "ruby script <#{@user}>" if from.nil?
  43.  
  44. if not to.kind_of? Array then
  45. to = [ to ]
  46. end
  47.  
  48. if not cc.kind_of? Array then
  49. cc = [ cc ]
  50. end
  51.  
  52. template = ERB.new <<-EOM
  53. From: <%= from %>
  54. To: <%= to.join(', ') %>
  55. Subject: <%= subject %>
  56.  
  57. <%= body %>
  58. EOM
  59.  
  60. msg = template.result(binding)
  61.  
  62. #puts msg
  63.  
  64. smtp = ::Net::SMTP.new(@server, @port)
  65. #smtp.set_debug_output $stderr
  66.  
  67. if @user.nil? then
  68. smtp.start(hostname)
  69. else
  70. smtp.start(hostname, @user, @pass, @auth_type)
  71. end
  72.  
  73. to.each { |to_addr|
  74. smtp.send_message(msg, from, to_addr)
  75. }
  76. smtp.finish
  77.  
  78. end
  79.  
  80. end
  81.  
  82. end
  83. end
  84.  
  85. # example
  86. smtp = Pixelcop::Net::SMTP.new('mail.operative.com')
  87. smtp.send("dpeterka@operative.com", "yo", "this is my test arrrrrr")
Add Comment
Please, Sign In to add comment