Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple message templates #8

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions app/controllers/mail_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ def create
@page.request, @page.response = request, response

config, part_page = config_and_page(@page)
mail_config = config.dup
mail_config.delete(:redirect_to)

mail = Mail.new(part_page, config, params[:mailer])
mail = Mail.new(part_page, mail_config, params[:mailer])
@page.last_mail = part_page.last_mail = mail
process_mail(mail, config)

Expand All @@ -34,4 +36,4 @@ def config_and_page(page)
[(string.empty? ? {} : YAML::load(string).symbolize_keys), page]
end

end
end
99 changes: 54 additions & 45 deletions app/models/mail.rb
Original file line number Diff line number Diff line change
@@ -1,36 +1,42 @@
class Mail
attr_reader :page, :config, :data, :errors
# config can have some separate parts
def initialize(page, config, data)
@page, @config, @data = page, config.with_indifferent_access, data
@required = @data.delete(:required)
@errors = {}
end

def self.valid_config?(config)
return false if config['recipients'].blank? and config['recipients_field'].blank?
return false if config['from'].blank? and config['from_field'].blank?
mail_config = config.dup
return false if mail_config.blank?
mail_config.delete(:redirect_to)
mail_config.keys.each do |key|
return false if mail_config[key]['recipients'].blank? and mail_config[key]['recipients_field'].blank?
return false if mail_config[key]['from'].blank? and mail_config[key]['from_field'].blank?
end
true
end

def valid?
def valid?(config_key)
unless defined?(@valid)
@valid = true
if recipients.blank? and !is_required_field?(config[:recipients_field])
if recipients(config_key).blank? and !is_required_field?(config[config_key][:recipients_field])
errors['form'] = 'Recipients are required.'
@valid = false
end

if recipients.any?{|e| !valid_email?(e)}
if recipients(config_key).any?{|e| !valid_email?(e)}
errors['form'] = 'Recipients are invalid.'
@valid = false
end

if from.blank? and !is_required_field?(config[:from_field])
if from(config_key).blank? and !is_required_field?(config[config_key][:from_field])
errors['form'] = 'From is required.'
@valid = false
end

if !valid_email?(from)
if !valid_email?(from(config_key))
errors['form'] = 'From is invalid.'
@valid = false
end
Expand All @@ -47,59 +53,62 @@ def valid?
@valid
end

def from
config[:from] || data[config[:from_field]]
def from(config_key)
config[config_key][:from] || data[config[config_key][:from_field]]
end

def recipients
config[:recipients] || data[config[:recipients_field]].split(/,/).collect{|e| e.strip}
def recipients(config_key)
config[config_key][:recipients] || data[config[config_key][:recipients_field]].split(/,/).collect{|e| e.strip}
end

def reply_to
config[:reply_to] || data[config[:reply_to_field]]
def reply_to(config_key)
config[config_key][:reply_to] || data[config[config_key][:reply_to_field]]
end

def sender
config[:sender]
def sender(config_key)
config[config_key][:sender]
end

def subject
data[:subject] || config[:subject] || "Form Mail from #{page.request.host}"
def subject(config_key)
data[:subject] || config[config_key][:subject] || "Form Mail from #{page.request.host}"
end

def cc
data[config[:cc_field]] || config[:cc] || ""
def cc(config_key)
data[config[config_key][:cc_field]] || config[config_key][:cc] || ""
end

def send
return false if not valid?

plain_body = (page.part( :email ) ? page.render_part( :email ) : page.render_part( :email_plain ))
html_body = page.render_part( :email_html ) || nil
config.keys.each do |config_key|
return false if not valid? config_key

part_name = "email_#{config_key}".to_sym
plain_body = (page.part( part_name ) ? page.render_part( part_name ) : page.render_part( :email_plain ))
html_body = page.render_part( "#{part_name.to_s}_html".to_sym ) || nil

if plain_body.blank? and html_body.blank?
plain_body = <<-EMAIL
The following information was posted:
#{data.to_hash.to_yaml}
EMAIL
end

if plain_body.blank? and html_body.blank?
plain_body = <<-EMAIL
The following information was posted:
#{data.to_hash.to_yaml}
EMAIL
end
headers = { 'Reply-To' => reply_to(config_key) || from(config_key) }
if sender(config_key)
headers['Return-Path'] = sender(config_key)
headers['Sender'] = sender(config_key)
end

headers = { 'Reply-To' => reply_to || from }
if sender
headers['Return-Path'] = sender
headers['Sender'] = sender
Mailer.deliver_generic_mail(
:recipients => recipients(config_key),
:from => from(config_key),
:subject => subject(config_key),
:plain_body => plain_body,
:html_body => html_body,
:cc => cc(config_key),
:headers => headers
)
@sent = true
end

Mailer.deliver_generic_mail(
:recipients => recipients,
:from => from,
:subject => subject,
:plain_body => plain_body,
:html_body => html_body,
:cc => cc,
:headers => headers
)
@sent = true
rescue Exception => e
errors['base'] = e.message
@sent = false
Expand All @@ -118,4 +127,4 @@ def valid_email?(email)
def is_required_field?(field_name)
@required && @required.any? {|name,_| name == field_name}
end
end
end
6 changes: 4 additions & 2 deletions lib/mailer_process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ def process_with_mailer(request, response)
# If configured to do so, receive and process the mailer POST
if Radiant::Config['mailer.post_to_page?'] && request.post? && request.parameters[:mailer]
config, part_page = mailer_config_and_page
mail_config = config.dup
mail_config.delete(:redirect_to)

mail = Mail.new(part_page, config, request.parameters[:mailer])
mail = Mail.new(part_page, mail_config, request.parameters[:mailer])
self.last_mail = part_page.last_mail = mail

if mail.send
Expand All @@ -33,4 +35,4 @@ def mailer_config_and_page
string = page.render_part(:mailer)
[(string.empty? ? {} : YAML::load(string).symbolize_keys), page]
end
end
end
4 changes: 2 additions & 2 deletions lib/mailer_tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def config
page = page.parent
end
string = page.render_part(:mailer)
(string.empty? ? {} : YAML::load(string))
(string.empty? ? {} : YAML::load(string).symbolize_keys)
end
end

Expand Down Expand Up @@ -327,4 +327,4 @@ def add_required(result, tag)
def raise_error_if_name_missing(tag_name, tag_attr)
raise "`#{tag_name}' tag requires a `name' attribute" if tag_attr['name'].blank?
end
end
end