【问题标题】:how attach image to mail?如何将图像附加到邮件?
【发布时间】:2015-10-07 17:17:02
【问题描述】:

请帮忙解决问题。

我安装 gem 'paperclip' 并将字段添加到表 'sends':

create_table "sends", force: :cascade do |t|
  t.text     "message"
  t.string   "subject"
  t.string   "image_file_name"
  t.string   "image_content_type"
  t.integer  "image_file_size"
  t.datetime "image_updated_at"
end

我需要发送带有附件的邮件。 app/mailers/send_mailer.rb:

class SendMailer < ApplicationMailer
  default from: 'no-reply@kalinin.ru'

  def sends_send(send, email)
    @send = send
    @email = email
    @image_name = send.image_file_name     
    @image_path = send.image.url(:medium)     

    p '================================='
    p @image_name
    p @image_path

    mail.attachments[@image_name] = File.read(@image_path)
    mail(to: email.email, subject: send.subject)
  end  
end

但运行发送发送后,控制台显示如下:

"================================="
"296_197.png"
"/system/sends/images/000/000/012/medium/296_197.png?1444228995"
  Rendered send_mailer/sends_send.html.erb within layouts/mailer (0.1ms)

SendMailer#sends_send: processed outbound mail in 4.7ms
Completed 500 Internal Server Error in 9ms (ActiveRecord: 0.3ms)

浏览器显示如下:

SendsController#send_up 中的 Errno::ENOENT 没有这样的文件或目录 - /system/sends/images/000/000/012/medium/296_197.png?1444228995

请帮助将图片附加到邮件中。

开发.rb:

config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
config.action_mailer.delivery_method = :sendmail

我还为目录“公共”777 递归设置权限并尝试:

class SendMailer < ApplicationMailer
  default from: 'no-reply@kalinin.ru'

  def sends_send(send, email)
    @send = send
    @email = email
    @image_name = send.image_file_name     
    #@image_path = send.image.url(:medium)  
    @image_path = '/public' + send.image.url(:medium).split('?').first   

    p '================================='
    p @image_name
    p @image_path

    mail.attachments[@image_name] = File.read(@image_path)
    mail(to: email.email, subject: send.subject)
  end  
end

但浏览器显示:

没有这样的文件或目录 - /public/system/sends/images/000/000/012/medium/296_197.png

但是这个文件存在于硬盘上

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 paperclip sendmail


    【解决方案1】:

    你真的有一个名为/system/sends/images/000/000/012/medium/296_197.png?1444228995 的文件吗?看来您需要删除.png 之后的所有内容,即问号和其他数字。试试这个:

    @image_path = send.image.url(:medium).split('?').first
    

    【讨论】:

    • 是的,我真的有这个文件在硬盘上。 split('?').first 不是解决问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-06
    • 2016-12-29
    • 2016-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-01
    相关资源
    最近更新 更多