2015-10-07 119 views
0

请帮忙解决问题。如何将图像附加到邮件?

我安装宝石“回形针”和字段添加到表“发送”:

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 

我需要带附件发送邮件。 应用程序/邮寄/ send_mailer.rb:

class SendMailer < ApplicationMailer 
    default from: '[email protected]' 

    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 

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

"=================================" 
"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#

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

请帮助将图像附加到邮件。

development.rb:

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

我设立许可目录 '公开' 777递归和尝试:

class SendMailer < ApplicationMailer 
    default from: '[email protected]' 

    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

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

回答

0

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

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

是的,我真的有硬盘上的这个文件。 split('?')。首先是没有解决问题 – stackow1

相关问题