2014-02-03 19:23:00 +00:00
|
|
|
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
|
2013-06-12 15:59:58 +00:00
|
|
|
|
2012-04-13 16:42:25 +00:00
|
|
|
require 'mail'
|
|
|
|
|
2014-12-27 15:25:58 +00:00
|
|
|
module Channel::EmailBuild
|
2012-04-13 16:42:25 +00:00
|
|
|
|
2014-12-27 14:07:49 +00:00
|
|
|
=begin
|
|
|
|
|
2014-12-27 15:25:58 +00:00
|
|
|
mail = Channel::EmailBuild.build(
|
|
|
|
:from => 'sender@example.com',
|
|
|
|
:to => 'recipient@example.com',
|
|
|
|
:body => 'somebody with some text',
|
|
|
|
:content_type => 'text/plain',
|
2014-12-27 14:07:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2012-04-13 16:42:25 +00:00
|
|
|
def build(attr, notification = false)
|
|
|
|
mail = Mail.new
|
|
|
|
|
|
|
|
# set organization
|
|
|
|
organization = Setting.get('organization')
|
|
|
|
if organization then;
|
2012-07-28 16:36:47 +00:00
|
|
|
mail['Organization'] = organization.to_s
|
2012-04-13 16:42:25 +00:00
|
|
|
end
|
2012-08-06 06:29:54 +00:00
|
|
|
|
2012-04-13 16:42:25 +00:00
|
|
|
# notification
|
|
|
|
if notification
|
|
|
|
attr['X-Loop'] = 'yes'
|
|
|
|
attr['Precedence'] = 'bulk'
|
|
|
|
attr['Auto-Submitted'] = 'auto-generated'
|
|
|
|
end
|
2012-08-06 06:29:54 +00:00
|
|
|
|
2014-12-27 14:07:49 +00:00
|
|
|
#attr['X-Powered-BY'] = 'Zammad - Support/Helpdesk (http://www.zammad.org/)'
|
|
|
|
attr['X-Mailer'] = 'Zammad Mail Service (1.x)'
|
2012-08-06 06:29:54 +00:00
|
|
|
|
2012-04-13 16:42:25 +00:00
|
|
|
# set headers
|
2014-12-27 14:07:49 +00:00
|
|
|
attr.each do |key, value|
|
|
|
|
next if key.to_s == 'attachments'
|
|
|
|
next if key.to_s == 'body'
|
|
|
|
next if key.to_s == 'content_type'
|
|
|
|
mail[key.to_s] = value.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
# add html part
|
|
|
|
if attr[:content_type] && attr[:content_type] == 'text/html'
|
|
|
|
mail.html_part = Mail::Part.new do
|
|
|
|
content_type 'text/html; charset=UTF-8'
|
2014-12-29 15:39:59 +00:00
|
|
|
|
|
|
|
# complete check
|
|
|
|
attr[:body] = html_complete_check( attr[:body] )
|
|
|
|
|
2014-12-27 14:07:49 +00:00
|
|
|
body attr[:body]
|
2012-04-13 16:42:25 +00:00
|
|
|
end
|
2014-12-27 14:07:49 +00:00
|
|
|
|
|
|
|
# generate plain part
|
2014-12-27 15:25:58 +00:00
|
|
|
attr[:body] = attr[:body].html2text
|
2012-04-13 16:42:25 +00:00
|
|
|
end
|
|
|
|
|
2014-12-27 14:07:49 +00:00
|
|
|
# add plain text part
|
2012-04-13 16:42:25 +00:00
|
|
|
mail.text_part = Mail::Part.new do
|
2014-12-27 14:07:49 +00:00
|
|
|
content_type 'text/plain; charset=UTF-8'
|
2012-04-13 16:42:25 +00:00
|
|
|
body attr[:body]
|
|
|
|
end
|
|
|
|
|
|
|
|
# add attachments
|
|
|
|
if attr[:attachments]
|
|
|
|
attr[:attachments].each do |attachment|
|
|
|
|
mail.attachments[attachment.filename] = {
|
|
|
|
:content_type => attachment.preferences['Content-Type'],
|
|
|
|
:mime_type => attachment.preferences['Mime-Type'],
|
2014-04-28 07:44:36 +00:00
|
|
|
:content => attachment.content
|
2012-04-13 16:42:25 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
2014-10-22 21:00:11 +00:00
|
|
|
mail
|
2012-04-13 16:42:25 +00:00
|
|
|
end
|
2014-12-29 15:39:59 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
full_html_document_string = Channel::EmailBuild.html_complete_check( html_string )
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.html_complete_check(html)
|
|
|
|
return html if html =~ /<html>/i
|
|
|
|
|
|
|
|
css = 'font-family:Geneva,Helvetica,Arial,sans-serif; font-size: 12px;'
|
|
|
|
|
|
|
|
html = <<HERE
|
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>'
|
|
|
|
<head>
|
|
|
|
<body style="#{css}">
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
HERE
|
|
|
|
|
|
|
|
html
|
|
|
|
end
|
2014-10-22 21:00:11 +00:00
|
|
|
end
|