Class: MailHandler

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

Overview

– copyright ChiliProject is a project management system.

Copyright (C) 2010-2013 the ChiliProject Team

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.

See doc/COPYRIGHT.rdoc for more details. ++

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_name, #find_language, #format_date, #format_time, included, #l, #l_hours, #l_or_humanize, #ll, #month_name, #set_language_if_valid, #valid_languages

Instance Attribute Details

#emailObject (readonly)

Returns the value of attribute email


22
23
24
# File 'app/models/mail_handler.rb', line 22

def email
  @email
end

#userObject (readonly)

Returns the value of attribute user


22
23
24
# File 'app/models/mail_handler.rb', line 22

def user
  @user
end

Class Method Details

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


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/models/mail_handler.rb', line 24

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

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

  @@handler_options[:allow_override] = @@handler_options[:allow_override].split(',').collect(&:strip) if @@handler_options[:allow_override].is_a?(String)
  @@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_permission_check] = (@@handler_options[:no_permission_check].to_s == '1' ? true : false)
  super email
end

Instance Method Details

#receive(email) ⇒ Object

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


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/models/mail_handler.rb', line 42

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
    logger.info  "MailHandler: ignoring email from emission address [#{sender_email}]" if logger && logger.info
    return false
  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.}]" if logger && logger.info
    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 = MailHandler.create_user_from_email(email)
      if @user
        logger.info "MailHandler: [#{@user.}] account created" if logger && logger.info
        Mailer.(@user, @user.password)
      else
        logger.error "MailHandler: could not create account for [#{sender_email}]" if logger && logger.error
        return false
      end
    else
      # Default behaviour, emails from unknown users are ignored
      logger.info  "MailHandler: ignoring email from unknown user [#{sender_email}]" if logger && logger.info
      Mailer.deliver_mail_handler_unauthorized_action(user, email.subject.to_s, :to => sender_email) if Setting.mail_handler_confirmation_on_failure
      return false
    end
  end
  User.current = @user
  dispatch
end