Rails ActionMailer中附件的问题

时间:2016-08-09 09:48:53

标签: ruby-on-rails ruby email actionmailer

我使用以下代码(如Rails指南中所示):

def email(file, report_filename, report_type)
    attachments['image.jpg'] = File.read('/Users/admin/Desktop/Codica/fame_and_partners/website/maxresdefault.jpg')
    mail(
      to: 'some_email@gmail.com',
      subject: "Daily orders report #{Date.today.to_date} #{report_type}",
      layout: false
    )
  end

当我收到电子邮件时,我有一个名为noname的文件,其中包含以下内容而非我的图片:

--
Content-Type: image/jpeg;
 charset=UTF-8;
 filename=image.jpg
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
 filename=image.jpg
Content-ID: <57a9a5c241e82_b9a3fd05c463be4522b9@MacBook-Pro-citrus.local.mail>

/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAUDBAoKCggICgoICAgICAgICAgI
CAgICAgICAgHCAgICAgIChANCAgOCQcHDSENGhERHx8fBxcYJBgSJBASExIB
BQUFCAcIDwkJDxQUDxQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQU
FBQUFBQUFBQUFBQUFBQUFBQUFP/AABEIBDgHgAMBIgACEQEDEQH/xAAdAAAB
BQEBAQEAAAAAAAAAAAADAAECBAUGBwgJ/8QAWxAAAgECAwUFAwcHCAYIBQEJ
AQIAAxEEEiEFMUFRYQYTInGBMpGhBxRCUrHR8AgjYnKSweEzQ1NUgrLS8RUk
RHSTohcYNFVjc4PCFiU1ZKOzJjZFhJS04vLT/8QAGwEAAwEBAQEBAAAAAAAA
AAAAAAECAwQFBgf/xAA/EQACAgEEAAMGBAQEBQMFAQAAAQIRAwQSITETQVEF
FCJhcZEygaHwUrHB0RUzQuEjYnKC8QZToiQ0krLS4v/aAAwDAQACEQMRAD8A
0agt1ipDiZFqgMGak+nPmjRo8zu5QveX00tM3vQJOjWF4qA0VYcrx6tXlYQA
...
--

我使用的是Rails 3.2.2.22。如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

attachments.inline['photo.png'] = File.read('path/to/photo.png')

<%= image_tag attachments['photo.png'].url -%>