email_build.rb 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. # Copyright (C) 2012-2016 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. attr['X-Auto-Response-Suppress'] = 'All'
  25. end
  26. attr['X-Powered-By'] = 'Zammad - Helpdesk/Support (https://zammad.org/)'
  27. attr['X-Mailer'] = 'Zammad Mail Service'
  28. # set headers
  29. attr.each do |key, value|
  30. next if key.to_s == 'attachments'
  31. next if key.to_s == 'body'
  32. next if key.to_s == 'content_type'
  33. mail[key.to_s] = if value && value.class != Array
  34. value.to_s
  35. else
  36. value
  37. end
  38. end
  39. # add html part
  40. if attr[:content_type] && attr[:content_type] == 'text/html'
  41. html_alternative = Mail::Part.new do
  42. content_type 'text/html; charset=UTF-8'
  43. # complete check
  44. html_document = Channel::EmailBuild.html_complete_check(attr[:body])
  45. body html_document
  46. end
  47. # generate plain part
  48. attr[:body] = attr[:body].html2text
  49. end
  50. # add plain text part
  51. text_alternative = Mail::Part.new do
  52. content_type 'text/plain; charset=UTF-8'
  53. body attr[:body]
  54. end
  55. # build email without any attachments
  56. if !html_alternative && attr[:attachments].blank?
  57. mail.content_type 'text/plain; charset=UTF-8'
  58. mail.body attr[:body]
  59. return mail
  60. end
  61. # build email with attachments
  62. alternative_bodies = Mail::Part.new { content_type 'multipart/alternative' }
  63. alternative_bodies.add_part text_alternative
  64. if html_alternative
  65. html_container = Mail::Part.new { content_type 'multipart/related' }
  66. html_container.add_part html_alternative
  67. # place to add inline attachments related to html alternative
  68. attr[:attachments]&.each do |attachment|
  69. next if attachment.class == Hash
  70. next if attachment.preferences['Content-ID'].blank?
  71. attachment = Mail::Part.new do
  72. content_type attachment.preferences['Content-Type']
  73. content_id "<#{attachment.preferences['Content-ID']}>"
  74. content_disposition attachment.preferences['Content-Disposition'] || 'inline'
  75. content_transfer_encoding 'binary'
  76. body attachment.content.force_encoding('BINARY')
  77. end
  78. html_container.add_part attachment
  79. end
  80. alternative_bodies.add_part html_container
  81. end
  82. mail.add_part alternative_bodies
  83. # add attachments
  84. attr[:attachments]&.each do |attachment|
  85. if attachment.class == Hash
  86. attachment['content-id'] = nil
  87. mail.attachments[ attachment[:filename] ] = attachment
  88. else
  89. next if attachment.preferences['Content-ID'].present?
  90. filename = attachment.filename
  91. encoded_filename = Mail::Encodings.decode_encode filename, :encode
  92. disposition = attachment.preferences['Content-Disposition'] || 'attachment'
  93. content_type = attachment.preferences['Content-Type'] || 'application/octet-stream'
  94. mail.attachments[attachment.filename] = {
  95. content_disposition: "#{disposition}; filename=\"#{encoded_filename}\"",
  96. content_type: "#{content_type}; filename=\"#{encoded_filename}\"",
  97. content: attachment.content
  98. }
  99. end
  100. end
  101. mail
  102. end
  103. =begin
  104. quoted_in_one_line = Channel::EmailBuild.recipient_line('Somebody @ "Company"', 'some.body@example.com')
  105. returns
  106. '"Somebody @ \"Company\"" <some.body@example.com>'
  107. =end
  108. def self.recipient_line(realname, email)
  109. return "#{realname} <#{email}>" if realname.match?(/^[A-z]+$/i)
  110. "\"#{realname.gsub('"', '\"')}\" <#{email}>"
  111. end
  112. =begin
  113. Check if string is a complete html document. If not, add head and css styles.
  114. full_html_document_string = Channel::EmailBuild.html_complete_check(html_string)
  115. =end
  116. def self.html_complete_check(html)
  117. # apply mail client fixes
  118. html = Channel::EmailBuild.html_mail_client_fixes(html)
  119. return html if html.match?(/<html>/i)
  120. # use block form because variable html could contain backslashes and e. g. '\1' that
  121. # must not be handled as back-references for regular expressions
  122. Rails.configuration.html_email_body.sub('###html###') { html }
  123. end
  124. =begin
  125. Add/change markup to display html in any mail client nice.
  126. html_string_with_fixes = Channel::EmailBuild.html_mail_client_fixes(html_string)
  127. =end
  128. def self.html_mail_client_fixes(html)
  129. # https://github.com/martini/zammad/issues/165
  130. 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;">')
  131. new_html.gsub!(/<p>/mxi, '<p style="margin: 0;">')
  132. new_html.gsub!(%r{</?hr>}mxi, '<hr style="margin-top: 6px; margin-bottom: 6px; border: 0; border-top: 1px solid #dfdfdf;">')
  133. new_html
  134. end
  135. end