7

我試圖添加附件到這個網站上的聯繫表格我正在做,但我不能得到行動郵件程序附加上傳的文件。我有回形針上傳文件到S3,但我不能讓它抓住文件並將其附加到消息。ActionMailer - 如何從s3添加附件

我的應用程序堆棧:Heroku的,Rails 3中,和回形針上傳到S3,繼承人是我到目前爲止有:

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's 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)  

我在做什麼錯?我仍然是一個鐵軌noob,所以我拼湊在一起。

回答

-3

如果你沒有一個S3帳戶已經去得到一個位置:

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

您需要添加到您的接觸模型:

應用程序/模型/ contact.rb

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

確保你應用程序的名字就是你的軌道在Heroku應用程序名稱。並確保您將圖片重命名爲任何您命名的圖片。

然後你需要一個配置文件在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 

請確保您獲得正確的密鑰和密碼。

在你的寶石文件請確保您有這些寶石安裝:

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

然後在你的表格,你需要一個文件域並聲明形式多:

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

而且應該做的它。 如果你沒有一個S3帳戶已經去得到一個位置:

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

您需要添加到您的接觸模型:

應用程序/模型/ contact.rb

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

請確保您的appname是您在heroku上的rails應用程序名稱。並確保您將圖片重命名爲任何您命名的圖片。

然後你需要一個配置文件在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 

請確保您獲得正確的密鑰和密碼。

在你的寶石文件請確保您有這些寶石安裝:

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

然後在你的表格,你需要一個文件域並聲明形式多:

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

然後郵寄您的聯繫與圖片。你說你正在使用rails 3?

您的聯繫模型

所以:

class Contact << ActiveRecord::Base 

    before_save :mail_user 

    def mailer_user 
     ContactMailer.contact_notification(@user).deliver 
    end 

end 

然後在你的郵件(假設你是在Rails 3中):

class ContactMailer < ActionMailer::Base 

    default :from => "[email protected]" 

    def contact_notification(@user) 
    @subscription = "test" 
    @url = "test" 
    mail(:to => "[email protected]", 
     :subject => "Test") 
    end 

end 

因此,在您郵件視圖,您需要包括和圖像標記像所以:

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

然後,你只需要在創建聯繫人後發送給你電子郵件,幷包括在tachment。

+2

可笑的長答案決不會解決OP的問題。 – 2012-11-16 04:37:23

1

快速注意:

亞馬遜現在需要

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

,而不是上面列出的S3寶石。