如何附加文档到电子邮件ActiveStorage和Cloudinary?

问题描述 投票:0回答:1

我正在使用ActiveStorage和Cloudinary。

gem 'cloudinary', '~> 1.14.0'
gem 'activestorage-cloudinary-service', '~> 0.2.3'

我的邮件中有以下内容。

if @job_request.work_files.attached?
  @job_request.work_files.each do |file|
    attachments[file.blob[:filename]] = { mime_type: file.blob.content_type, content: file.download }
  end
end

这在Heroku上引起了一个错误。

SendGridActionMailer::DeliveryMethod::SendgridDeliveryError

Sendgrid delivery failed with 400 The attachment content is required.

用户可以提交带有文件的工作请求(使用ActiveStorage和Cloudinary,Cloudinary允许提交图片、文档、pdf和电子表格)。我需要将这些文件附加到邮件中并发送,有什么办法可以解决?

ruby-on-rails ruby cloudinary rails-activestorage
1个回答
0
投票

我简单地使用了 open-uri 库,打开远程文件并将其附加到

require 'open-uri'

class MyMailer << ApplicationMailer
  def new_job(job_request)
     @job_request = job_request
     if @job_request.work_files.attached?
       @job_request.work_files.each do |file|
         temp_file = open(file.service_url).read
         attachments[file.blob[:filename]] = { mime_type: file.blob.content_type, content: temp_file }
       end
     end

    mail to: @job_request.email
  end
end
© www.soinside.com 2019 - 2024. All rights reserved.