Guest User

Untitled

a guest
Feb 19th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. module Mack
  2. module Notifier
  3. module Adapters # :nodoc:
  4. # Converts a Mack::Notifier object into a TMail object.
  5. class Tmail < Mack::Notifier::Adapters::Base
  6.  
  7. # Returns the underlying TMail object.
  8. # Raises Mack::Errors::UnconvertedNotifier if the convert method hasn't
  9. # been called first.
  10. def transformed
  11. raise Mack::Errors::UnconvertedNotifier.new if @tmail.nil?
  12. @tmail
  13. end
  14.  
  15. # Returns the ready to be delivered encoded String
  16. def deliverable
  17. transformed.encoded
  18. end
  19.  
  20. # Converts the Mack::Notifier object to a TMail object.
  21. def convert
  22. @tmail = TMail::Mail.new
  23. @tmail.to = mack_notifier.to
  24. @tmail.cc = mack_notifier.cc
  25. @tmail.bcc = mack_notifier.bcc
  26. @tmail.reply_to = mack_notifier.reply_to
  27. @tmail.from = mack_notifier.from
  28. @tmail.subject = mack_notifier.subject
  29. @tmail.date = mack_notifier.date_sent
  30. @tmail.mime_version = mack_notifier.mime_version
  31.  
  32. # set text and html bodies
  33. main_body = TMail::Mail.new
  34. unless mack_notifier.body(:plain).blank?
  35. text = TMail::Mail.new
  36. text.content_type = "text/plain"
  37. text.body = mack_notifier.body(:plain)
  38. main_body.parts << text
  39. end
  40. unless mack_notifier.body(:html).blank?
  41. html = TMail::Mail.new
  42. html.content_type = "text/html"
  43. html.body = mack_notifier.body(:html)
  44. main_body.parts << html
  45. end
  46.  
  47. unless main_body.parts.empty?
  48. main_body.content_type = "multipart/alternative"
  49. if mack_notifier.attachments.any? # there's an attachment
  50. @tmail.parts << main_body
  51. else
  52. if main_body.parts.size == 1
  53. @tmail.body = main_body.parts.first.body
  54. else
  55. @tmail.parts << main_body
  56. end
  57. end
  58. end
  59.  
  60. # set attachments, if any.
  61. mack_notifier.attachments.each do |at|
  62. attachment = TMail::Mail.new
  63. attachment.body = Base64.encode64(at.body)
  64. attachment.transfer_encoding = "Base64"
  65. attachment.content_type = "application/octet-stream"
  66. attachment['Content-Disposition'] = "attachment; filename=#{at.file_name}"
  67. @tmail.parts << attachment
  68. end
  69.  
  70. @tmail.content_type = mack_notifier.content_type
  71. end
  72.  
  73. end # Tmail
  74. end # Adapters
  75. end # Notifier
  76. end # Mack
Add Comment
Please, Sign In to add comment