首页 > 解决方案 > 发送带有附件名称操作 Mailer Ruby on Rails 的电子邮件

问题描述

我有一些来自哈希的参数,我想通过电子邮件发送。对于我的附件文件(发票)的名称,我想要我的商店名称和有关此发票的信息。当我运行我的测试时,我收到 9 封电子邮件,而不是 3 封,因为我的 .each 但我不知道该怎么做,有人可以帮助我只收到 3 封带有名称和 shop 变量的文档。

这是我的控制器:

if(params.dig('connection')!= nil)

   #GET ALL MY DOCUMENTS 
documents =  params.dig('connection', 'subscriptions').each_with_object([]) { |x, arr| x['documents'].each { |d| arr << d['url'] } }.to_s
    #GET THE NAME OF THE SHOP 
shop = params.dig('connection','connector', 'name').to_s
    #GET ALL THE INFORMATIONS 
names =  params.dig('connection', 'subscriptions').each_with_object([]) { |x, arr| x['documents'].each { |d| arr << d['name'] } }.to_s

    full_name = JSON.parse(names)
    full_name.each do |name| puts "CHAQUE NAME " + name  

        array_docs =  JSON.parse(documents)
        bearer_token = request.headers["Authorization"]

        array_docs.each do |doc| 
            decoded_doc = URI.parse(URI.escape(doc)).to_s                
            tmp_file = Tempfile.new(['invoices', '.pdf'])
            tmp_file.binmode
            open(decoded_doc, "Authorization" => bearer_token) do |url_file|
            tmp_file.write(url_file.read)
            tmp_file.rewind
            tmp_file.read
                attached_file = tmp_file.path

                attachment = Array.new
                attachment = {filename: shop+".pdf", file: attached_file}
              InvoiceMailer.send_invoice(attachment).deliver
            end
        end
    end
end

我可以做类似的事情:

documents =  params.dig('connection', 'subscriptions').each_with_object([]) { |x, arr| x['documents'].each { |d| arr << d['url'], |n| arr << n['name'] } }.to_s

标签: ruby-on-railsrubyemailactionmailer

解决方案


推荐阅读