summaryrefslogtreecommitdiffstats
path: root/app/models/mail_handler.rb
diff options
context:
space:
mode:
authorJean-Philippe Lang <jp_lang@yahoo.fr>2009-06-14 14:48:34 +0000
committerJean-Philippe Lang <jp_lang@yahoo.fr>2009-06-14 14:48:34 +0000
commitb3afde14fa6046a022eda73edee85ab48171909f (patch)
tree89dc8da3ab3f51ab9ed88fcc04e216822410aebe /app/models/mail_handler.rb
parentc48193f8c113650c14d98b2f96279d57118df973 (diff)
downloadredmine-b3afde14fa6046a022eda73edee85ab48171909f.tar.gz
redmine-b3afde14fa6046a022eda73edee85ab48171909f.zip
Ability to accept incoming emails from unknown users (#2230, #3003).
An option lets you specify how to handle emails from unknown users: * ignore: the email is ignored (previous and default behaviour) * accept: the sender is considered as an anonymous user * create: a user account is created (username/password are sent back to the user) Permissions have to be consistent with the chosen option. Eg. if you choose 'create', the 'Non member' role must have the 'Add issues' permission so that an issue can be created by an unknown user via email. If you choose 'accept', the 'Anonymous' role must have this permission. git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@2789 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'app/models/mail_handler.rb')
-rw-r--r--app/models/mail_handler.rb48
1 files changed, 43 insertions, 5 deletions
diff --git a/app/models/mail_handler.rb b/app/models/mail_handler.rb
index 027665329..6d02ae3ed 100644
--- a/app/models/mail_handler.rb
+++ b/app/models/mail_handler.rb
@@ -38,15 +38,34 @@ class MailHandler < ActionMailer::Base
end
# Processes incoming emails
+ # Returns the created object (eg. an issue, a message) or false
def receive(email)
@email = email
- @user = User.active.find_by_mail(email.from.to_a.first.to_s.strip)
- unless @user
- # Unknown user => the email is ignored
- # TODO: ability to create the user's account
- logger.info "MailHandler: email submitted by unknown user [#{email.from.first}]" if logger && logger.info
+ @user = User.find_by_mail(email.from.to_a.first.to_s.strip)
+ if @user && !@user.active?
+ logger.info "MailHandler: ignoring email from non-active user [#{@user.login}]" 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.login}] account created" if logger && logger.info
+ Mailer.deliver_account_information(@user, @user.password)
+ else
+ logger.error "MailHandler: could not create account for [#{email.from.first}]" if logger && logger.error
+ return false
+ end
+ else
+ # Default behaviour, emails from unknown users are ignored
+ logger.info "MailHandler: ignoring email from unknown user [#{email.from.first}]" if logger && logger.info
+ return false
+ end
+ end
User.current = @user
dispatch
end
@@ -239,4 +258,23 @@ class MailHandler < ActionMailer::Base
def self.full_sanitizer
@full_sanitizer ||= HTML::FullSanitizer.new
end
+
+ # Creates a user account for the +email+ sender
+ def self.create_user_from_email(email)
+ addr = email.from_addrs.to_a.first
+ if addr && !addr.spec.blank?
+ user = User.new
+ user.mail = addr.spec
+
+ names = addr.name.blank? ? addr.spec.gsub(/@.*$/, '').split('.') : addr.name.split
+ user.firstname = names.shift
+ user.lastname = names.join(' ')
+ user.lastname = '-' if user.lastname.blank?
+
+ user.login = user.mail
+ user.password = ActiveSupport::SecureRandom.hex(5)
+ user.language = Setting.default_language
+ user.save ? user : nil
+ end
+ end
end