email_build.rb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
  2. require 'mail'
  3. module Channel::EmailBuild
  4. =begin
  5. mail = Channel::EmailBuild.build(
  6. :from => 'sender@example.com',
  7. :to => 'recipient@example.com',
  8. :body => 'somebody with some text',
  9. :content_type => 'text/plain',
  10. )
  11. =end
  12. def self.build(attr, notification = false)
  13. mail = Mail.new
  14. # set organization
  15. organization = Setting.get('organization')
  16. if organization
  17. mail['Organization'] = organization.to_s
  18. end
  19. # notification
  20. if notification
  21. attr['X-Loop'] = 'yes'
  22. attr['Precedence'] = 'bulk'
  23. attr['Auto-Submitted'] = 'auto-generated'
  24. end
  25. #attr['X-Powered-BY'] = 'Zammad - Support/Helpdesk (http://www.zammad.org/)'
  26. attr['X-Mailer'] = 'Zammad Mail Service (1.x)'
  27. # set headers
  28. attr.each do |key, value|
  29. next if key.to_s == 'attachments'
  30. next if key.to_s == 'body'
  31. next if key.to_s == 'content_type'
  32. mail[key.to_s] = value.to_s
  33. end
  34. # add html part
  35. if attr[:content_type] && attr[:content_type] == 'text/html'
  36. html_alternative = Mail::Part.new do
  37. content_type 'text/html; charset=UTF-8'
  38. # complete check
  39. html_document = Channel::EmailBuild.html_complete_check( attr[:body] )
  40. body html_document
  41. end
  42. # generate plain part
  43. attr[:body] = attr[:body].html2text
  44. end
  45. # add plain text part
  46. text_alternative = Mail::Part.new do
  47. content_type 'text/plain; charset=UTF-8'
  48. body attr[:body]
  49. end
  50. # build email without any attachments
  51. if !html_alternative && ( !attr[:attachments] || attr[:attachments].empty? )
  52. mail.content_type 'text/plain; charset=UTF-8'
  53. mail.body attr[:body]
  54. return mail
  55. end
  56. # build email with attachments
  57. alternative_bodies = Mail::Part.new { content_type 'multipart/alternative' }
  58. alternative_bodies.add_part text_alternative
  59. if html_alternative
  60. html_container = Mail::Part.new { content_type 'multipart/related' }
  61. html_container.add_part html_alternative
  62. alternative_bodies.add_part html_container
  63. # place to add inline attachments related to html alternative
  64. end
  65. mail.add_part alternative_bodies
  66. # add attachments
  67. if attr[:attachments]
  68. attr[:attachments].each do |attachment|
  69. if attachment.class == Hash
  70. attachment['content-id'] = nil
  71. mail.attachments[ attachment[:filename] ] = attachment
  72. else
  73. mail.attachments[attachment.filename] = {
  74. :content_type => attachment.preferences['Content-Type'],
  75. :mime_type => attachment.preferences['Mime-Type'],
  76. :content => attachment.content,
  77. 'content-id' => nil,
  78. }
  79. end
  80. end
  81. end
  82. mail
  83. end
  84. =begin
  85. full_html_document_string = Channel::EmailBuild.html_complete_check( html_string )
  86. =end
  87. def self.html_complete_check(html)
  88. return html if html =~ /<html>/i
  89. css = "font-family:'Helvetica Neue', Helvetica, Arial, Geneva, sans-serif; font-size: 12px;"
  90. html = <<HERE
  91. <!DOCTYPE html>
  92. <html>
  93. <head>
  94. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
  95. <style type="text/css">
  96. body {
  97. width:90% !important;
  98. -webkit-text-size-adjust:90%;
  99. -ms-text-size-adjust:90%;
  100. #{css};
  101. }
  102. img {
  103. outline:none; text-decoration:none; -ms-interpolation-mode: bicubic;
  104. }
  105. a img {
  106. border:none;
  107. }
  108. table td {
  109. border-collapse: collapse;
  110. }
  111. table {
  112. border-collapse: collapse; mso-table-lspace:0pt; mso-table-rspace:0pt;
  113. }
  114. p, table, div, td {
  115. max-width: 600px;
  116. }
  117. p {
  118. margin: 0;
  119. }
  120. blockquote, pre {
  121. margin: 0px;
  122. padding: 8px 12px 8px 12px;
  123. }
  124. </style>
  125. <head>
  126. <body style="#{css}">#{html}</body>
  127. </html>
  128. HERE
  129. html
  130. end
  131. end