orbit-4-2/app/mailer/mail_cron_mailer.rb

85 lines
2.6 KiB
Ruby
Raw Normal View History

2014-02-19 03:26:02 +00:00
# encoding: utf-8
class MailCronMailer < ActionMailer::Base
default :from => "orbit_test@rulingcom.com"
def get_settings
@site = Site.first
@smtp_settings = {
: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']
}
if !@site['site_settings']['user_name'].blank?
@smtp_settings[:user_name] = @site['site_settings']['user_name']
end
if !@site['site_settings']['password'].blank?
@smtp_settings[:password] = @site['site_settings']['password']
end
ActionMailer::Base.smtp_settings = @smtp_settings
end
def cron_mail( data_id, group_mail )
get_settings
@data = MailCron.find(data_id)
if @data
@user = User.find(@data.create_user_id)
unit_field = User.get_member_list_attribute_field("staff","Unit")
unit = User.get_member_list_attribute_value(@data.create_user_id,unit_field.id).get_value_by_locale(I18n.locale) rescue nil
if !@data.mail_cron_files.blank?
@data.mail_cron_files.each_with_index do |mail_cron_file, i|
file_type = File.extname(mail_cron_file.file.to_s)
file_content = Mongo::GridFileSystem.new(Mongoid.database).open(mail_cron_file.file.url.gsub("/gridfs/", "").force_encoding("UTF-8"), 'r')
attachments["#{mail_cron_file.title}#{file_type}"] = file_content.read
end
end
if !@data.mail_content["text"].blank?
mail( :from => "\"#{unit}\" <#{@data.mail_from}>", :reply_to => @data.mail_reply_to, :bcc => group_mail, :subject => @data.mail_subject ) do |format|
format.html { render :text => @data.mail_content["text"] }
end
elsif !@data.mail_content["template"].blank?
mail( :from => "\"#{unit}\" <#{@data.mail_from}>", :reply_to => @data.mail_reply_to, :bcc => group_mail, :subject => @data.mail_subject ) do |format|
format.html { render :template => @data.mail_content["template"], :collection => @data.mail_content }
end
end
@mail_cron_log = {
:mail_subject => @data.mail_subject,
:mail_to => group_mail,
:mail_user => "#{!unit.blank? ? unit : @user.name} <#{@data.mail_from}>",
# :mail_user => @user.name,
:mailer_count => group_mail.split(",").size,
:mail_from_app => @data.mail_from_app
}
@mail_cron_log = MailCronLog.new(@mail_cron_log)
@mail_cron_log.save
@data.destroy
end
end
end