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-2016 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



25
26
27
# File 'app/models/mail_handler.rb', line 25

def email
  @email
end

#handler_optionsObject (readonly)

Returns the value of attribute handler_options



25
26
27
# File 'app/models/mail_handler.rb', line 25

def handler_options
  @handler_options
end

#userObject (readonly)

Returns the value of attribute user



25
26
27
# File 'app/models/mail_handler.rb', line 25

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 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

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



27
28
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 27

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')

  raw_mail.force_encoding('ASCII-8BIT')

  ActiveSupport::Notifications.instrument("receive.action_mailer") do |payload|
    mail = Mail.new(raw_mail)
    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 Exception => e
  logger.error "MailHandler: an unexpected error occurred when receiving email: #{e.message}" if logger
  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
141
142
143
144
145
146
147
148
149
150
151
# 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
  if sender_email.casecmp(Setting.mail_from.to_s.strip) == 0
    if logger
      logger.info  "MailHandler: ignoring email from Redmine emission address [#{sender_email}]"
    end
    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) && value.match(ignored_value)) || value == ignored_value
        if logger
          logger.info "MailHandler: ignoring email with #{key}:#{value} header"
        end
        return false
      end
    end
  end
  @user = User.find_by_mail(sender_email) if sender_email.present?
  if @user && !@user.active?
    if logger
      logger.info  "MailHandler: ignoring email from non-active user [#{@user.}]"
    end
    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
        if logger
          logger.info "MailHandler: [#{@user.}] account created"
        end
        add_user_to_group(handler_options[:default_group])
        unless handler_options[:no_account_notice]
          Mailer.(@user, @user.password).deliver
        end
      else
        if logger
          logger.error "MailHandler: could not create account for [#{sender_email}]"
        end
        return false
      end
    else
      # Default behaviour, emails from unknown users are ignored
      if logger
        logger.info  "MailHandler: ignoring email from unknown user [#{sender_email}]"
      end
      return false
    end
  end
  User.current = @user
  dispatch
end