123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050 |
- class Channel::EmailParser
- include Channel::EmailHelper
- PROCESS_TIME_MAX = 180
- EMAIL_REGEX = %r{.+@.+}
- RECIPIENT_FIELDS = %w[to cc delivered-to x-original-to envelope-to].freeze
- SENDER_FIELDS = %w[from reply-to return-path sender].freeze
- EXCESSIVE_LINKS_MSG = __('This message cannot be displayed because it contains over 5,000 links. Download the raw message below and open it via an Email client if you still wish to view it.').freeze
- MESSAGE_STRUCT = Struct.new(:from_display_name, :subject, :msg_size).freeze
- def parse(msg, allow_missing_attribute_exceptions: true)
- msg = msg.force_encoding('binary')
-
-
- msg = Mail::Utilities.binary_unsafe_to_crlf(msg)
- mail = Mail.new(msg)
- message_ensure_message_id(msg, mail)
- force_parts_encoding_if_needed(mail)
- headers = message_header_hash(mail)
- body = message_body_hash(mail)
- sender_attributes = self.class.sender_attributes(headers)
- if allow_missing_attribute_exceptions && sender_attributes.blank?
- msg = __('Could not parse any sender attribute from the email. Checked fields:')
- msg += ' '
- msg += SENDER_FIELDS.map { |f| f.split('-').map(&:capitalize).join('-') }.join(', ')
- raise Exceptions::MissingAttribute.new('email', msg)
- end
- message_attributes = [
- { mail_instance: mail },
- headers,
- body,
- sender_attributes,
- { raw: msg },
- ]
- message_attributes.reduce({}.with_indifferent_access, &:merge)
- end
- def process(channel, msg, exception = true)
- process_with_timeout(channel, msg)
- rescue => e
- failed_email = ::FailedEmail.create!(data: msg, parsing_error: e)
- message = <<~MESSAGE.chomp
- Can't process email. Run the following command to get the message for issue report at https://github.com/zammad/zammad/issues:
- zammad run rails r "puts FailedEmail.find(#{failed_email.id}).data"
- MESSAGE
- puts "ERROR: #{message}"
- puts "ERROR: #{e.inspect}"
- Rails.logger.error message
- Rails.logger.error e
- return false if exception == false
- raise failed_email.parsing_error
- end
- def process_with_timeout(channel, msg)
- Timeout.timeout(PROCESS_TIME_MAX) do
- _process(channel, msg)
- end
- end
- def _process(channel, msg)
-
- mail = parse(msg)
- Rails.logger.info "Process email with msgid '#{mail[:message_id]}'"
-
- UserInfo.current_user_id = 1
-
- original_interface_handle = ApplicationHandleInfo.current
- transaction_params = { interface_handle: "#{original_interface_handle}.postmaster", disable: [] }
- filters = {}
- Setting.where(area: 'Postmaster::PreFilter').reorder(:name).each do |setting|
- filters[setting.name] = Setting.get(setting.name).constantize
- end
- filters.each do |key, backend|
- Rails.logger.debug { "run postmaster pre filter #{key}: #{backend}" }
- begin
- backend.run(channel, mail, transaction_params)
- rescue => e
- Rails.logger.error "can't run postmaster pre filter #{key}: #{backend}"
- Rails.logger.error e.inspect
- raise e
- end
- end
-
- if mail[:'x-zammad-ignore'] == 'true' || mail[:'x-zammad-ignore'] == true
- Rails.logger.info "ignored email with msgid '#{mail[:message_id]}' from '#{mail[:from]}' because of x-zammad-ignore header"
- return [{}, nil, nil, mail]
- end
- ticket = nil
- article = nil
- session_user = nil
-
- mail = prepare_idn_inbound(mail)
-
- Transaction.execute(transaction_params) do
-
- session_user_id = mail[:'x-zammad-session-user-id']
- if !session_user_id
- raise __('No x-zammad-session-user-id, no sender set!')
- end
- session_user = User.lookup(id: session_user_id)
- if !session_user
- raise "No user found for x-zammad-session-user-id: #{session_user_id}!"
- end
-
- UserInfo.current_user_id = session_user.id
-
- if mail[:'x-zammad-ticket-id']
- ticket = Ticket.find_by(id: mail[:'x-zammad-ticket-id'])
- end
- if mail[:'x-zammad-ticket-number']
- ticket = Ticket.find_by(number: mail[:'x-zammad-ticket-number'])
- end
-
- if ticket
- set_attributes_by_x_headers(ticket, 'ticket', mail, 'followup')
-
- ticket.save! if ticket.has_changes_to_save?
-
- if !mail[:'x-zammad-ticket-followup-state'] && !mail[:'x-zammad-ticket-followup-state_id']
- new_state = Ticket::State.find_by(default_create: true)
- if ticket.state_id != new_state.id && !mail[:'x-zammad-out-of-office']
- ticket.state = Ticket::State.find_by(default_follow_up: true)
- ticket.save!
- end
- end
-
- if mail[:'x-zammad-ticket-followup-tags'].present?
- mail[:'x-zammad-ticket-followup-tags'].each do |tag|
- ticket.tag_add(tag, sourceable: mail[:'x-zammad-ticket-followup-tags-source'])
- end
- end
- end
-
- if !ticket
- preferences = {}
- if channel[:id]
- preferences = {
- channel_id: channel[:id]
- }
- end
-
- group = nil
- if channel[:group_id]
- group = Group.lookup(id: channel[:group_id])
- else
- mail_to_group = self.class.mail_to_group(mail[:to])
- if mail_to_group.present?
- group = mail_to_group
- end
- end
- if group.blank? || group.active == false
- group = Group.where(active: true).reorder(id: :asc).first
- end
- if group.blank?
- group = Group.first
- end
- title = mail[:subject]
- if title.blank?
- title = '-'
- end
- ticket = Ticket.new(
- group_id: group.id,
- title: title,
- preferences: preferences,
- )
- set_attributes_by_x_headers(ticket, 'ticket', mail)
-
- ticket.save!
-
- if mail[:'x-zammad-ticket-tags'].present?
- mail[:'x-zammad-ticket-tags'].each do |tag|
- ticket.tag_add(tag, sourceable: mail[:'x-zammad-ticket-tags-source'])
- end
- end
- end
-
- article = Ticket::Article.new(
- ticket_id: ticket.id,
- type_id: Ticket::Article::Type.find_by(name: 'email').id,
- sender_id: Ticket::Article::Sender.find_by(name: 'Customer').id,
- content_type: mail[:content_type],
- body: mail[:body],
- from: mail[:from],
- reply_to: mail[:'reply-to'],
- to: mail[:to],
- cc: mail[:cc],
- subject: mail[:subject],
- message_id: mail[:message_id],
- internal: false,
- )
-
- set_attributes_by_x_headers(article, 'article', mail)
-
- article.preferences.merge!(mail[:sanitized_body_info])
-
- article.save!
-
- article.save_as_raw(msg)
-
- mail[:attachments]&.each do |attachment|
- filename = attachment[:filename].force_encoding('utf-8')
- if !filename.force_encoding('UTF-8').valid_encoding?
- filename = filename.utf8_encode(fallback: :read_as_sanitized_binary)
- end
- Store.create!(
- object: 'Ticket::Article',
- o_id: article.id,
- data: attachment[:data],
- filename: filename,
- preferences: attachment[:preferences]
- )
- end
- end
- ticket.reload
- article.reload
- session_user.reload
-
- filters = {}
- Setting.where(area: 'Postmaster::PostFilter').reorder(:name).each do |setting|
- filters[setting.name] = Setting.get(setting.name).constantize
- end
- filters.each_value do |backend|
- Rails.logger.debug { "run postmaster post filter #{backend}" }
- begin
- backend.run(channel, mail, ticket, article, session_user)
- rescue => e
- Rails.logger.error "can't run postmaster post filter #{backend}"
- Rails.logger.error e.inspect
- end
- end
-
- [ticket, article, session_user, mail]
- end
- def self.mail_to_group(to)
- begin
- to = Mail::AddressList.new(to)&.addresses&.first&.address
- rescue
- Rails.logger.error 'Can not parse :to field for group destination!'
- end
- return if to.blank?
- email = EmailAddress.find_by(email: to.downcase)
- return if email&.channel.blank?
- email.channel&.group
- end
- def self.check_attributes_by_x_headers(header_name, value)
- class_name = nil
- attribute = nil
-
- return true if header_name == 'x-zammad-ticket-tags'
- if header_name =~ %r{^x-zammad-(.+?)-(followup-|)(.*)$}i
- class_name = $1
- attribute = $3
- end
- return true if !class_name
- if class_name.casecmp('article').zero?
- class_name = 'Ticket::Article'
- end
- return true if !attribute
- key_short = attribute[ attribute.length - 3, attribute.length ]
- return true if key_short != '_id'
- class_object = class_name.to_classname.constantize
- return if !class_object
- class_instance = class_object.new
- return false if !class_instance.association_id_validation(attribute, value)
- true
- end
- def self.sender_attributes(from)
- if from.is_a?(ActiveSupport::HashWithIndifferentAccess)
- from = SENDER_FIELDS.filter_map { |f| from[f] }
- .map(&:to_utf8).compact_blank
- .partition { |address| address.match?(EMAIL_REGEX) }
- .flatten.first
- end
- data = {}.with_indifferent_access
- return data if from.blank?
- from = from.gsub('<>', '').strip
- mail_address = begin
- Mail::AddressList.new(from).addresses
- .select { |a| a.address.present? }
- .partition { |a| a.address.match?(EMAIL_REGEX) }
- .flatten.first
- rescue Mail::Field::ParseError => e
- $stdout.puts e
- end
- if mail_address&.address.present?
- data[:from_email] = mail_address.address
- data[:from_local] = mail_address.local
- data[:from_domain] = mail_address.domain
- data[:from_display_name] = mail_address.display_name || mail_address.comments&.first
- elsif from =~ %r{^(.+?)<((.+?)@(.+?))>}
- data[:from_email] = $2
- data[:from_local] = $3
- data[:from_domain] = $4
- data[:from_display_name] = $1
- else
- data[:from_email] = from
- data[:from_local] = from
- data[:from_domain] = from
- data[:from_display_name] = from
- end
-
- data[:from_display_name] =
- Mail::Field.new('X-From', data[:from_display_name].to_utf8)
- .to_s
- .delete('"')
- .strip
- .gsub(%r{(^'|'$)}, '')
- data
- end
- def set_attributes_by_x_headers(item_object, header_name, mail, suffix = false)
-
- item_object.attributes.each_key do |key|
-
- next if key == 'updated_by_id'
- next if key == 'created_by_id'
-
- key_short = key[ key.length - 3, key.length ]
- if key_short == '_id'
- key_short = key[ 0, key.length - 3 ]
- header = "x-zammad-#{header_name}-#{key_short}"
- if suffix
- header = "x-zammad-#{header_name}-#{suffix}-#{key_short}"
- end
-
- if mail[header.to_sym]
- Rails.logger.info "set_attributes_by_x_headers header #{header} found #{mail[header.to_sym]}"
- item_object.class.reflect_on_all_associations.map do |assoc|
- next if assoc.name.to_s != key_short
- Rails.logger.info "set_attributes_by_x_headers found #{assoc.class_name} lookup for '#{mail[header.to_sym]}'"
- item = assoc.class_name.constantize
- assoc_object = nil
- if item.new.respond_to?(:name)
- assoc_object = item.lookup(name: mail[header.to_sym])
- end
- if !assoc_object && item.new.respond_to?(:login)
- assoc_object = item.lookup(login: mail[header.to_sym])
- end
- if !assoc_object && item.new.respond_to?(:email)
- assoc_object = item.lookup(email: mail[header.to_sym])
- end
- if assoc_object.blank?
-
- mail.delete(header.to_sym)
- next
- end
- Rails.logger.info "set_attributes_by_x_headers assign #{item_object.class}
- item_object[key] = assoc_object.id
- item_object.history_change_source_attribute(mail[:"#{header}-source"], key)
- end
- end
- end
-
- header = "x-zammad-#{header_name}-#{key}"
- if suffix
- header = "x-zammad-#{header_name}-#{suffix}-#{key}"
- end
- next if !mail[header.to_sym]
- Rails.logger.info "set_attributes_by_x_headers header #{header} found. Assign #{key}=#{mail[header.to_sym]}"
- item_object[key] = mail[header.to_sym]
- item_object.history_change_source_attribute(mail[:"#{header}-source"], key)
- end
- end
- def self.reprocess_failed_articles
- articles = Ticket::Article.where(body: ::HtmlSanitizer::UNPROCESSABLE_HTML_MSG)
- articles.reorder(id: :desc).as_batches do |article|
- if !article.as_raw&.content
- puts "No raw content for article id #{article.id}! Please verify manually via command: Ticket::Article.find(#{article.id}).as_raw"
- next
- end
- puts "Fix article #{article.id}..."
- ApplicationHandleInfo.use('email_parser.postmaster') do
- parsed = Channel::EmailParser.new.parse(article.as_raw.content)
- if parsed[:body] == ::HtmlSanitizer::UNPROCESSABLE_HTML_MSG
- puts "ERROR: Failed to reprocess the article, please verify the content of the article and if needed increase the timeout (see: Setting.get('html_sanitizer_processing_timeout'))."
- next
- end
- article.update!(body: parsed[:body], content_type: parsed[:content_type])
- end
- end
- puts "#{articles.count} articles are affected."
- end
- =begin
- process oversized emails by
- - Reply with a postmaster message to inform the sender
- =end
- def process_oversized_mail(channel, msg)
- postmaster_response(channel, msg)
- end
- private
-
- def force_parts_encoding_if_needed(mail)
-
- ([mail] + mail.parts).each { |elem| force_single_part_encoding_if_needed(elem) }
- end
- # https://github.com/zammad/zammad/issues/2922
- def force_single_part_encoding_if_needed(part)
- return if part.charset&.downcase != 'iso-2022-jp'
- part.body = force_japanese_encoding part.body.encoded.unpack1('M')
- end
- ISO2022JP_REGEXP = %r{=\?ISO-2022-JP\?B\?(.+?)\?=}
- # https://github.com/zammad/zammad/issues/3115
- def header_field_unpack_japanese(field)
- field.value.gsub ISO2022JP_REGEXP do
- force_japanese_encoding Base64.decode64($1)
- end
- end
- # generate Message ID on the fly if it was missing
- # yes, Mail gem generates one in some cases
- # but it is 100% random so duplicate messages would not be detected
- def message_ensure_message_id(raw, parsed)
- field = parsed.header.fields.find { |elem| elem.name == 'Message-ID' }
- return true if field&.unparsed_value.present?
- parsed.message_id = generate_message_id(raw, parsed.from)
- end
- def message_header_hash(mail)
- imported_fields = mail.header.fields.to_h do |f|
- begin
- value = if f.value.match?(ISO2022JP_REGEXP)
- value = header_field_unpack_japanese(f)
- else
- f.decoded.to_utf8
- end
- # fields that cannot be cleanly parsed fallback to the empty string
- rescue Mail::Field::IncompleteParseError
- value = ''
- rescue Encoding::CompatibilityError => e
- try_iso88591 = f.value.force_encoding('iso-8859-1').encode('utf-8')
- raise e if !try_iso88591.is_utf8?
- f.value = try_iso88591
- value = f.decoded.to_utf8
- rescue Date::Error => e
- raise e if !f.name.eql?('Resent-Date')
- f.value = ''
- rescue
- value = f.decoded.to_utf8(fallback: :read_as_sanitized_binary)
- end
- [f.name.downcase, value]
- end
- # imported_fields = mail.header.fields.map { |f| [f.name.downcase, f.to_utf8] }.to_h
- raw_fields = mail.header.fields.index_by { |f| "raw-
- custom_fields = {}.tap do |h|
- h.replace(imported_fields.slice(*RECIPIENT_FIELDS)
- .transform_values { |v| v.match?(EMAIL_REGEX) ? v : '' })
- h['x-any-recipient'] = h.values.compact_blank.join(', ')
- h['message_id'] = imported_fields['message-id']
- h['subject'] = imported_fields['subject']
- h['date'] = begin
- Time.zone.parse(mail.date.to_s)
- rescue
- nil
- end
- end
- [imported_fields, raw_fields, custom_fields].reduce({}.with_indifferent_access, &:merge)
- end
- def message_body_hash(mail)
- if mail.html_part&.body.present?
- content_type = mail.html_part.mime_type || 'text/plain'
- (body, sanitized_body_info) = body_text(mail.html_part, strict_html: true)
- elsif mail.text_part.present? && mail.all_parts.any? { |elem| elem.inline? && elem.content_type&.start_with?('image') }
- content_type = 'text/html'
- body = mail
- .all_parts
- .reduce('') do |memo, part|
- if part.mime_type == 'text/plain' && !part.attachment?
- memo += body_text(part, strict_html: false).first.text2html
- elsif part.inline? && part.content_type&.start_with?('image')
- memo += "<img src='cid:#{part.cid}'>"
- end
- memo
- end
- elsif mail.text_part.present?
- content_type = 'text/plain'
- body = mail
- .all_parts
- .reduce('') do |memo, part|
- if part.mime_type == 'text/plain' && !part.attachment?
- memo += body_text(part, strict_html: false).first
- end
- memo
- end
- elsif mail&.body.present? && (mail.mime_type.nil? || mail.mime_type.match?(%r{^text/(plain|html)$}))
- content_type = mail.mime_type || 'text/plain'
- (body, sanitized_body_info) = body_text(mail, strict_html: content_type.eql?('text/html'))
- end
- content_type = 'text/plain' if body.blank?
- {
- attachments: collect_attachments(mail),
- content_type: content_type || 'text/plain',
- body: body.presence || 'no visible content',
- sanitized_body_info: sanitized_body_info || {},
- }.with_indifferent_access
- end
- def body_text(message, **options)
- body_text = begin
- message.body.to_s
- rescue Mail::UnknownEncodingType
- message.body.raw_source
- end
- body_text = body_text.utf8_encode(from: message.charset, fallback: :read_as_sanitized_binary)
- body_text = Mail::Utilities.to_lf(body_text)
-
- return [body_text, {}] if !options[:strict_html]
-
- return [EXCESSIVE_LINKS_MSG, {}] if body_text.scan(%r{<a[[:space:]]}i).count >= 5_000
- body_text.html2html_strict
- end
- def collect_attachments(mail)
- attachments = []
- attachments.push(*get_nonplaintext_body_as_attachment(mail))
- mail.parts.each do |part|
- attachments.push(*gracefully_get_attachments(part, attachments, mail))
- end
- attachments
- end
- def get_nonplaintext_body_as_attachment(mail)
- if !(mail.html_part&.body.present? || (!mail.multipart? && mail.mime_type.present? && mail.mime_type != 'text/plain'))
- return
- end
- message = mail.html_part || mail
- if !mail.mime_type.starts_with?('text/') && mail.html_part.blank?
- return gracefully_get_attachments(message, [], mail)
- end
- filename = message.filename.presence || (message.mime_type.eql?('text/html') ? 'message.html' : '-no name-')
- headers_store = {
- 'content-alternative' => true,
- 'original-format' => message.mime_type.eql?('text/html'),
- 'Mime-Type' => message.mime_type,
- 'Charset' => message.charset,
- }.compact_blank
- [{
- data: body_text(message).first,
- filename: filename,
- preferences: headers_store
- }]
- end
- def gracefully_get_attachments(part, attachments, mail)
- get_attachments(part, attachments, mail).flatten.compact
- rescue => e
- raise e if (fail_count ||= 0).positive?
- (fail_count += 1) && retry
- end
- def get_attachments(file, attachments, mail)
- return file.parts.map { |p| get_attachments(p, attachments, mail) } if file.parts.any?
- return [] if [mail.text_part&.body&.encoded, mail.html_part&.body&.encoded].include?(file.body.encoded)
- return [] if file.content_type&.start_with?('text/plain') && !file.attachment?
-
- headers_store = {}
- file.header.fields.each do |field|
-
- value = field.to_utf8
- if value.blank?
- value = field.raw_value
- end
- headers_store[field.name.to_s] = value
- rescue
- headers_store[field.name.to_s] = field.raw_value
- end
-
- if headers_store['Content-ID'].blank? && headers_store['Content-Id'].present?
- headers_store['Content-ID'] = headers_store['Content-Id']
- end
- if headers_store['Content-ID']
- headers_store['Content-ID'].delete_prefix!('<')
- headers_store['Content-ID'].delete_suffix!('>')
- end
-
-
- begin
- filename = file.header[:content_disposition].try(:filename)
- rescue
- begin
- case file.header[:content_disposition].to_s
- when %r{(filename|name)(\*{0,1})="(.+?)"}i, %r{(filename|name)(\*{0,1})='(.+?)'}i, %r{(filename|name)(\*{0,1})=(.+?);}i
- filename = $3
- end
- rescue
- Rails.logger.debug { 'Unable to get filename' }
- end
- end
- begin
- case file.header[:content_disposition].to_s
- when %r{(filename|name)(\*{0,1})="(.+?)"}i, %r{(filename|name)(\*{0,1})='(.+?)'}i, %r{(filename|name)(\*{0,1})=(.+?);}i
- filename = $3
- end
- rescue
- Rails.logger.debug { 'Unable to get filename' }
- end
-
- if filename.blank?
- case headers_store['Content-Disposition'].to_s
- when %r{(filename|name)(\*{0,1})="(.+?)"}i, %r{(filename|name)(\*{0,1})='(.+?)'}i, %r{(filename|name)(\*{0,1})=(.+?);}i
- filename = $3
- end
- end
-
- filename ||= file.header[:content_location].to_s.dup.force_encoding('utf-8')
- file_body = String.new(file.body.to_s)
-
- if filename.blank? && headers_store['Content-Type'].present? && headers_store['Content-Type'].match?(%r{^message/rfc822}i)
- begin
- parser = Channel::EmailParser.new
- mail_local = parser.parse(file_body)
- filename = if mail_local[:subject].present?
- "#{mail_local[:subject]}.eml"
- elsif headers_store['Content-Description'].present?
- "#{headers_store['Content-Description']}.eml".to_s.force_encoding('utf-8')
- else
- 'Mail.eml'
- end
- rescue
- filename = 'Mail.eml'
- end
- end
-
- if filename.blank?
- ['(filename|name)(\*{0,1})="(.+?)"(;|$)', '(filename|name)(\*{0,1})=\'(.+?)\'(;|$)', '(filename|name)(\*{0,1})=(.+?)(;|$)'].each do |regexp|
- if headers_store['Content-Type'] =~ %r{#{regexp}}i
- filename = $3
- break
- end
- end
- end
-
-
- if filename.present?
- filename = Mail::Encodings.value_decode(filename)
- end
- if !filename.force_encoding('UTF-8').valid_encoding?
- filename = filename.utf8_encode(fallback: :read_as_sanitized_binary)
- end
-
- if filename.blank? && headers_store['Content-ID'].present? && headers_store['Content-ID'] =~ %r{(.+?\..{2,6})@.+?}i
- filename = $1
- end
-
- if filename.blank? && (content_type = headers_store['Content-Type'])
- map = {
- 'message/delivery-status': %w[txt delivery-status],
- 'text/plain': %w[txt document],
- 'text/html': %w[html document],
- 'video/quicktime': %w[mov video],
- 'image/jpeg': %w[jpg image],
- 'image/jpg': %w[jpg image],
- 'image/png': %w[png image],
- 'image/gif': %w[gif image],
- 'text/calendar': %w[ics calendar],
- }
- map.each do |type, ext|
- next if !content_type.match?(%r{^#{Regexp.quote(type)}}i)
- filename = if headers_store['Content-Description'].present?
- "#{headers_store['Content-Description']}.#{ext[0]}".to_s.force_encoding('utf-8')
- else
- "#{ext[1]}.#{ext[0]}"
- end
- break
- end
- end
-
- if filename.blank? && headers_store['Content-ID'].present? && headers_store['Content-ID'] =~ %r{(.+?)@.+?}i
- filename = $1
- end
-
- if filename.blank?
- filename = 'file'
- end
-
- local_filename = ''
- local_extention = ''
- if filename =~ %r{^(.*?)\.(.+?)$}
- local_filename = $1
- local_extention = $2
- end
- 1.upto(1000) do |i|
- filename_exists = false
- attachments.each do |attachment|
- if attachment[:filename] == filename
- filename_exists = true
- end
- end
- break if filename_exists == false
- filename = if local_extention.present?
- "#{local_filename}#{i}.#{local_extention}"
- else
- "#{local_filename}#{i}"
- end
- end
-
- if file.header[:content_type]&.string
- headers_store['Mime-Type'] = file.header[:content_type].string
- end
-
- if file.header&.charset
- headers_store['Charset'] = file.header.charset
- end
-
- headers_store.delete('Content-Transfer-Encoding')
- headers_store.delete('Content-Disposition')
- attach = {
- data: file_body,
- filename: filename,
- preferences: headers_store,
- }
- [attach]
- end
-
-
- def postmaster_response(channel, msg)
- begin
- reply_mail = compose_postmaster_reply(msg)
- rescue NotificationFactory::FileNotFoundError => e
- Rails.logger.error "No valid postmaster email_oversized template found. Skipping postmaster reply. #{e.inspect}"
- return
- end
- Rails.logger.info "Send mail too large postmaster message to: #{reply_mail[:to]}"
- reply_mail[:from] = EmailAddress.find_by(channel: channel).email
- channel.deliver(reply_mail)
- rescue => e
- Rails.logger.error "Error during sending of postmaster oversized email auto-reply: #{e.inspect}\n#{e.backtrace}"
- end
-
- def compose_postmaster_reply(raw_incoming_mail, locale = nil)
- parsed_incoming_mail = Channel::EmailParser.new.parse(raw_incoming_mail)
-
- mail = MESSAGE_STRUCT.new
- mail.from_display_name = parsed_incoming_mail[:from_display_name]
- mail.subject = parsed_incoming_mail[:subject]
- mail.msg_size = format('%<MB>.2f', MB: raw_incoming_mail.size.to_f / 1024 / 1024)
- reply = NotificationFactory::Mailer.template(
- template: 'email_oversized',
- locale: locale,
- format: 'txt',
- objects: {
- mail: mail,
- },
- raw: true,
- standalone: true,
- )
- reply.merge(
- to: parsed_incoming_mail[:from_email],
- body: reply[:body].gsub(%r{\n}, "\r\n"),
- content_type: 'text/plain',
- References: parsed_incoming_mail[:message_id],
- 'In-Reply-To': parsed_incoming_mail[:message_id],
- )
- end
- def guess_email_fqdn(from)
- Mail::Address.new(from).domain.strip
- rescue
- nil
- end
- def generate_message_id(raw_message, from)
- fqdn = guess_email_fqdn(from) || 'zammad_generated'
- "<gen-#{Digest::MD5.hexdigest(raw_message)}@#{fqdn}>"
- end
-
-
-
-
-
-
-
-
- def force_japanese_encoding(input)
- %w[ISO-2022-JP ISO-2022-JP-KDDI SJIS]
- .lazy
- .map { |encoding| try_encoding(input, encoding) }
- .detect(&:present?)
- end
- def try_encoding(input, encoding)
- input.force_encoding(encoding).encode('UTF-8')
- rescue
- nil
- end
- end
- module Mail
-
- class Field
- def raw_value
- begin
- value = @raw_value.try(:utf8_encode)
- rescue
- value = @raw_value.utf8_encode(fallback: :read_as_sanitized_binary)
- end
- return value if value.blank?
- value.sub(%r{^.+?:(\s|)}, '')
- end
- end
-
-
- class Body
- def decoded
- if Encodings.defined?(encoding)
- Encodings.get_encoding(encoding).decode(raw_source)
- else
- Rails.logger.info "UnknownEncodingType: Don't know how to decode #{encoding}!"
- raw_source
- end
- end
- end
- end
|