ActionMailer - How to add an attachment from s3

I am trying to add attachments to the contact form on this site. I create, but I can’t get an email action program to attach the downloaded file. I have a clip uploading a file to S3, but I cannot get it to capture the file and attach it to the message.

My application stack: Heroku, Rails 3, and loading a clip on S3, this is what I still have:

def contact_notification(sender) @sender = sender if attachments.count > 0 # Parse the S3 URL into its constituent parts uri = URI.parse @sender.photo.url(:original).authenticated_url # Use Ruby built-in Net::HTTP to read the attachment into memory response = Net::HTTP.start(uri.host, uri.port) { |http| http.get uri.path } # Attach it to your outgoing ActionMailer email attachments[@sender.attachment_file_name] = response.body end mail(:to => xxx) 

end

What am I doing wrong? I'm still noob rails, so I put it together.

+7
source share
2 answers

Quick note:

Amazon now requires

 gem 'aws-sdk', :require => "aws-sdk" 

instead of the s3 gem indicated above.

+1
source

If you do not have an s3 account, go here:

http://aws.amazon.com/s3/

You need to add this to your contact model:

application / models / contact.rb

  has_attached_file :picture, :styles => {:large => "275x450>"}, :storage => :s3, :s3_credentials => "#{RAILS_ROOT}/config/s3.yml", :path => "appname/:attachment/:style/:id.:extension" 

Make sure the application name is your rails application name on heroku. And make sure you rename the image according to what you called your image.

Then you need the configuration file in config/s3.yml .

 development: bucket: bucked_name access_key_id: key secret_access_key: secret production: bucket: bucked_name access_key_id: key secret_access_key: secret 

Make sure you understand the key and the secret correctly.

In your gem file, make sure you install these stones:

 gem "aws-s3", :require => "aws/s3" gem "paperclip" 

Then in your form you will need a file field and declare a multipart form:

 <% form_for(@contact, :html => {:multipart => true}) do |f| %> <p><%= f.file_field :picture %></p> <% end %> 

And that should do it. If you do not have an s3 account, go here:

http://aws.amazon.com/s3/

You need to add this to your contact model:

application / models / contact.rb

  has_attached_file :picture, :styles => {:large => "275x450>"}, :storage => :s3, :s3_credentials => "#{RAILS_ROOT}/config/s3.yml", :path => "appname/:attachment/:style/:id.:extension" 

Make sure the application name is your rails application name on heroku. And make sure you rename the image according to what you called your image.

Then you need the configuration file in config/s3.yml .

 development: bucket: bucked_name access_key_id: key secret_access_key: secret production: bucket: bucked_name access_key_id: key secret_access_key: secret 

Make sure you understand the key and the secret correctly.

In your gem file, make sure you install these stones:

 gem "aws-s3", :require => "aws/s3" gem "paperclip" 

Then in your form you will need a file field and declare a multipart form:

 <% form_for(@contact, :html => {:multipart => true}) do |f| %> <p><%= f.file_field :picture %></p> <% end %> 

Then write your contact with the image. Did you say you use rails 3?

So, in your contact model:

 class Contact << ActiveRecord::Base before_save :mail_user def mailer_user ContactMailer.contact_notification(@user).deliver end end 

Then in your mail program (assuming you are on Rails 3):

 class ContactMailer < ActionMailer::Base default :from => " sam@codeglot.com " def contact_notification(@user) @subscription = "test" @url = "test" mail(:to => " test@test.com ", :subject => "Test") end end 

So, in your postal view, you need to include the image tag like this:

 <%= image_tag(@contact.picture(:small)) %> 

Then you just need to send an email after creating the contact and enable the attachment.

-3
source

All Articles