ActionMailer - 如何从 s3 添加附件
我正在尝试将附件添加到我正在制作的网站上的联系表单中,但我无法让操作邮件程序附加上传的文件。我有回形针将文件上传到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
快速说明:
亚马逊现在需要
而不是上面列出的 s3 gem。
A quick note:
Amazon now requires
instead of the s3 gem listed above.
如果您还没有 s3 帐户,请在此处获取一个:
http://aws.amazon.com/s3 /
您需要将其添加到您的联系人模型中:
app/models/contact.rb
确保您的 appname 是您在 Heroku 上的 Rails 应用程序名称。并确保将图片重命名为您为图片命名的名称。
然后你需要一个位于 config/s3.yml 中的配置文件。
确保您获得的密钥和秘密正确。
在您的 gem 文件中,确保安装了这些 gem:
然后在您的表单中,您需要一个文件字段并将表单声明为多部分:
这样就可以了。
如果您还没有 s3 帐户,请在此处获取一个:
http://aws.amazon.com/s3 /
您需要将其添加到您的联系人模型中:
app/models/contact.rb
确保您的 appname 是您在 Heroku 上的 Rails 应用程序名称。并确保将图片重命名为您为图片命名的名称。
然后你需要一个位于 config/s3.yml 中的配置文件。
确保您获得的密钥和秘密正确。
在您的 gem 文件中,确保安装了这些 gem:
然后在您的表单中,您需要一个文件字段并将表单声明为多部分:
然后将图片邮寄给您的联系人。你说你用的是rails 3?
因此,在您的联系人模型中:
然后在您的邮件程序中(假设您使用 Rails 3):
因此,在您的邮件程序视图中,您需要包含图像标签,如下所示:
然后,您只需在创建联系人后向您发送电子邮件并包含依恋。
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
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
.Make sure you get the key and secret correct.
In your gem file make sure you have these gems install :
Then in your form you need a file field and declare the form a multipart:
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
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
.Make sure you get the key and secret correct.
In your gem file make sure you have these gems install :
Then in your form you need a file field and declare the form a multipart:
Then mail your contact with the picture. You said that you were using rails 3?
So in your contact model:
Then in your mailer (assuming you are on Rails 3):
So in your mailer view you need to include and image tag like so:
Then you just need to send you email after a contact is created and include the attachment.