Skip to content

Commit

Permalink
Refactored to retrieve email creds via an yaml file instead of saving…
Browse files Browse the repository at this point in the history
… them to the database via custom fields.
  • Loading branch information
mully committed Mar 18, 2008
1 parent 6a78132 commit 85635be
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 50 deletions.
86 changes: 50 additions & 36 deletions app/models/mail_reader.rb
@@ -1,10 +1,8 @@
require 'net/imap'

class MailReader < ActionMailer::Base

def receive(email)

issue = Issue.new(
issue = Issue.create(
:subject => email.subject,
:description => email.body,
:priority_id => 3,
Expand All @@ -13,7 +11,6 @@ def receive(email)
:author_id => 2,
:status_id => 1
)
issue.save

if email.has_attachments?
for attachment in email.attachments
Expand All @@ -27,41 +24,38 @@ def receive(email)
end

def self.check_mail
# Find all of the projects that have enabled the "ticket emailer" plugin
@projects = Project.find :all, :include=>:enabled_modules, :conditions=>"enabled_modules.name='ticket_emailer'"

begin
require 'net/imap'
rescue LoadError
raise RequiredLibraryNotFoundError.new('NET::Imap could not be loaded')
end

@projects.each do |@@project|

email_server = @@project.custom_values.detect {|v| v.custom_field_id == Setting.plugin_redmine_ticket_emailer['email_server'].to_i}
email_server = email_server.value if email_server

email_login = @@project.custom_values.detect {|v| v.custom_field_id == Setting.plugin_redmine_ticket_emailer['email_login'].to_i}
email_login = email_login.value if email_login

email_password = @@project.custom_values.detect {|v| v.custom_field_id == Setting.plugin_redmine_ticket_emailer['email_password'].to_i}
email_password = email_password.value if email_password

email_folder = @@project.custom_values.detect {|v| v.custom_field_id == Setting.plugin_redmine_ticket_emailer['email_folder'].to_i}
email_folder = email_folder.value if email_folder

use_ssl = @@project.custom_values.detect {|v| v.custom_field_id == Setting.plugin_redmine_ticket_emailer['use_ssl'].to_i}
use_ssl = use_ssl.value if use_ssl

email_port = @@project.custom_values.detect {|v| v.custom_field_id == Setting.plugin_redmine_ticket_emailer['email_port'].to_i}
email_port = email_port.value.to_i if email_port

imap = Net::IMAP.new(email_server, port=email_port, usessl=use_ssl)
@@config_path = (RAILS_ROOT + '/config/emailer.yml')

# Cycle through all of the projects created in the yaml file
YAML.load_file(@@config_path).keys.each do |project_name|

#Find the project based off the name in the YAML if the emailer is enabled in Redmine
@@project = Project.find_by_name project_name, :include=>:enabled_modules , :conditions=>"enabled_modules.name='ticket_emailer'"

imap.login(email_login, email_password)
imap.select(email_folder)
imap.search(['ALL']).each do |message_id|
msg = imap.fetch(message_id,'RFC822')[0].attr['RFC822']
MailReader.receive(msg)
#Mark message as deleted and it will be removed from storage when user session closd
imap.store(message_id, "+FLAGS", [:Deleted])
unless @@project.nil?
@@config = YAML.load_file(@@config_path)[project_name].symbolize_keys

imap = Net::IMAP.new(@@config[:email_server], port=@@config[:email_port], usessl=@@config[:use_ssl])

imap.login(@@config[:email_login], @@config[:email_password])
imap.select(@@config[:email_folder])

imap.search(['ALL']).each do |message_id|
msg = imap.fetch(message_id,'RFC822')[0].attr['RFC822']
MailReader.receive(msg)
#Mark message as deleted and it will be removed from storage when user session closd
imap.store(message_id, "+FLAGS", [:Deleted])
end
# tell server to permanently remove all messages flagged as :Deleted
imap.expunge()
end
# tell server to permanently remove all messages flagged as :Deleted
imap.expunge()
end
end

Expand All @@ -79,5 +73,25 @@ def attach_files(obj, attachment)
end
attached
end

private

def connect_to_email
begin
require 'net/imap'
rescue LoadError
raise RequiredLibraryNotFoundError.new('NET::Imap could not be loaded')
end

begin
@@config_path = (RAILS_ROOT + '/config/emailer.yml')
@@config = YAML.load_file(@@config_path)['mindbites'].symbolize_keys
end

imap = Net::IMAP.new(@@config[:email_server], port=@@config[:email_port], usessl=@@config[:use_ssl])

imap.login(@@config[:email_login], @@config[:email_password])
imap.select(@@config[:email_folder])
end

end
14 changes: 0 additions & 14 deletions init.rb
Expand Up @@ -25,18 +25,6 @@
author 'Jim Mulholland'
description 'A plugin to allow users to email tickets to Redmine.'
version '0.1.0'

# The data necessary to log into your email server.
# These setting values will be id fields used to map to the
# project custom fields that will be set-up to add email
# server data on an individual project basis.
# For this first rev, I am assuming you have an IMAP server
settings :default => {'email_server' => 0,
'email_login' => 0,
'email_password' => 0,
'use_ssl' => 0,
'email_port' => 0,
'email_folder' => 0}, :partial => 'settings/settings'

# This plugin adds a project module
# It can be enabled/disabled at project level (Project settings -> Modules)
Expand All @@ -46,6 +34,4 @@
permission :view_ticket_emailer, {:ticket_emailer => :show}
end

# A new item is added to the project menu
menu :project_menu, 'Emailer Setup', :controller => 'ticket_emailer', :action => 'show'
end

0 comments on commit 85635be

Please sign in to comment.