Rails - ActionMailer sometimes shows attachments before the email content? - ruby-on-rails

How can I make it so ActionMailer always shows attachments at the bottom of the message:
HTML, TXT, Attachments....
Problem is the attachment here is a text file:
----==_mimepart_4d8f976d6359a_4f0d15a519e35138763f4
Date: Sun, 27 Mar 2011 13:00:45 -0700
Mime-Version: 1.0
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename=x00_129999.olk14message
Content-ID: <4d8f976d49c72_4f0d15a519e351387611f#railgun64.53331.mail>
Thanks

I know there is already an accepted answer, but switching the order of attachments[] and mail() didn't solve it for me. What is different about my situation is that I was trying to attach a text file attachment (.txt)
What works for me is setting the content_type and parts_order defaults for the mailer.
MyMailer < ActionMailer::Base
default :from => "Awesome App <support#example.com>",
:content_type => 'multipart/alternative',
:parts_order => [ "text/html", "text/enriched", "text/plain", "application/pdf" ]
def pdf_email(email, subject, pdfname, pdfpath)
attachments[pdfname] = File.read(pdfpath)
mail(:to => email, :subject => subject)
end
def txt_email(email, subject, filename, filebody)
attachments[filename] = filebody
mail(:to => email, :subject => subject)
end
end
If you are trying to send an email in Rails 3 with a plain text file (.txt), trying adding :content_type and parts_order to your defaults so that the text file does not appear above the message in your email.

I had the same problem, and in my case the solution was to swap the attachment and mail lines. First attach, then call mail.
Rails 3
WRONG
def pdf_email(email, subject, pdfname, pdfpath)
mail(:to => email, :subject => subject)
attachments[pdfname] = File.read(pdfpath)
end
GOOD
def pdf_email(email, subject, pdfname, pdfpath)
attachments[pdfname] = File.read(pdfpath)
mail(:to => email, :subject => subject)
end

this is rail 2.3 code (might be slightly different in rails3)
just move you text part before attachment
recipients to#domain.com
from me#domain.com
subject "some subject"
content_type "multipart/mixed"
part "text/plain" do |p|
p.body = render_message 'my_message' #this is template file
end
attachment "application/octet-stream" do |a|
a.body = File.read("some_file.jpg")
a.filename = 'name.jpg'
end

Related

Rails actionmailer: attachments from database

I'm using actionmailer to send mails in rails. I want to attach multiple attachments:
def prepare_attachments(languages)
attachments = {}
languages.each do |language|
next unless language.document
attachments[language.document.filename] = language.document.read
end
return attachments
end
def distribution_email(recipient, languages)
attachments = self.prepare_attachments(languages)
mail(
:to => recipient,
:subject => 'Test'
)
end
The delivered mail doesn't contain any attachment.
This is working:
def distribution_email(recipient, languages)
attachments['test.pdf'] = File.read("/tmp/test.pdf")
mail(
:to => recipient,
:subject => 'Welcome to My Awesome Site'
)
end
What am i doing wrong?
I fount the solution, one must not override attachment:
def prepare_attachments(languages)
attachments = {}
languages.each do |language|
next unless language.document
attachments[language.document.filename] = language.document.read
end
return attachments
end
def distribution_email(recipient, languages)
self.prepare_attachments(languages).each do |filename, content|
attachments[filename] = content
end
mail(
:to => recipient,
:subject => 'Test'
)
end

Rails: change default sender in action mailer

I am sending email using action mailer in my rails app. But it allows only one default sender. This is my UserMailer class:
class UserMailer < ActionMailer::Base
default :from => "example#example.com"
def welcome_email(user, order)
#user = user
#order = order
mail(:to => user.email, :subject => "Your Order")
end
def signup_email(user)
#user = user
mail(:to => user.email, :subject => "Thank you.")
end
def invite_confirm(curuser,usemail,post)
#greeting = "Hi"
#user = curuser
#post = post
mail(:to => user.email, :subject => "Hello")
end
end
I tried this:
class UserMailer < ActionMailer::Base
def welcome_email(user, order)
#user = user
#order = order
mail(:to => user.email, :subject => "Your Order", :from => "abc#xyz.com")
end
def signup_email(user)
#user = user
mail(:to => user.email, :subject => "Thank you.", :from => "qwe#asd.com")
end
def invite_confirm(curuser,usemail,post)
#greeting = "Hi"
#user = curuser
#post = post
mail(:to => user.email, :subject => "Hello", :from => "zyx#asd.com")
end
end
But still it is sending email from "example#example.com"
Is there any way to change sender for each method written in UserMailer class? Am i supposed to change anywhere else?
In config/environments/development.rb and config/environments/production.rb i have this:
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => "587",
:domain => "gmail.com",
:authentication => "plain",
:user_name => "example#example.com",
:password => "example",
:enable_starttls_auto => true
}
I guess, i should not change anything here.
You can pass it as a parameter to the mail method:
def new_mail
mail from: "example#example.com", to: "user#example.com"
end
I think you want to send mail with three different emails of the for-each action. Because you use gmail, you need Sending mail from a different address.
No single vendor is optimal for all three types of email; you likely
will use several vendors.
For “company email,” that is, sending individual email to customers or
business associates, you’ll probably use Gmail or Google Apps for
Business. For a single address, you can set up a single Gmail account
to receive and send email from a different address. More likely,
you’ll want several email addresses for your company mail. For that,
use Google Apps for Business.
Send Email with Rails
I found that, this can't be done using smtp. Need to use amazon SES which allows multi sender support.
Here's what i use, it allows to make a "title" different.
class UserMailer < ActionMailer::Base
default :from => '"example" <example#domain.com>'
def send_signup_email(user)
#user = user
mail(to: #user.email, subject: 'example')
end
end

Ruby Contact Mailer not working properly

I have a simple script for my contact_mailer.rb which has a simple form as the front end with one dropdown to select purpose and has a switch-case block to choose the email ID accordingly. Now, somehow mail is always being sent to the first one i.e. when 'localization'.
Only this gets fired whatever be selected. Please explain why this may be happening. Am pasting code snippet below for reference:
class ContactUsMailer < ActionMailer::Base
default :from => "bot#mydomain.com"
def contact_us_email(name, message, purpose, email)
#name = name
#message = message
#purpose = purpose
#email = email
content_type "text/html"
case #purpose
when 'localization'
mail(:to => 'me#mydomain.com', :subject => purpose)
when 'marketing'
mail(:to => 'me#mydomain.com', :cc => 'me#mydomain.com, me#mydomain.com', :subject => purpose)
when 'network'
mail(:to => 'me.agm#gmail.com', :subject => purpose)
when 'recruitment'
mail(:to => 'me#mydomain.com', :cc => 'me#mydomain.com', :subject => purpose)
when 'general'
mail(:to => 'me#mydomain.com', :subject => purpose)
else
mail(:to => 'me#mydomain.com', :subject => purpose)
end
end
end
Thanks and Regards

Converting old Mailer to Rails 3 (multipart/mixed)

I'm having some difficulties converting this old mailer api to rails 3:
content_type "multipart/mixed"
part :content_type => "multipart/alternative" do |alt|
alt.part "text/plain" do |p|
p.body = render_message("summary_report.text.plain.erb",
:message =>
message.gsub(/<.br.>/,"\n"),
:campaign=>campaign,
:aggregate=>aggregate,
:promo_messages=>campaign.participating_promo_msgs)
end
alt.part "text/html" do |p|
p.body = render_message("summary_report.text.html.erb",
:message => message,
:campaign=>campaign,
:aggregate=>aggregate,:promo_messages=>campaign.participating_promo_msgs)
end
end
if bounce_path
attachment :content_type => "text/csv",
:body=> File.read(bounce_path),
:filename => "rmo_bounced_emails.csv"
end
attachment :content_type => "application/pdf",
:body => File.read(report_path),
:filename=>"rmo_report.pdf"
In particular I don't understand how to differentiate the different multipart options. Any idea?
"Action Mailer will automatically send multipart emails if you have different templates for the same action."
For example having these files would give you text and html versions:
summary_report.text.erb
summary_report.html.erb
Check the Rails Guides for details:
http://guides.rubyonrails.org/action_mailer_basics.html#sending-multipart-emails
http://guides.rubyonrails.org/action_mailer_basics.html#sending-emails-with-attachments

actionmailer question on rails 3

I'm trying to convert this code
def password_reset_instructions(user)
subject "Registered"
recipients user.email
body :edit_password_reset_url => edit_password_reset_url(user.perishable_token)
end
to this code
def password_reset_instructions(user)
#user = user
mail(:to => user.email, :subject => "Registered")
end
My problem is i don't know where to put the code below.
:edit_password_reset_url => edit_password_reset_url(user.perishable_token)"
I am using authlogic on rails 3.
In Rails 3, Mailers work just like controllers. You can use the instance variable of the user in the accompanying view.
Not tested, but try this:
def password_reset_instructions(user)
#edit_password_reset_url = edit_password_reset_path(user.perishable_token)
mail(
:subject => "Password Reset Instructions",
:recipients => user.email
)
end

Resources