ActionMailer - 如何从s3添加附件

rug*_*ert 7 ruby-on-rails actionmailer heroku amazon-s3 ruby-on-rails-3

我试图添加附件到这个网站上的联系表格我正在制作,但我不能让动作邮件附加上传的文件.我有回形针将文件上传到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)      
Run Code Online (Sandbox Code Playgroud)

结束

我究竟做错了什么?我仍然是一个铁杆菜鸟所以我拼凑这个.

the*_*gah -6

如果您还没有 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"
Run Code Online (Sandbox Code Playgroud)

确保您的 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
Run Code Online (Sandbox Code Playgroud)

确保您获得的密钥和秘密正确。

在您的 gem 文件中,确保安装了这些 gem:

gem "aws-s3", :require => "aws/s3"
gem "paperclip"
Run Code Online (Sandbox Code Playgroud)

然后在您的表单中,您需要一个文件字段并将表单声明为多部分:

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

那应该可以了。如果您还没有 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"
Run Code Online (Sandbox Code Playgroud)

确保您的 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
Run Code Online (Sandbox Code Playgroud)

确保您获得的密钥和秘密正确。

在您的 gem 文件中,确保安装了这些 gem:

gem "aws-s3", :require => "aws/s3"
gem "paperclip"
Run Code Online (Sandbox Code Playgroud)

然后在您的表单中,您需要一个文件字段并将表单声明为多部分:

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

然后将照片邮寄给您的联系人。你说你用的是rails 3?

所以在你的联系模型中:

class Contact << ActiveRecord::Base

    before_save :mail_user

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

end
Run Code Online (Sandbox Code Playgroud)

然后在你的邮件程序中(假设你使用的是 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
Run Code Online (Sandbox Code Playgroud)

因此,在您的邮件视图中,您需要包含图像标签,如下所示:

<%= image_tag(@contact.picture(:small)) %>
Run Code Online (Sandbox Code Playgroud)

然后,您只需在创建联系人后向您发送电子邮件并包含附件即可。

  • 可笑的长答案根本无法解决OP的问题。 (5认同)