Skip to content

Commit

Permalink
updating the mailer with new rails 3 mail::message
Browse files Browse the repository at this point in the history
  • Loading branch information
Doug Youch committed Apr 1, 2011
1 parent 4562dad commit bf1791d
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 108 deletions.
131 changes: 35 additions & 96 deletions app/models/mail_template_mailer.rb
Expand Up @@ -29,115 +29,39 @@ class MailTemplateMailer < ActionMailer::Base
# Either :text or :html (or both) must be specified,
# :from is optional and will use the default or the
# Configuration reply to email
def message_to_address(email,subj,options ={})
subject subj
recipients email

if options[:from]
from options[:from]
elsif !Configuration.mailing_from.to_s.empty?
from "\"#{Configuration.mailing_from}\" <#{Configuration.reply_to_email}>"
else
from Configuration.reply_to_email
def message_to_address(email, subj, options ={})
raise "missing email content" if options[:text].blank? && options[:html].blank?

mail(:to => email, :subject => subj, :from => get_from_address(options)) do |format|
format.text { render :text => options[:text] } if options[:text]
format.html { render :text => options[:html] } if options[:html]
end

if options[:text] && options[:html]
content_type "multipart/alternative"

part :content_type => 'text/plain',
:body => options[:text]
part :content_type => "text/html", :transfer_encoding => '7bit',
:body => options[:html]
elsif options[:text]
content_type 'text/plain'
body options[:text]
elsif options[:html]
content_type "text/html"
body options[:html]
else
raise "missing :text or :html options for message body"
end

true
end

def to_address(email,mail_template,variables = {},queue_hash=nil) #:nodoc:
def to_address(email, mail_template, variables={}, queue_hash=nil) #:nodoc:
variables = variables.clone
variables.stringify_keys!

unless mail_template.is_a?(MailTemplate)
mail_template = MailTemplate.find(mail_template)
end

subject mail_template.render_subject(variables)
recipients email
raise "missing email content" unless mail_template.body_type.include?('text') || mail_template.body_type.include?('html')

if variables['system:from']
from variables['system:from']
elsif variables['system:from_mail_name']
from "\"#{variables['system:from_mail_name']}\" <#{Configuration.reply_to_email}>"
elsif !Configuration.mailing_from.to_s.empty?
from "\"#{Configuration.mailing_from}\" <#{Configuration.reply_to_email}>"
else
from Configuration.reply_to_email
((variables.delete('attachments') || []) + mail_template.attachments).each do |file|
file = file.is_a?(DomainFile) ? file.filename : file
name = File.basename(file)
mime_types = MIME::Types.type_for(file)
content_type = mime_types[0].to_s || 'text/plain'
attachments[name] = { :content => File.read(file), :content_type => content_type }
end

headers mail_template.additional_headers(variables)

sent_on Time.now

attachments = variables.delete('attachments') || []
attachments += mail_template.attachments

if attachments.length > 0

if mail_template.body_type.include?('text') && mail_template.body_type.include?('html')
part(:content_type => "multipart/alternative") do |p|
p.part :content_type => 'text/plain',
:body => mail_template.render_text(variables)
p.part :content_type => "text/html", :transfer_encoding => '7bit',
:body => mail_template.render_html(variables)
end
elsif mail_template.body_type.include?('text')
part :content_type => 'text/plain',
:body => mail_template.render_text(variables)
elsif mail_template.body_type.include?('html')
part :content_type => "text/html",
:body => mail_template.render_html(variables)
end
else
if mail_template.body_type.include?('text') && mail_template.body_type.include?('html')
content_type "multipart/alternative"

part :content_type => 'text/plain',
:body => mail_template.render_text(variables)
part :content_type => "text/html", :transfer_encoding => '7bit',
:body => mail_template.render_html(variables)
elsif mail_template.body_type.include?('text')
content_type 'text/plain'
body mail_template.render_text(variables)
elsif mail_template.body_type.include?('html')
content_type "text/html"
body mail_template.render_html(variables)
end

end

headers mail_template.additional_headers(variables)

if attachments.length > 0
attachments.each do |attached_file|
filename = attached_file.is_a?(DomainFile) ? attached_file.filename : attached_file
attachment_name = attached_file.is_a?(DomainFile) ? attached_file.name : File.basename(attached_file)
File.open(filename) do |file_obj|
mime_types = MIME::Types.type_for(filename)
attachment :body => file_obj.read,
:filename => attachment_name,
:content_type => mime_types[0].to_s || 'text/plain'
end
end
mail(:to => email, :subject => mail_template.render_subject(variables), :from => get_from_address(variables)) do |format|
format.text { render :text => mail_template.render_text(variables) } if mail_template.body_type.include?('text')
format.html { render :text => mail_template.render_html(variables) } if mail_template.body_type.include?('html')
end

true
end

# user should have email and name
Expand All @@ -146,7 +70,7 @@ def to_user(user,mail_template,variables = {}) #:nodoc:
user = EndUser.find(user)
end

vars = user.attributes.to_hash.merge(variables.to_hash)
vars = user.attributes.to_hash.merge(variables.to_hash)
vars[:name] = user.name if user

# Change language to the recipient if necessary
Expand All @@ -173,7 +97,6 @@ def to_user(user,mail_template,variables = {}) #:nodoc:
end

to_address(user.email,mail_template,vars)

end

def receive(email)
Expand Down Expand Up @@ -209,4 +132,20 @@ def receive(email)

handler_info[:class].receive(email)
end

protected

def get_from_address(options={})
if options[:from]
options[:from]
elsif options['system:from']
options['system:from']
elsif options['system:from_mail_name']
"\"#{options['system:from_mail_name']}\" <#{Configuration.reply_to_email}>"
elsif ! Configuration.mailing_from.to_s.empty?
"\"#{Configuration.mailing_from}\" <#{Configuration.reply_to_email}>"
else
Configuration.reply_to_email
end
end
end
2 changes: 1 addition & 1 deletion spec/controllers/mail_manager_controller_spec.rb
Expand Up @@ -101,7 +101,7 @@
it 'should generate the text of a template' do
@tmpl_body = post('generate_text_body', :html => "<br> <br> lots o txt </br> ",
:mail_template => @tmpl_post_options, :email => ['daffy1@mywebiva.net'], :path => 1)
response.body.should include(" lots o txt \n ")
response.body.should include("lots o txt")
end

it 'should delete a template' do
Expand Down
8 changes: 1 addition & 7 deletions spec/models/mail_template_mailer_spec.rb
Expand Up @@ -28,7 +28,7 @@
it 'should send a message to an email address' do
@direct_mail = MailTemplateMailer.message_to_address('bugsbunny@mywebiva.net','test subject', {:text => 'test text body', :html => "<b>test html body", :from => 'admin@mywebiva.net'} ).deliver
ActionMailer::Base.deliveries.size.should == 1
@direct_mail.body.should =~ /<b>test html body/
@direct_mail.html_part.to_s.should =~ /<b>test html body/
end
it 'should send mail template to address if available' do
create_complete_template
Expand Down Expand Up @@ -65,12 +65,6 @@
ActionMailer::Base.deliveries.size.should == 1
@tmpl_mail.to[0].should == "bugsbunny@mywebiva.com"
end
it 'should not be encoded when content type is text' do
create_complete_template
@tmpl_mail = MailTemplateMailer.to_user(@user.id,3, {:email => 'your_email'}).deliver
ActionMailer::Base.deliveries.size.should == 1
@tmpl_mail.content_transfer_encoding.should be_nil
end
end

def create_email(headers, body)
Expand Down
4 changes: 2 additions & 2 deletions spec/models/triggered_action_spec.rb
Expand Up @@ -19,8 +19,8 @@
:email_addresses => 'test@webiva.com'
).to_hash)

MailTemplateMailer.should_receive(:deliver_message_to_address).with('test@webiva.com','Test Message Subject',{ :html => 'Test Message Body' })

email = mock :deliver => nil
MailTemplateMailer.should_receive(:message_to_address).with('test@webiva.com','Test Message Subject',{ :html => 'Test Message Body' }).and_return(email)

@act.perform({},EndUser.new)
end
Expand Down
2 changes: 1 addition & 1 deletion vendor/modules/webform/app/models/webform_form_result.rb
Expand Up @@ -91,7 +91,7 @@ def assign_entry(values = {},application_state = {})
end

def send_result_to(email)
MailTemplateMailer.deliver_message_to_address(email, self.email_subject, :html => self.email_message_html_body, :text => self.email_message_text_body)
MailTemplateMailer.message_to_address(email, self.email_subject, :html => self.email_message_html_body, :text => self.email_message_text_body).deliver
end

def email_subject
Expand Down
Expand Up @@ -120,7 +120,8 @@ def generate_page_renderer(paragraph, options={}, inputs={})

assert_difference 'EndUser.count', 1 do
assert_difference 'WebformFormResult.count', 1 do
MailTemplateMailer.should_receive(:deliver_message_to_address).with('test-form@test.dev', anything(), anything())
email = mock :deliver => nil
MailTemplateMailer.should_receive(:message_to_address).with('test-form@test.dev', anything(), anything()).and_return(email)
param_str = 'results_' + @rnd.paragraph.id.to_s
renderer_post @rnd, param_str => @post_data
end
Expand Down

0 comments on commit bf1791d

Please sign in to comment.