发送带附件的电子邮件操作 Mailer Ruby on Rails

huangapple go评论83阅读模式
英文:

Send email with attachments action Mailer Ruby on Rails

问题

根据<https://edgeguides.rubyonrails.org/action_mailer_basics.html>,我尝试配置我的应用程序以发送带附件的电子邮件,但出现了错误NameError (undefined local variable or method attachment for #&lt;InvoiceMailer:0x00007fde0d7bfe88&gt; Did you mean? attachments)

以下是我在我的控制器中的内容:

array_docs.each do |doc| 
    decoded_doc = URI.decode(doc)               

    tmp_file = Tempfile.new(['bill', '.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
       attachment = tmp_file.path
       InvoiceMailer.send_invoice(attachment).deliver
    end
end

以下是我的Invoice Mailer:

class InvoiceMailer < ApplicationMailer

default :from => 'Test <no-reply@test.co>'
 
  def send_invoice(attachment)
    attachments['test-bill'] = File.read(attachment)
    mail(to: "steven@test.com",
    subject: "check if it works")
  end
end

以及我的视图send_invoice.html.erb

<!DOCTYPE html>

<html>
    <head>
        <meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
    </head>
    <body>
        <h1>Test to check if it works </h1>
        <p>Hello world</p>
    </body>
</html>

添加到我的development.rb并重新启动服务器:

config.action_mailer.delivery_method = :sendmail
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true

如何将变量从控制器传递到邮件发送文件中?

英文:

Following <https://edgeguides.rubyonrails.org/action_mailer_basics.html>, I try to configure my app to send email with attachments but I have the error NameError (undefined local variable or method attachment for #&lt;InvoiceMailer:0x00007fde0d7bfe88&gt;
Did you mean? attachments)

Here is what I have in my controller :

        array_docs.each do |doc| 
            decoded_doc = URI.decode(doc)               
            
            tmp_file = Tempfile.new([&#39;bill&#39;, &#39;.pdf&#39;])
            tmp_file.binmode
            open(decoded_doc, &quot;Authorization&quot; =&gt; bearer_token) do |url_file|
               tmp_file.write(url_file.read)
               tmp_file.rewind
               tmp_file.read
               attachment = tmp_file.path
               InvoiceMailer.send_invoice.deliver
            end
        end

Here is my Invoice Mailer :

class InvoiceMailer &lt; ApplicationMailer

default :from =&gt; &#39;Test &lt;no-reply@test.co&gt;&#39;
 
  def send_invoice
    attachments[&#39;test-bill&#39;] = File.read(attachment)
    mail(to: &quot;steven@test.com&quot;,
    subject: &quot;check if it works&quot;)
  end
end

And my view send_invoice.html.erb
<!DOCTYPE html>

&lt;html&gt;
&#160;&#160;&lt;head&gt;
&#160;&#160;&#160;&#160;&lt;meta content=&quot;text/html; charset=UTF-8&quot; http-equiv=&quot;Content-Type&quot; /&gt;
&#160;&#160;&lt;/head&gt;
&#160;&#160;&lt;body&gt;
&#160;&#160;&#160;&#160;&lt;h1&gt;Test to check if it works &lt;/h1&gt;
    &lt;p&gt;Hello world&lt;/p&gt;
&#160;&#160;&lt;/body&gt;
&lt;/html&gt;

Added to my development.rb and restart my server :

config.action_mailer.delivery_method = :sendmail
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true

How to pass the variable from the controller to the mailer file ?

答案1

得分: 0

# controller
InvoiceMailer.send_invoice(attachment).deliver

# mailer
def send_invoice(attachment)
  attachments['test-bill'] = File.read(attachment)
  ...
end
英文:
# controller
InvoiceMailer.send_invoice(attachment).deliver

# mailer
def send_invoice(attachment)
  attachments[&#39;test-bill&#39;] = File.read(attachment)
  ...
end

</details>



# 答案2
**得分**: 0

这对我有效:

在控制器中:

```ruby
attached_file = tmp_file.read

attachment = Array.new
attachment = {filename: "nameoffile.something", file: attached_file}
InvoiceMailer.send_invoice(attachment).deliver

邮件类中,您可以通过仅发送attached_file来简化附件,文件名只是灵活的,如果您想要使用其他名称重命名它。

def send_invoice(attachment)
   if attachment.present? 
     attachments[attachment[:filename]] = {content: attachment[:file]}
   end
  ...
end
英文:

this works for me:

in controller

attached_file = tmp_file.read

attachment&#160;= Array.new
attachment = {filename: &quot;nameoffile.something&quot;, file: attached_file}
InvoiceMailer.send_invoice(attachment).deliver

the mailer class, you can simplify the attachment via sending only the attached_file, the filename is just be flexible if you want to rename it with other names.

def send_invoice(attachment)
   if attachment.present? 
     attachments[attachment[:filename]] = {content: attachment[:file]}
   end
  ...
end

huangapple
  • 本文由 发表于 2020年1月3日 18:05:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/59576575.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定