From e5f7bd4cacdcb0f216982f2219f0ba90f003b32e Mon Sep 17 00:00:00 2001 From: manson Date: Tue, 20 May 2014 19:29:51 +0800 Subject: [PATCH] Mailer --- .gitignore | 1 + app/controllers/admin/sites_controller.rb | 6 +- app/mailers/orbit_mailer.rb | 69 +++++++++++++++++++ app/models/email.rb | 52 ++++++++++++++ app/models/email_file.rb | 10 +++ app/models/email_log.rb | 27 ++++++++ app/views/admin/sites/_mail_cron.html.erb | 12 ++-- app/views/admin/sites/_mail_cron_log.html.erb | 10 +-- app/views/email/mail.html.erb | 9 +++ config/application.rb | 1 + 10 files changed, 183 insertions(+), 14 deletions(-) create mode 100644 app/mailers/orbit_mailer.rb create mode 100644 app/models/email.rb create mode 100644 app/models/email_file.rb create mode 100644 app/models/email_log.rb create mode 100644 app/views/email/mail.html.erb diff --git a/.gitignore b/.gitignore index 77555f7..97ef970 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,7 @@ # Ignore all logfiles and tempfiles. /log/*.log +/log/*.gz /tmp /Gemfile.lock diff --git a/app/controllers/admin/sites_controller.rb b/app/controllers/admin/sites_controller.rb index 68ff39e..3da7c88 100644 --- a/app/controllers/admin/sites_controller.rb +++ b/app/controllers/admin/sites_controller.rb @@ -32,12 +32,12 @@ class Admin::SitesController < OrbitAdminController @user_actions = UserAction.all.desc(:created_at).page(params[:page]).per(10) - # @mail_crons = MailCron.desc(:created_at) + @mail_crons = Email.can_deliver.desc(:created_at) - # @mail_cron_logs = MailCronLog.desc(:created_at).page(params[:page]).per(100) + @mail_cron_logs = EmailLog.desc(:created_at).page(params[:page]).per(10) respond_to do |format| - format.html # index.html.erb + format.html format.js end end diff --git a/app/mailers/orbit_mailer.rb b/app/mailers/orbit_mailer.rb new file mode 100644 index 0000000..f0fa7c7 --- /dev/null +++ b/app/mailers/orbit_mailer.rb @@ -0,0 +1,69 @@ +class OrbitMailer < ActionMailer::Base + default :from => "noreply@rulingcom.com" + + def setup + site = Site.first + + mail_setting = { + :address => site['site_settings']['address'], + :port => site['site_settings']['port'], + :domain => site['site_settings']['domain'], + :authentication => site['site_settings']['authentication'], + :enable_starttls_auto => site['site_settings']['enable_starttls_auto'], + :user_name => site['site_settings']['user_name'].blank? ? nil : site['site_settings']['user_name'], + :password => site['site_settings']['password'].blank? ? nil : site['site_settings']['password'] + } + + ActionMailer::Base.smtp_settings = mail_setting + end + + def set_mail(email) + setup + + I18n.locale = email.mail_lang + + site = Site.first + + mail_from = email.mail_from.nil? ? site['site_settings']['service_email'] : email.mail_from + mail_reply_to = email.mail_reply_to.nil? ? site['site_settings']['reply_email'] : email.mail_from + + unless email.email_files.blank? + email.email_files.each_with_index do |email_file, i| + file_type = File.extname(email_file.file.to_s) + file_content = File.read(email_file.file.url) + attachments["#{email_file.title}#{file_type}"] = file_content.read + end + end + + if email.template.nil? + mail( :from => "#{site.title} <#{mail_from}>", + :reply_to => mail_reply_to, + :bcc => email.mail_to.split(","), + :subject => email.mail_subject ) do |format| + format.html { render :text => email.mail_content } + end + else + @data = email.template_data + mail( :from => "#{site.title} <#{mail_from}>", + :reply_to => mail_reply_to, + :bcc => email.mail_to.split(","), + :subject => email.mail_subject ) do |format| + format.html { render :template => email.template, :collection => @data } + end + end + + mail_log = EmailLog.new({ + :mail_subject => email.mail_subject, + :mail_to => email.mail_to, + :mail_user => email.create_user, + :mailer_count => email.mail_to.split(",").size, + :module_app => email.module_app + }) + + mail_log.save + + # email.destroy + + end + +end \ No newline at end of file diff --git a/app/models/email.rb b/app/models/email.rb new file mode 100644 index 0000000..be96d2c --- /dev/null +++ b/app/models/email.rb @@ -0,0 +1,52 @@ +class Email + include Mongoid::Document + include Mongoid::Timestamps + + field :module_app_key + field :mail_from + field :mail_to + field :mail_reply_to + field :mail_subject + field :mail_content + field :mail_sentdate , :type => DateTime, :default => Time.now + field :mail_lang, :default => I18n.locale + + field :template # Path to template file + field :template_data # Data to render template + + field :create_user_id + field :update_user_id + + has_many :email_files, :autosave => true, :dependent => :destroy + + scope :can_deliver, ->{ where(:mail_sentdate.lte => Time.now) } + + def deliver + OrbitMailer.set_mail(self).deliver + end + + def module_app=(app) + self.module_app_key = app.key + end + + def module_app + ModuleApp.find_by(:key=>self.module_app_key) rescue nil + end + + def create_user=(user) + self.create_user_id = user.id + end + + def create_user + User.find(self.create_user_id) rescue nil + end + + def update_user=(user) + self.update_user_id = user.id + end + + def update_user + User.find(self.update_user_id) rescue nil + end + +end \ No newline at end of file diff --git a/app/models/email_file.rb b/app/models/email_file.rb new file mode 100644 index 0000000..97b4429 --- /dev/null +++ b/app/models/email_file.rb @@ -0,0 +1,10 @@ +class EmailFile + include Mongoid::Document + include Mongoid::Timestamps + + mount_uploader :file, AssetUploader + + field :title + + belongs_to :emails +end \ No newline at end of file diff --git a/app/models/email_log.rb b/app/models/email_log.rb new file mode 100644 index 0000000..64593c8 --- /dev/null +++ b/app/models/email_log.rb @@ -0,0 +1,27 @@ +class EmailLog + include Mongoid::Document + include Mongoid::Timestamps + + field :module_app_key + field :mail_subject + field :mail_to + field :mail_user_id + field :mailer_count + + def module_app=(app) + self.module_app_key = app.key + end + + def module_app + ModuleApp.find_by(:key=>self.module_app_key) rescue nil + end + + def mail_user=(user) + self.mail_user_id = user.id + end + + def mail_user + User.find(self.mail_user_id) rescue nil + end + +end \ No newline at end of file diff --git a/app/views/admin/sites/_mail_cron.html.erb b/app/views/admin/sites/_mail_cron.html.erb index 69e4643..6cb75bc 100644 --- a/app/views/admin/sites/_mail_cron.html.erb +++ b/app/views/admin/sites/_mail_cron.html.erb @@ -5,16 +5,16 @@ <%= check_box_tag 'to_delete[]', mail_cron.id, false, :class => "list-check" %> <% end -%> - <%= display_date_time(mail_cron.mail_sentdate) %> + <%= format_value(mail_cron.mail_sentdate) %> <%= mail_cron.mail_subject %>
- <%= mail_cron.mail_from_app %> + <%= mail_cron.module_app.key %> \ No newline at end of file diff --git a/app/views/admin/sites/_mail_cron_log.html.erb b/app/views/admin/sites/_mail_cron_log.html.erb index 827f4d4..5174194 100644 --- a/app/views/admin/sites/_mail_cron_log.html.erb +++ b/app/views/admin/sites/_mail_cron_log.html.erb @@ -5,15 +5,15 @@ <%= check_box_tag 'to_delete[]', mail_cron_log.id, false, :class => "list-check" %> <% end -%> - <%= display_date_time(mail_cron_log.created_at) %> + <%= format_value(mail_cron_log.created_at) %> <%= mail_cron_log.mail_subject %>
- <%= mail_cron_log.mail_user %> - <%= mail_cron_log.mail_from_app %> + <%= mail_cron_log.mail_user.user_name %> + <%= mail_cron_log.module_app.key %> \ No newline at end of file diff --git a/app/views/email/mail.html.erb b/app/views/email/mail.html.erb new file mode 100644 index 0000000..7d25e18 --- /dev/null +++ b/app/views/email/mail.html.erb @@ -0,0 +1,9 @@ + + + + + + +

<%= @data['title'] %>

+ + \ No newline at end of file diff --git a/config/application.rb b/config/application.rb index ca3b77a..44d2141 100644 --- a/config/application.rb +++ b/config/application.rb @@ -25,6 +25,7 @@ module Orbit # set default locale to something other than :en I18n.default_locale = :en I18n.available_locales = [:en, :zh_tw] + config.autoload_paths += %W(#{config.root}/lib)