user385948
user385948

Reputation: 909

Email attachments

I want to be able to attach a file that I generate (on the fly, I dont want to save the file on my server) and send it out via email.

I have the text all done, but I'm lost on how to attach the file to the email using sendmail

Thank you

Upvotes: 9

Views: 13133

Answers (3)

BvuRVKyUVlViVIc7
BvuRVKyUVlViVIc7

Reputation: 11821

See http://railscasts.com/episodes/206-action-mailer-in-rails-3

def registration_confirmation(user)
  @user = user
  attachments["rails.png"] = File.read("#{Rails.root}/public/images/rails.png")
  mail(:to => "#{user.name} <#{user.email}>", :subject => "Registered")
end

Upvotes: 21

Phrogz
Phrogz

Reputation: 303460

For alternative consideration, I personally have had a good experience using Pony for email. It happens to have a lot of gem dependencies, but it is very nice to work with. See the README for more information.

Upvotes: 1

Peter Brown
Peter Brown

Reputation: 51717

You should be using Action Mailer that's built into Rails which supports sending emails with attachments.

For Rails 3.x - http://guides.rubyonrails.org/action_mailer_basics.html#sending-emails-with-attachments

For Rails 2.3.8+ - http://guides.rubyonrails.org/v2.3.8/action_mailer_basics.html#sending-multipart-emails-with-attachments

Upvotes: 6

Related Questions