Email attachments

12,663

Solution 1

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

Solution 2

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

Solution 3

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.

Share:
12,663

Related videos on Youtube

user385948
Author by

user385948

Updated on May 14, 2022

Comments

  • user385948
    user385948 18 minutes

    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

  • Deepak Lamichhane
    Deepak Lamichhane over 7 years
    But if you are sending diffrent file format like csv, xls etc., then you should also mention the body part otherwise the attachment is not send properly in the mail
  • BvuRVKyUVlViVIc7
    BvuRVKyUVlViVIc7 over 7 years
    it would be nice if you accept my answer if it helped you