reuse action mailer template - ruby-on-rails

How do I reuse the same action mailer template for multiple mailer "actions"?
In ActionController, you can do
...
render :action => 'another_action'
I'd imagine the same thing can be done in ActionMailer, but I couldn't seem to find the right method. If it's relevant, I'm on Rails 2.3.2.
Thanks!

You're looking for render_message, there is a good example in the API Docs Multipart Message section - pasted below.
class ApplicationMailer < ActionMailer::Base
def signup_notification(recipient)
recipients recipient.email_address_with_name
subject "New account information"
from "system#example.com"
content_type "multipart/alternative"
part :content_type => "text/html",
:body => render_message("signup-as-html", :account => recipient)
part "text/plain" do |p|
p.body = render_message("signup-as-plain", :account => recipient)
p.transfer_encoding = "base64"
end
end
end

Related

ActionMailer help, paste email from session!

Notifier.rb
class Notifier < ActionMailer::Base
def inquiry_notification(inquiry)
recipients inquiry.respondent.email
from "#{#laz.email}"
subject "Survey"
content_type "text/html"
end
end
Part of Controller.rb
...
#laz = User.find(:all)
respondents.each do |r|
inquiry = Inquiry.create(:question_id => #question.id, :respondent_id => r.id, :is_answered => 0)
Notifier.deliver_inquiry_notification(inquiry)
end
....
I need to paste into "FROM" (notifier.rb) email that user has.
For example: session[:user].email <- paste this , because i work with sessions and i have many users (admins and auditors).
Why dont you just add additional parameter to your inquiry_notification method like this:
def inquiry_notification(inquiry, from_email)
recipients inquiry.respondent.email
from from_email
subject "Survey"
content_type "text/html"
end
If you dont want to do so, you can use for example Thread.current:
in controller
Thread.current[:email] = 'test#email.com'
in Notifier
def inquiry_notification(inquiry)
recipients inquiry.respondent.email
from Thread.current[:email]
subject "Survey"
content_type "text/html"
end

Rails - ActionMailer - How to send an attachment that you create?

In rails3 w ActionMailer, I want to send a .txt file attachment. The challenge is this txt file does not exist but rather I want to create the txt file given a large block of text that I have.
Possible? Ideas? Thanks
It's described for files in the API documentation of ActionMailer::Base
class ApplicationMailer < ActionMailer::Base
def welcome(recipient)
attachments['free_book.pdf'] = File.read('path/to/file.pdf')
mail(:to => recipient, :subject => "New account information")
end
end
But that doesn't have to be a File, it can be a string too. So you could do something like (I'm also using the longer Hash-based form where you can specify your own mimetype too, you can find documentation for this in ActionMailer::Base#attachments):
class ApplicationMailer < ActionMailer::Base
def welcome(recipient)
attachments['filename.jpg'] = {:mime_type => 'application/mymimetype',
:content => some_string }
mail(:to => recipient, :subject => "New account information")
end
end
First the method to send email
class ApplicationMailer < ActionMailer::Base
def welcome(user, filename, path)
attachments[filename] = File.read(path)
mail(:to => user.email, :subject => "New account information")
end
end
Call the method with the params
UserMailer.welcome(user, filename, path).deliver

How do I attach a prawnto-rendered .pdf to an email in Rails 2.3.5?

My application creates a .pdf file when it is rendered by passing it to the URL (for example, domain.com/letter/2.pdf)
It doesn't get saved anywhere.
How can I make that actual pdf an attachment in an outbound email.
Here is my mailer:
def campaign_email(contact,email)
subject email.subject
recipients contact.email
from 'Me <me#me.com>'
sent_on Date.today
attachment = File.read("http://localhost:3000/contact_letters/#{attachment.id}.pdf")
attachment "application/pdf" do |a|
a.body = attachment
a.filename = "Othersheet.pdf"
end
end
This is the controller that creates/renders the PDF:
def create
#contact_letter = ContactLetter.new(params[:contact_letter])
#contact = Contact.find_by_id(#contact_letter.contact_id)
#letter = Letter.find_by_id(#contact_letter.letter_id)
if #contact_letter.save
flash[:notice] = "Successfully created contact letter."
#redirect_to contact_path(#contact_letter.contact_id)
redirect_to contact_letter_path(#contact_letter, :format => 'pdf')
else
render :action => 'new'
end
end
NOTE: I hardcoded localhost:3000/ how can I substitute that with a variable so that on dev it is localhost:3000 and on production is it the correct domain? Is there a way to include routing in this?)
ERROR: I get an
Invalid argument -
http://localhost:3000/contact_letters/9.pdf
Here's an example for rails 2
class ApplicationMailer < ActionMailer::Base
# attachments
def signup_notification(recipient, letter)
recipients recipient.email_address_with_name
subject "New account information"
from "system#example.com"
attachment :content_type => "image/jpeg",
:body => File.read("an-image.jpg")
attachment "application/pdf" do |a|
a.body = letter
end
end
end
in your view or wherever your calling your method:
ApplicationMailer.deliver_signup_notification(letter)
one quick an easy solution would be fetch the url content using net/http and open-uri, to get the attachment
attachments['free_book.pdf'] = open("http://#{request.host}/letter/#{id}.pdf")
eg:
def campaign_email(contact,email)
subject email.subject
recipients contact.email
attachments['free_book.pdf'] = open("http://#{request.host}/letter/#{id}.pdf")
from 'Me <me#me.com>'
sent_on Date.today
body :email => email
end
or, call the PDF generation inside your mailer controller action
I got it to work by passing the pdf object directly into the campaign_email method and then assigning an attachment.

How can I use from names in ActionMailer? [Rails 2.3.5]

I want to show a pretty name in the e-mail clients
This should do the trick!
class UserMailer < ActionMailer::Base
def welcome_email(user)
recipients user.email
# PRETTY NAMES
from "Prettiest Pony <prettypony#imaginarium.tld>"
subject "Welcome to My Awesome Site"
sent_on Time.now
body {:user => user, :url => "http://example.com/login"}
end
end
See ActionMailer basics for more information

How Do I Prevent Email Attachments from Rendering Inline using ActionMailer

I am using the following code to send an email with a pdf attachment:
class StudyMailer < ActionMailer::Base
def notify_office(study, sent_at = Time.now)
subject "Email Subject Goes Here"
recipients 'user#domain.come'
from "#{study.sender.full_name} <#{study.sender.email}>"
sent_on sent_at
body :study => study
for document in study.documents
attachment :content_type => "application/pdf", :body => File.read(document.document.path) #absolute path to .pdf document
end
end
end
When the email is sent, the attachment seems to render inline as binary code rather than as a .pdf attachment.
How do I render the .pdf as a typical attachment, rather than inline?
attachment :content_type => "application/pdf",
:content_disposition => "attachment",
:filename => File.basename(fattach),
:body => File.new(fattach,'rb').read()
Notice the content-disposition line.
I believe you have to indicate the multipart nature of the email, so add this line under the from line:
content_type "multipart/alternative"
Does your email have a template? If the email does not have a template, the attachment shows up inline even if everything else is set up correctly. Create an attachment email template.
views/notifier/attachment.html.erb
<p> Please see attachment </p>
Then in Notifier, specify to use this template.
notifier.rb
def my_email_method
...
mail(:template_name => 'attachment', :from => from_address, ...)
end

Resources