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

Constant Summary collapse

@@ignored_emails_headers =
{
  'X-Auto-Response-Suppress' => 'oof',
  'Auto-Submitted' => /\Aauto-(replied|generated)/,
  'X-Autoreply' => 'yes'
}

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_time, included, #l, #l_hours, #l_or_humanize, #languages_options, #ll, #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

#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


59
60
61
62
63
64
65
66
67
68
# File 'app/models/mail_handler.rb', line 59

def self.extract_options_from_env(env)
  options = {:issue => {}}
  %w(project status tracker category priority).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).each do |option|
    options[option.to_sym] = env[option] if env[option]
  end
  options
end

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


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

def self.receive(email, options={})
  @@handler_options = options.deep_dup

  @@handler_options[:issue] ||= {}

  if @@handler_options[:allow_override].is_a?(String)
    @@handler_options[:allow_override] = @@handler_options[:allow_override].split(',').collect(&:strip)
  end
  @@handler_options[:allow_override] ||= []
  # Project needs to be overridable if not specified
  @@handler_options[:allow_override] << 'project' unless @@handler_options[:issue].has_key?(:project)
  # Status overridable by default
  @@handler_options[:allow_override] << 'status' unless @@handler_options[:issue].has_key?(:status)

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

  email.force_encoding('ASCII-8BIT')
  super(email)
end

.safe_receive(*args) ⇒ Object

Receives an email and rescues any exception


50
51
52
53
54
55
# File 'app/models/mail_handler.rb', line 50

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

Instance Method Details

#loggerObject


70
71
72
# File 'app/models/mail_handler.rb', line 70

def logger
  Rails.logger
end

#receive(email) ⇒ Object

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


83
84
85
86
87
88
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
# File 'app/models/mail_handler.rb', line 83

def receive(email)
  @email = email
  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.downcase == Setting.mail_from.to_s.strip.downcase
    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