Class: MailHandler

Inherits:
ActionMailer::Base
  • Object
show all
Includes:
ActionView::Helpers::SanitizeHelper, Redmine::I18n
Defined in:
app/models/mail_handler.rb

Overview

Redmine - project management software Copyright (C) 2006-2020 Jean-Philippe Lang

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

Defined Under Namespace

Classes: MissingInformation, UnauthorizedAction

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Redmine::I18n

#current_language, #day_letter, #day_name, #find_language, #format_date, #format_hours, #format_time, included, #l, #l_hours, #l_hours_short, #l_or_humanize, #languages_options, #ll, #lu, #month_name, #set_language_if_valid, #valid_languages

Instance Attribute Details

#emailObject (readonly)

Returns the value of attribute email


27
28
29
# File 'app/models/mail_handler.rb', line 27

def email
  @email
end

#handler_optionsObject (readonly)

Returns the value of attribute handler_options


27
28
29
# File 'app/models/mail_handler.rb', line 27

def handler_options
  @handler_options
end

#userObject (readonly)

Returns the value of attribute user


27
28
29
# File 'app/models/mail_handler.rb', line 27

def user
  @user
end

Class Method Details

.extract_options_from_env(env) ⇒ Object

Extracts MailHandler options from environment variables Use when receiving emails with rake tasks


63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/models/mail_handler.rb', line 63

def self.extract_options_from_env(env)
  options = {:issue => {}}
  %w(project status tracker category priority assigned_to fixed_version).each do |option|
    options[:issue][option.to_sym] = env[option] if env[option]
  end
  %w(allow_override unknown_user no_permission_check no_account_notice no_notification default_group project_from_subaddress).each do |option|
    options[option.to_sym] = env[option] if env[option]
  end
  if env['private']
    options[:issue][:is_private] = '1'
  end
  options
end

.html_body_to_text(html) ⇒ Object

Converts a HTML email body to text


540
541
542
# File 'app/models/mail_handler.rb', line 540

def html_body_to_text(html)
  Redmine::WikiFormatting.html_parser.to_text(html)
end

.new_user_from_attributes(email_address, fullname = nil) ⇒ Object

Returns a User from an email address and a full name


552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
# File 'app/models/mail_handler.rb', line 552

def new_user_from_attributes(email_address, fullname=nil)
  user = User.new

  # Truncating the email address would result in an invalid format
  user.mail = email_address
  assign_string_attribute_with_limit(user, 'login', email_address, User::LOGIN_LENGTH_LIMIT)

  names = fullname.blank? ? email_address.gsub(/@.*$/, '').split('.') : fullname.split
  assign_string_attribute_with_limit(user, 'firstname', names.shift, 30)
  assign_string_attribute_with_limit(user, 'lastname', names.join(' '), 30)
  user.lastname = '-' if user.lastname.blank?
  user.language = Setting.default_language
  user.generate_password = true
  user.mail_notification = 'only_my_events'

  unless user.valid?
    user. = "user#{Redmine::Utils.random_hex(6)}" unless user.errors[:login].blank?
    user.firstname = "-" unless user.errors[:firstname].blank?
    (puts user.errors[:lastname]; user.lastname  = "-") unless user.errors[:lastname].blank?
  end
  user
end

.plain_text_body_to_text(text) ⇒ Object

Converts a plain/text email body to text


545
546
547
548
549
# File 'app/models/mail_handler.rb', line 545

def plain_text_body_to_text(text)
  # Removes leading spaces that would cause the line to be rendered as
  # preformatted text with textile
  text.gsub(/^ +(?![*#])/, '')
end

.receive(raw_mail, options = {}) ⇒ Object


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/models/mail_handler.rb', line 29

def self.receive(raw_mail, options={})
  options = options.deep_dup

  options[:issue] ||= {}

  options[:allow_override] ||= []
  if options[:allow_override].is_a?(String)
    options[:allow_override] = options[:allow_override].split(',')
  end
  options[:allow_override].map! {|s| s.strip.downcase.gsub(/\s+/, '_')}
  # Project needs to be overridable if not specified
  options[:allow_override] << 'project' unless options[:issue].has_key?(:project)

  options[:no_account_notice] = (options[:no_account_notice].to_s == '1')
  options[:no_notification] = (options[:no_notification].to_s == '1')
  options[:no_permission_check] = (options[:no_permission_check].to_s == '1')

  ActiveSupport::Notifications.instrument("receive.action_mailer") do |payload|
    mail = Mail.new(raw_mail.b)
    set_payload_for_mail(payload, mail)
    new.receive(mail, options)
  end
end

.safe_receive(*args) ⇒ Object

Receives an email and rescues any exception


54
55
56
57
58
59
# File 'app/models/mail_handler.rb', line 54

def self.safe_receive(*args)
  receive(*args)
rescue => e
  Rails.logger.error "MailHandler: an unexpected error occurred when receiving email: #{e.message}"
  return false
end

Instance Method Details

#loggerObject


77
78
79
# File 'app/models/mail_handler.rb', line 77

def logger
  Rails.logger
end

#receive(email, options = {}) ⇒ Object

Processes incoming emails Returns the created object (eg. an issue, a message) or false


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/models/mail_handler.rb', line 89

def receive(email, options={})
  @email = email
  @handler_options = options
  sender_email = email.from.to_a.first.to_s.strip
  # Ignore emails received from the application emission address to avoid hell cycles
  emission_address = Setting.mail_from.to_s.gsub(/(?:.*<|>.*|\(.*\))/, '').strip
  if sender_email.casecmp(emission_address) == 0
    logger&.info "MailHandler: ignoring email from Redmine emission address [#{sender_email}]"
    return false
  end
  # Ignore auto generated emails
  self.class.ignored_emails_headers.each do |key, ignored_value|
    value = email.header[key]
    if value
      value = value.to_s.downcase
      if (ignored_value.is_a?(Regexp) && ignored_value.match?(value)) || value == ignored_value
        logger&.info "MailHandler: ignoring email with #{key}:#{value} header"
        return false
      end
    end
  end
  @user = User.find_by_mail(sender_email) if sender_email.present?
  if @user && !@user.active?
    logger&.info "MailHandler: ignoring email from non-active user [#{@user.}]"
    return false
  end
  if @user.nil?
    # Email was submitted by an unknown user
    case handler_options[:unknown_user]
    when 'accept'
      @user = User.anonymous
    when 'create'
      @user = create_user_from_email
      if @user
        logger&.info "MailHandler: [#{@user.}] account created"
        add_user_to_group(handler_options[:default_group])
        unless handler_options[:no_account_notice]
          ::Mailer.(@user, @user.password)
        end
      else
        logger&.error "MailHandler: could not create account for [#{sender_email}]"
        return false
      end
    else
      # Default behaviour, emails from unknown users are ignored
      logger&.info "MailHandler: ignoring email from unknown user [#{sender_email}]"
      return false
    end
  end
  User.current = @user
  dispatch
end