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

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

Related

Pass current_user.id into actionmailer

Im trying to pass the current_employer.id into action mailer. So i cans end an email to users that belong to the Employer.
my mailer looks like this.
class ScheduleMailer < ActionMailer::Base
default to: Proc.new { Employee.where(:employer_id => current_employer.id
).pluck(:email) },
from: 'noreply#scheduled.com'
def schedule_post_reg(employ)
mail( :subject => "Your schedule has been posted.")
end
end
I get this error
NameError (undefined local variable or method `current_employer' for #<ScheduleMailer:0x0000010cec0880>):
app/mailers/schedule_mailer.rb:3:in `block in <class:ScheduleMailer>'
app/mailers/schedule_mailer.rb:10:in `schedule_post_reg'
app/controllers/schedules_controller.rb:235:in `approve_shift'
Any suggestion on how to pass the current_employer.id not the actionmailer would be greatly appreciated.
The Mailer has no direct access to the request context. The reason is because an email is not necessarily a result of an HTTP request. If you deliver the email from the CLI, for instance, there is no HTTP request.
You need to pass the employer as argument of the mailer.
class ScheduleMailer < ActionMailer::Base
default from: 'noreply#usescheduled.com'
def schedule_post_reg(employee, employ)
mail({
:to => employee.email
:subject => "Your schedule has been posted.")
})
end
end
If you need to send the same email to different employees, given a single employer, you can either pass the employer and pluck all the emails into the "to:" field (but all the recipients will see all the other emails)
def schedule_post_reg(employer, employ)
mail({
:to => Employee.where(:employer_id => employer.id).pluck(:email)
:subject => "Your schedule has been posted.")
})
end
or use bcc, or loop all the emails and call the mailer once for every employee email to generate a different email for each employee.
In Controller write somethigng like this
#employer = Employer.find_by_id(params[:id])
ScheduleMailer.schedule_post_reg(employ, #employer).deliver
In Mailer
def schedule_post_reg(employ, employer)
employees_emails = Employee.where(:employer_id => employer.id
).map(&:email).join(",")
mail(:to => employees_emails, :subject => "Your schedule has been posted.")
end

Issue with ActionMailer Mandrill in Rails

I have a cab booking platform created in Rails. I am using the Mandrill smtp settings in production to send booking confirmation mails to users of my platform. Then I generated a mailer called user_mailer with the following code:
class UserMailer < ActionMailer::Base
default :from => "my_company_email"
def booking_confirmation(user)
#user = user
mail(:to => user.email, :subject => "Booking Confirmation")
end
end
Then I created booking_confirmation.html.erb page in user_mailer views with some generic content inside. Finally I called the user_mailer in one of my controllers as follows:
UserMailer.booking_confirmation(current_user).deliver
My problem is that when I want to include more details (such as Travel date, Travel time, etc.) within the mail delivered to my customer. I am trying this within my booking_confirmation.html.erb page: <%= #user.bookings.last.date %> to display the Travel Date to the customer. But this doesn't get displayed. Why is it so?
I would pass in the booking like
UserMailer.booking_confirmation(current_user, booking.id).deliver
then in the class UserMailer I would do
class UserMailer < ActionMailer::Base
default :from => "my_company_email"
def booking_confirmation(user, booking)
#user = user
#booking = Booking.find(booking)
mail(:to => user.email, :subject => "Booking Confirmation")
end
end
now on your erb you should have #booking.date to use
Its not a mandrill issue.something wrong with application code.
Check whether your following code has the date value present for last bookings.
#user.bookings.last.date

image attachment in mail with rails3

Here i need to attach one image with mail, the passing image like this
**imageurl** = "https://chart.googleapis.com/chart?chs=200x200&cht=qr&chl=%22hai%22&choe=UTF-8"
class UserMailer < ActionMailer::Base
default :from => "mail#example.com"
def welcome_email(**imageurl**,bname,mailid)
**attachments['image.png'] = File.read(imageURL)**
mail(:to => mailid,
:subject => "Code for "+bname+"",
:body => "code for bname" )
end
end
end
here i got some attachment error. Have any changes in this attachment?
thanks
i think you have a URL i.e a string which File.read cannot read.
require 'open-uri'
class UserMailer < ActionMailer::Base
def welcome_email(image_url,bname,mailid)
attachments['image.png'] = open(URI.parse(image_url))
...
end
end
The above should do the trick i think.
require 'open-uri'
class UserMailer < ActionMailer::Base
def welcome_email(image_url,bname,mailid)
mail.attachments[image.png] = { :mime_type => type*, :content => open(URI.parse(image_url)}
...
end
end
where type* is the type of attached file in your case it will be('image/png')

Devise email confirmation not working on Heroku

When I click on the link in my confirmation email that devise sends, it seems to go to a path that is not recognized by my application.
The url looks something like this:
http://glowing-flower-855.heroku.com/users/confirmation?confirmation_token=lIUuOINyxfTW3TBPPI
which looks correct, but it seems to go to my 500.html file.
It has something to do with this code in my user model that overrides Devise's confirm! method:
def confirm!
UserMailer.welcome_message(self).deliver
super
end
According to my logs, this is the error:
2011-06-10T03:48:11+00:00 app[web.1]: ArgumentError (A sender (Return-Path, Sender or From) required to send a message):
2011-06-10T03:48:11+00:00 app[web.1]: app/models/user.rb:52:in `confirm!'
which points to this line: UserMailer.welcome_message(self).deliver
Here's my user mailer class:
class UserMailer < ActionMailer::Base
def welcome_message(user)
#user = user
mail(:to => user.email, :subject => "Welcome to DreamStill")
end
end
You are missing the "from:" value, it's a must for SMTP handling:
class UserMailer < ActionMailer::Base
# Option 1
#default_from "bob#dylan.com"
def welcome_message(user)
#user = user
mail(
# Option 2
:from => "paul#mccarthy.com",
:to => user.email,
:subject => "Welcome to DreamStill"
)
end
end

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

Resources