Class: MailHandler
- Inherits:
-
ActionMailer::Base
- Object
- ActionMailer::Base
- MailHandler
- Includes:
- ActionView::Helpers::SanitizeHelper, Redmine::I18n
- Defined in:
- app/models/mail_handler.rb
Overview
Redmine - project management software Copyright © 2006- 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: InsufficientPermissions, LockedTopic, MissingContainer, MissingInformation, NotAllowedInProject, UnauthorizedAction
Instance Attribute Summary collapse
-
#email ⇒ Object
readonly
Returns the value of attribute email.
-
#handler_options ⇒ Object
readonly
Returns the value of attribute handler_options.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Class Method Summary collapse
-
.extract_options_from_env(env) ⇒ Object
Extracts MailHandler options from environment variables Use when receiving emails with rake tasks.
-
.html_body_to_text(html) ⇒ Object
Converts a HTML email body to text.
-
.new_user_from_attributes(email_address, fullname = nil) ⇒ Object
Returns a User from an email address and a full name.
-
.plain_text_body_to_text(text) ⇒ Object
Converts a plain/text email body to text.
- .receive(raw_mail, options = {}) ⇒ Object
-
.safe_receive(*args) ⇒ Object
Receives an email and rescues any exception.
Instance Method Summary collapse
- #logger ⇒ Object
-
#receive(email, options = {}) ⇒ Object
Processes incoming emails Returns the created object (eg. an issue, a message) or false.
Methods included from Redmine::I18n
#abbr_day_name, #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, #normalize_float, #set_language_if_valid, #valid_languages
Instance Attribute Details
#email ⇒ Object (readonly)
Returns the value of attribute email.
31 32 33 |
# File 'app/models/mail_handler.rb', line 31 def email @email end |
#handler_options ⇒ Object (readonly)
Returns the value of attribute handler_options.
31 32 33 |
# File 'app/models/mail_handler.rb', line 31 def @handler_options end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
31 32 33 |
# File 'app/models/mail_handler.rb', line 31 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
67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'app/models/mail_handler.rb', line 67 def self.(env) = {:issue => {}} %w(project status tracker category priority assigned_to fixed_version).each do |option| [: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| [option.to_sym] = env[option] if env[option] end if env['private'] [:issue][:is_private] = '1' end end |
.html_body_to_text(html) ⇒ Object
Converts a HTML email body to text
588 589 590 |
# File 'app/models/mail_handler.rb', line 588 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
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 |
# File 'app/models/mail_handler.rb', line 600 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.login = "user#{Redmine::Utils.random_hex(6)}" unless user.errors[:login].blank? user.firstname = "-" unless user.errors[:firstname].blank? 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
593 594 595 596 597 |
# File 'app/models/mail_handler.rb', line 593 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
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'app/models/mail_handler.rb', line 33 def self.receive(raw_mail, ={}) = .deep_dup [:issue] ||= {} [:allow_override] ||= [] if [:allow_override].is_a?(String) [:allow_override] = [:allow_override].split(',') end [:allow_override].map! {|s| s.strip.downcase.gsub(/\s+/, '_')} # Project needs to be overridable if not specified [:allow_override] << 'project' unless [:issue].has_key?(:project) [:no_account_notice] = ([:no_account_notice].to_s == '1') [:no_notification] = ([:no_notification].to_s == '1') [:no_permission_check] = ([: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, ) end end |
.safe_receive(*args) ⇒ Object
Receives an email and rescues any exception
58 59 60 61 62 63 |
# File 'app/models/mail_handler.rb', line 58 def self.safe_receive(*args) receive(*args) rescue => e Rails.logger.error "MailHandler: an unexpected error occurred when receiving email: #{e.}" return false end |
Instance Method Details
#logger ⇒ Object
81 82 83 |
# File 'app/models/mail_handler.rb', line 81 def logger Rails.logger end |
#receive(email, options = {}) ⇒ Object
Processes incoming emails Returns the created object (eg. an issue, a message) or false
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 93 def receive(email, ={}) @email = email @handler_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.login}]" return false end if @user.nil? # Email was submitted by an unknown user case [:unknown_user] when 'accept' @user = User.anonymous when 'create' @user = create_user_from_email if @user logger&.info "MailHandler: [#{@user.login}] account created" add_user_to_group([:default_group]) unless [:no_account_notice] ::Mailer.deliver_account_information(@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 |