ActionMailer - 如何从 s3 添加附件

发布于 2024-11-01 18:44:12 字数 756 浏览 0 评论 0原文

我正在尝试将附件添加到我正在制作的网站上的联系表单中,但我无法让操作邮件程序附加上传的文件。我有回形针将文件上传到 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)      

结束

我做错了什么?我仍然是一个 Rails 菜鸟,所以我将这些拼凑在一起。

Im trying to add attachments to the contact form on this site Im making but I cant get action mailer to attach the uploaded file. I have paperclip uploading the file to S3 but I cant get it to grab the file and attach it to the message.

My app stack is: Heroku, Rails 3, and paperclip uploading to S3, heres what I have so far:

  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)      

end

What am I doing wrong? Im still a rails noob so Im piecing this together.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柳絮泡泡 2024-11-08 18:44:12

快速说明:

亚马逊现在需要

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

而不是上面列出的 s3 gem。

A quick note:

Amazon now requires

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

instead of the s3 gem listed above.

奈何桥上唱咆哮 2024-11-08 18:44:12

如果您还没有 s3 帐户,请在此处获取一个:

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

您需要将其添加到您的联系人模型中:

app/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"

确保您的 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 文件中,确保安装了这些 gem:

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 /

您需要将其添加到您的联系人模型中:

app/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"

确保您的 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 文件中,确保安装了这些 gem:

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)) %>

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

If you don't have an s3 account already go get one here:

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

You need to add this to your contact model:

app/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 you appname is your rails app name on heroku. And make sure you rename picture to whatever you have named your picture.

Then you need a config 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 get the key and secret correct.

In your gem file make sure you have these gems install :

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

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

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

And that should do it.
If you don't have an s3 account already go get one here:

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

You need to add this to your contact model:

app/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 you appname is your rails app name on heroku. And make sure you rename picture to whatever you have named your picture.

Then you need a config 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 get the key and secret correct.

In your gem file make sure you have these gems install :

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

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

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

Then mail your contact with the picture. You said that you were using 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 mailer (assuming you are on 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

So in your mailer view you need to include and image tag like so:

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

Then you just need to send you email after a contact is created and include the attachment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文