email_build.rb 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. module Channel::EmailBuild
  3. =begin
  4. generate email
  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. generate email with S/MIME
  12. mail = Channel::EmailBuild.build(
  13. from: 'sender@example.com',
  14. to: 'recipient@example.com',
  15. body: 'somebody with some text',
  16. content_type: 'text/plain',
  17. security: {
  18. type: 'S/MIME',
  19. encryption: {
  20. success: true,
  21. },
  22. sign: {
  23. success: true,
  24. },
  25. }
  26. )
  27. =end
  28. def self.build(attr, notification = false)
  29. mail = Mail.new
  30. # set headers
  31. attr.each do |key, value|
  32. next if key.to_s == 'attachments'
  33. next if key.to_s == 'body'
  34. next if key.to_s == 'content_type'
  35. next if key.to_s == 'security'
  36. mail[key.to_s] = if value.present? && value.class != Array
  37. value.to_s
  38. else
  39. value
  40. end
  41. end
  42. # add html part
  43. if attr[:content_type] && attr[:content_type] == 'text/html'
  44. html_alternative = Mail::Part.new do
  45. content_type 'text/html; charset=UTF-8'
  46. # complete check
  47. html_document = Channel::EmailBuild.html_complete_check(attr[:body])
  48. body html_document
  49. end
  50. # generate plain part
  51. attr[:body] = attr[:body].html2text
  52. end
  53. # add plain text part
  54. text_alternative = Mail::Part.new do
  55. content_type 'text/plain; charset=UTF-8'
  56. body attr[:body]
  57. end
  58. # build email without any attachments
  59. if !html_alternative && attr[:attachments].blank?
  60. mail.content_type 'text/plain; charset=UTF-8'
  61. mail.body attr[:body]
  62. SecureMailing.outgoing(mail, attr[:security])
  63. return mail
  64. end
  65. # build email with attachments
  66. alternative_bodies = Mail::Part.new { content_type 'multipart/alternative' }
  67. alternative_bodies.add_part text_alternative
  68. if html_alternative
  69. html_container = Mail::Part.new { content_type 'multipart/related' }
  70. html_container.add_part html_alternative
  71. # place to add inline attachments related to html alternative
  72. attr[:attachments]&.each do |attachment|
  73. next if attachment.class == Hash
  74. next if attachment.preferences['Content-ID'].blank?
  75. attachment = Mail::Part.new do
  76. content_type attachment.preferences['Content-Type']
  77. content_id "<#{attachment.preferences['Content-ID']}>"
  78. content_disposition attachment.preferences['Content-Disposition'] || 'inline'
  79. content_transfer_encoding 'binary'
  80. body attachment.content.force_encoding('BINARY')
  81. end
  82. html_container.add_part attachment
  83. end
  84. alternative_bodies.add_part html_container
  85. end
  86. mail.add_part alternative_bodies
  87. # add attachments
  88. attr[:attachments]&.each do |attachment|
  89. if attachment.class == Hash
  90. attachment['content-id'] = nil
  91. mail.attachments[attachment[:filename]] = attachment
  92. else
  93. next if attachment.preferences['Content-ID'].present?
  94. filename = attachment.filename
  95. encoded_filename = Mail::Encodings.decode_encode filename, :encode
  96. disposition = attachment.preferences['Content-Disposition'] || 'attachment'
  97. content_type = attachment.preferences['Content-Type'] || attachment.preferences['Mime-Type'] || 'application/octet-stream'
  98. mail.attachments[attachment.filename] = {
  99. content_disposition: "#{disposition}; filename=\"#{encoded_filename}\"",
  100. content_type: "#{content_type}; filename=\"#{encoded_filename}\"",
  101. content: attachment.content
  102. }
  103. end
  104. end
  105. SecureMailing.outgoing(mail, attr[:security])
  106. # set organization
  107. organization = Setting.get('organization')
  108. if organization.present?
  109. mail['Organization'] = organization.to_s
  110. end
  111. if notification
  112. mail['X-Loop'] = 'yes'
  113. mail['Precedence'] = 'bulk'
  114. mail['Auto-Submitted'] = 'auto-generated'
  115. mail['X-Auto-Response-Suppress'] = 'All'
  116. end
  117. mail['X-Powered-By'] = 'Zammad - Helpdesk/Support (https://zammad.org/)'
  118. mail['X-Mailer'] = 'Zammad Mail Service'
  119. mail
  120. end
  121. =begin
  122. quoted_in_one_line = Channel::EmailBuild.recipient_line('Somebody @ "Company"', 'some.body@example.com')
  123. returns
  124. '"Somebody @ \"Company\"" <some.body@example.com>'
  125. =end
  126. def self.recipient_line(realname, email)
  127. return "#{realname} <#{email}>" if realname.match?(/^[A-z]+$/i)
  128. "\"#{realname.gsub('"', '\"')}\" <#{email}>"
  129. end
  130. =begin
  131. Check if string is a complete html document. If not, add head and css styles.
  132. full_html_document_string = Channel::EmailBuild.html_complete_check(html_string)
  133. =end
  134. def self.html_complete_check(html)
  135. # apply mail client fixes
  136. html = Channel::EmailBuild.html_mail_client_fixes(html)
  137. return html if html.match?(/<html>/i)
  138. html_email_body = File.read(Rails.root.join('app/views/mailer/application_wrapper.html.erb').to_s)
  139. html_email_body.gsub!('###html_email_css_font###', Setting.get('html_email_css_font'))
  140. # use block form because variable html could contain backslashes and e. g. '\1' that
  141. # must not be handled as back-references for regular expressions
  142. html_email_body.sub('###html###') { html }
  143. end
  144. =begin
  145. Add/change markup to display html in any mail client nice.
  146. html_string_with_fixes = Channel::EmailBuild.html_mail_client_fixes(html_string)
  147. =end
  148. def self.html_mail_client_fixes(html)
  149. # https://github.com/martini/zammad/issues/165
  150. new_html = html.gsub('<blockquote type="cite">', '<blockquote type="cite" style="border-left: 2px solid blue; margin: 0 0 16px; padding: 8px 12px 8px 12px;">')
  151. new_html.gsub!(/<p>/mxi, '<p style="margin: 0;">')
  152. new_html.gsub!(%r{</?hr>}mxi, '<hr style="margin-top: 6px; margin-bottom: 6px; border: 0; border-top: 1px solid #dfdfdf;">')
  153. new_html
  154. end
  155. end