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

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

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)

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

  1. array_docs.each do |doc|
  2. decoded_doc = URI.decode(doc)
  3. tmp_file = Tempfile.new(['bill', '.pdf'])
  4. tmp_file.binmode
  5. open(decoded_doc, "Authorization" => bearer_token) do |url_file|
  6. tmp_file.write(url_file.read)
  7. tmp_file.rewind
  8. tmp_file.read
  9. attachment = tmp_file.path
  10. InvoiceMailer.send_invoice(attachment).deliver
  11. end
  12. end

以下是我的Invoice Mailer:

  1. class InvoiceMailer < ApplicationMailer
  2. default :from => 'Test <no-reply@test.co>'
  3. def send_invoice(attachment)
  4. attachments['test-bill'] = File.read(attachment)
  5. mail(to: "steven@test.com",
  6. subject: "check if it works")
  7. end
  8. end

以及我的视图send_invoice.html.erb

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
  5. </head>
  6. <body>
  7. <h1>Test to check if it works </h1>
  8. <p>Hello world</p>
  9. </body>
  10. </html>

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

  1. config.action_mailer.delivery_method = :sendmail
  2. config.action_mailer.perform_deliveries = true
  3. 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 :

  1. array_docs.each do |doc|
  2. decoded_doc = URI.decode(doc)
  3. tmp_file = Tempfile.new([&#39;bill&#39;, &#39;.pdf&#39;])
  4. tmp_file.binmode
  5. open(decoded_doc, &quot;Authorization&quot; =&gt; bearer_token) do |url_file|
  6. tmp_file.write(url_file.read)
  7. tmp_file.rewind
  8. tmp_file.read
  9. attachment = tmp_file.path
  10. InvoiceMailer.send_invoice.deliver
  11. end
  12. end

Here is my Invoice Mailer :

  1. class InvoiceMailer &lt; ApplicationMailer
  2. default :from =&gt; &#39;Test &lt;no-reply@test.co&gt;&#39;
  3. def send_invoice
  4. attachments[&#39;test-bill&#39;] = File.read(attachment)
  5. mail(to: &quot;steven@test.com&quot;,
  6. subject: &quot;check if it works&quot;)
  7. end
  8. end

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

  1. &lt;html&gt;
  2. &#160;&#160;&lt;head&gt;
  3. &#160;&#160;&#160;&#160;&lt;meta content=&quot;text/html; charset=UTF-8&quot; http-equiv=&quot;Content-Type&quot; /&gt;
  4. &#160;&#160;&lt;/head&gt;
  5. &#160;&#160;&lt;body&gt;
  6. &#160;&#160;&#160;&#160;&lt;h1&gt;Test to check if it works &lt;/h1&gt;
  7. &lt;p&gt;Hello world&lt;/p&gt;
  8. &#160;&#160;&lt;/body&gt;
  9. &lt;/html&gt;

Added to my development.rb and restart my server :

  1. config.action_mailer.delivery_method = :sendmail
  2. config.action_mailer.perform_deliveries = true
  3. config.action_mailer.raise_delivery_errors = true

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

答案1

得分: 0

  1. # controller
  2. InvoiceMailer.send_invoice(attachment).deliver
  3. # mailer
  4. def send_invoice(attachment)
  5. attachments['test-bill'] = File.read(attachment)
  6. ...
  7. end
英文:
  1. # controller
  2. InvoiceMailer.send_invoice(attachment).deliver
  3. # mailer
  4. def send_invoice(attachment)
  5. attachments[&#39;test-bill&#39;] = File.read(attachment)
  6. ...
  7. end
  8. </details>
  9. # 答案2
  10. **得分**: 0
  11. 这对我有效:
  12. 在控制器中:
  13. ```ruby
  14. attached_file = tmp_file.read
  15. attachment = Array.new
  16. attachment = {filename: "nameoffile.something", file: attached_file}
  17. InvoiceMailer.send_invoice(attachment).deliver

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

  1. def send_invoice(attachment)
  2. if attachment.present?
  3. attachments[attachment[:filename]] = {content: attachment[:file]}
  4. end
  5. ...
  6. end
英文:

this works for me:

in controller

  1. attached_file = tmp_file.read
  2. attachment&#160;= Array.new
  3. attachment = {filename: &quot;nameoffile.something&quot;, file: attached_file}
  4. 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.

  1. def send_invoice(attachment)
  2. if attachment.present?
  3. attachments[attachment[:filename]] = {content: attachment[:file]}
  4. end
  5. ...
  6. 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:

确定