mail.rb 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. # encoding: utf-8
  2. module Mail
  3. # Allows you to create a new Mail::Message object.
  4. #
  5. # You can make an email via passing a string or passing a block.
  6. #
  7. # For example, the following two examples will create the same email
  8. # message:
  9. #
  10. # Creating via a string:
  11. #
  12. # string = "To: mikel@test.lindsaar.net\r\n"
  13. # string << "From: bob@test.lindsaar.net\r\n"
  14. # string << "Subject: This is an email\r\n"
  15. # string << "\r\n"
  16. # string << "This is the body"
  17. # Mail.new(string)
  18. #
  19. # Or creating via a block:
  20. #
  21. # message = Mail.new do
  22. # to 'mikel@test.lindsaar.net'
  23. # from 'bob@test.lindsaar.net'
  24. # subject 'This is an email'
  25. # body 'This is the body'
  26. # end
  27. #
  28. # Or creating via a hash (or hash like object):
  29. #
  30. # message = Mail.new({:to => 'mikel@test.lindsaar.net',
  31. # 'from' => 'bob@test.lindsaar.net',
  32. # :subject => 'This is an email',
  33. # :body => 'This is the body' })
  34. #
  35. # Note, the hash keys can be strings or symbols, the passed in object
  36. # does not need to be a hash, it just needs to respond to :each_pair
  37. # and yield each key value pair.
  38. #
  39. # As a side note, you can also create a new email through creating
  40. # a Mail::Message object directly and then passing in values via string,
  41. # symbol or direct method calls. See Mail::Message for more information.
  42. #
  43. # mail = Mail.new
  44. # mail.to = 'mikel@test.lindsaar.net'
  45. # mail[:from] = 'bob@test.lindsaar.net'
  46. # mail['subject'] = 'This is an email'
  47. # mail.body = 'This is the body'
  48. def self.new(*args, &block)
  49. Message.new(args, &block)
  50. end
  51. # Sets the default delivery method and retriever method for all new Mail objects.
  52. # The delivery_method and retriever_method default to :smtp and :pop3, with defaults
  53. # set.
  54. #
  55. # So sending a new email, if you have an SMTP server running on localhost is
  56. # as easy as:
  57. #
  58. # Mail.deliver do
  59. # to 'mikel@test.lindsaar.net'
  60. # from 'bob@test.lindsaar.net'
  61. # subject 'hi there!'
  62. # body 'this is a body'
  63. # end
  64. #
  65. # If you do not specify anything, you will get the following equivalent code set in
  66. # every new mail object:
  67. #
  68. # Mail.defaults do
  69. # delivery_method :smtp, { :address => "localhost",
  70. # :port => 25,
  71. # :domain => 'localhost.localdomain',
  72. # :user_name => nil,
  73. # :password => nil,
  74. # :authentication => nil,
  75. # :enable_starttls_auto => true }
  76. #
  77. # retriever_method :pop3, { :address => "localhost",
  78. # :port => 995,
  79. # :user_name => nil,
  80. # :password => nil,
  81. # :enable_ssl => true }
  82. # end
  83. #
  84. # Mail.delivery_method.new #=> Mail::SMTP instance
  85. # Mail.retriever_method.new #=> Mail::POP3 instance
  86. #
  87. # Each mail object inherits the default set in Mail.delivery_method, however, on
  88. # a per email basis, you can override the method:
  89. #
  90. # mail.delivery_method :sendmail
  91. #
  92. # Or you can override the method and pass in settings:
  93. #
  94. # mail.delivery_method :sendmail, { :address => 'some.host' }
  95. #
  96. # You can also just modify the settings:
  97. #
  98. # mail.delivery_settings = { :address => 'some.host' }
  99. #
  100. # The passed in hash is just merged against the defaults with +merge!+ and the result
  101. # assigned the mail object. So the above example will change only the :address value
  102. # of the global smtp_settings to be 'some.host', keeping all other values
  103. def self.defaults(&block)
  104. Configuration.instance.instance_eval(&block)
  105. end
  106. # Returns the delivery method selected, defaults to an instance of Mail::SMTP
  107. def self.delivery_method
  108. Configuration.instance.delivery_method
  109. end
  110. # Returns the retriever method selected, defaults to an instance of Mail::POP3
  111. def self.retriever_method
  112. Configuration.instance.retriever_method
  113. end
  114. # Send an email using the default configuration. You do need to set a default
  115. # configuration first before you use self.deliver, if you don't, an appropriate
  116. # error will be raised telling you to.
  117. #
  118. # If you do not specify a delivery type, SMTP will be used.
  119. #
  120. # Mail.deliver do
  121. # to 'mikel@test.lindsaar.net'
  122. # from 'ada@test.lindsaar.net'
  123. # subject 'This is a test email'
  124. # body 'Not much to say here'
  125. # end
  126. #
  127. # You can also do:
  128. #
  129. # mail = Mail.read('email.eml')
  130. # mail.deliver!
  131. #
  132. # And your email object will be created and sent.
  133. def self.deliver(*args, &block)
  134. mail = self.new(args, &block)
  135. mail.deliver
  136. mail
  137. end
  138. # Find emails from the default retriever
  139. # See Mail::Retriever for a complete documentation.
  140. def self.find(*args, &block)
  141. retriever_method.find(*args, &block)
  142. end
  143. # Finds and then deletes retrieved emails from the default retriever
  144. # See Mail::Retriever for a complete documentation.
  145. def self.find_and_delete(*args, &block)
  146. retriever_method.find_and_delete(*args, &block)
  147. end
  148. # Receive the first email(s) from the default retriever
  149. # See Mail::Retriever for a complete documentation.
  150. def self.first(*args, &block)
  151. retriever_method.first(*args, &block)
  152. end
  153. # Receive the first email(s) from the default retriever
  154. # See Mail::Retriever for a complete documentation.
  155. def self.last(*args, &block)
  156. retriever_method.last(*args, &block)
  157. end
  158. # Receive all emails from the default retriever
  159. # See Mail::Retriever for a complete documentation.
  160. def self.all(*args, &block)
  161. retriever_method.all(*args, &block)
  162. end
  163. # Reads in an email message from a path and instantiates it as a new Mail::Message
  164. def self.read(filename)
  165. self.new(File.open(filename, 'rb') { |f| f.read })
  166. end
  167. # Delete all emails from the default retriever
  168. # See Mail::Retriever for a complete documentation.
  169. def self.delete_all(*args, &block)
  170. retriever_method.delete_all(*args, &block)
  171. end
  172. # Instantiates a new Mail::Message using a string
  173. def Mail.read_from_string(mail_as_string)
  174. Mail.new(mail_as_string)
  175. end
  176. def Mail.connection(&block)
  177. retriever_method.connection(&block)
  178. end
  179. # Initialize the observers and interceptors arrays
  180. @@delivery_notification_observers = []
  181. @@delivery_interceptors = []
  182. # You can register an object to be informed of every email that is sent through
  183. # this method.
  184. #
  185. # Your object needs to respond to a single method #delivered_email(mail)
  186. # which receives the email that is sent.
  187. def self.register_observer(observer)
  188. unless @@delivery_notification_observers.include?(observer)
  189. @@delivery_notification_observers << observer
  190. end
  191. end
  192. # You can register an object to be given every mail object that will be sent,
  193. # before it is sent. So if you want to add special headers or modify any
  194. # email that gets sent through the Mail library, you can do so.
  195. #
  196. # Your object needs to respond to a single method #delivering_email(mail)
  197. # which receives the email that is about to be sent. Make your modifications
  198. # directly to this object.
  199. def self.register_interceptor(interceptor)
  200. unless @@delivery_interceptors.include?(interceptor)
  201. @@delivery_interceptors << interceptor
  202. end
  203. end
  204. def self.inform_observers(mail)
  205. @@delivery_notification_observers.each do |observer|
  206. observer.delivered_email(mail)
  207. end
  208. end
  209. def self.inform_interceptors(mail)
  210. @@delivery_interceptors.each do |interceptor|
  211. interceptor.delivering_email(mail)
  212. end
  213. end
  214. protected
  215. def self.random_tag
  216. t = Time.now
  217. sprintf('%x%x_%x%x%d%x',
  218. t.to_i, t.tv_usec,
  219. $$, Thread.current.object_id.abs, self.uniq, rand(255))
  220. end
  221. private
  222. def self.something_random
  223. (Thread.current.object_id * rand(255) / Time.now.to_f).to_s.slice(-3..-1).to_i
  224. end
  225. def self.uniq
  226. @@uniq += 1
  227. end
  228. @@uniq = self.something_random
  229. end