forked from saurabh/orbit4-5
Mailer
This commit is contained in:
parent
cd86433abf
commit
e5f7bd4cac
|
@ -13,6 +13,7 @@
|
||||||
|
|
||||||
# Ignore all logfiles and tempfiles.
|
# Ignore all logfiles and tempfiles.
|
||||||
/log/*.log
|
/log/*.log
|
||||||
|
/log/*.gz
|
||||||
/tmp
|
/tmp
|
||||||
|
|
||||||
/Gemfile.lock
|
/Gemfile.lock
|
||||||
|
|
|
@ -32,12 +32,12 @@ class Admin::SitesController < OrbitAdminController
|
||||||
|
|
||||||
@user_actions = UserAction.all.desc(:created_at).page(params[:page]).per(10)
|
@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|
|
respond_to do |format|
|
||||||
format.html # index.html.erb
|
format.html
|
||||||
format.js
|
format.js
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -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
|
|
@ -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
|
|
@ -0,0 +1,10 @@
|
||||||
|
class EmailFile
|
||||||
|
include Mongoid::Document
|
||||||
|
include Mongoid::Timestamps
|
||||||
|
|
||||||
|
mount_uploader :file, AssetUploader
|
||||||
|
|
||||||
|
field :title
|
||||||
|
|
||||||
|
belongs_to :emails
|
||||||
|
end
|
|
@ -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
|
|
@ -5,16 +5,16 @@
|
||||||
<%= check_box_tag 'to_delete[]', mail_cron.id, false, :class => "list-check" %>
|
<%= check_box_tag 'to_delete[]', mail_cron.id, false, :class => "list-check" %>
|
||||||
<% end -%>
|
<% end -%>
|
||||||
</td>
|
</td>
|
||||||
<td><%= display_date_time(mail_cron.mail_sentdate) %></td>
|
<td><%= format_value(mail_cron.mail_sentdate) %></td>
|
||||||
<td><%= mail_cron.mail_subject %>
|
<td><%= mail_cron.mail_subject %>
|
||||||
<div class="quick-edit">
|
<div class="quick-edit">
|
||||||
<ul class="nav nav-pills">
|
<ul class="nav nav-pills">
|
||||||
<%= content_tag(:li, link_to(t(:detail), admin_mail_cron_path(mail_cron),:target => '_blank')) if at_least_module_manager %>
|
<%#= content_tag(:li, link_to(t(:detail), admin_mail_cron_path(mail_cron),:target => '_blank')) if at_least_module_manager %>
|
||||||
<%if at_least_module_manager %>
|
<%#if at_least_module_manager %>
|
||||||
<li><%= link_to t(:delete_), admin_mail_cron_path(mail_cron), :class=>"text-error", :confirm => t('sure?'), :method => :delete, :remote => true %></li>
|
<li><%#= link_to t(:delete_), admin_mail_cron_path(mail_cron), :class=>"text-error", :confirm => t('sure?'), :method => :delete, :remote => true %></li>
|
||||||
<% end -%>
|
<%# end -%>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
</td>
|
</td>
|
||||||
<td><%= mail_cron.mail_from_app %></td>
|
<td><%= mail_cron.module_app.key %></td>
|
||||||
</tr>
|
</tr>
|
|
@ -5,15 +5,15 @@
|
||||||
<%= check_box_tag 'to_delete[]', mail_cron_log.id, false, :class => "list-check" %>
|
<%= check_box_tag 'to_delete[]', mail_cron_log.id, false, :class => "list-check" %>
|
||||||
<% end -%>
|
<% end -%>
|
||||||
</td>
|
</td>
|
||||||
<td><%= display_date_time(mail_cron_log.created_at) %></td>
|
<td><%= format_value(mail_cron_log.created_at) %></td>
|
||||||
<td><%= mail_cron_log.mail_subject %>
|
<td><%= mail_cron_log.mail_subject %>
|
||||||
<div class="quick-edit">
|
<div class="quick-edit">
|
||||||
<ul class="nav nav-pills">
|
<ul class="nav nav-pills">
|
||||||
<%= content_tag(:li, link_to(t(:detail), admin_mail_cron_log_path(mail_cron_log),:target => '_blank')) if at_least_module_manager %>
|
<%#= content_tag(:li, link_to(t(:detail), admin_mail_cron_log_path(mail_cron_log),:target => '_blank')) if at_least_module_manager %>
|
||||||
<%= content_tag(:li, link_to(t(:delete_),admin_mail_cron_log_path(mail_cron_log), :confirm => t(:sure?), :method => :delete, :class=>"text-error", :remote => true)) if at_least_module_manager %>
|
<%#= content_tag(:li, link_to(t(:delete_),admin_mail_cron_log_path(mail_cron_log), :confirm => t(:sure?), :method => :delete, :class=>"text-error", :remote => true)) if at_least_module_manager %>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
</td>
|
</td>
|
||||||
<td><%= mail_cron_log.mail_user %> </td>
|
<td><%= mail_cron_log.mail_user.user_name %> </td>
|
||||||
<td><%= mail_cron_log.mail_from_app %> </td>
|
<td><%= mail_cron_log.module_app.key %> </td>
|
||||||
</tr>
|
</tr>
|
|
@ -0,0 +1,9 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1><%= @data['title'] %></h1>
|
||||||
|
</body>
|
||||||
|
</html>
|
|
@ -25,6 +25,7 @@ module Orbit
|
||||||
# set default locale to something other than :en
|
# set default locale to something other than :en
|
||||||
I18n.default_locale = :en
|
I18n.default_locale = :en
|
||||||
I18n.available_locales = [:en, :zh_tw]
|
I18n.available_locales = [:en, :zh_tw]
|
||||||
|
|
||||||
config.autoload_paths += %W(#{config.root}/lib)
|
config.autoload_paths += %W(#{config.root}/lib)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue