英文:
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 #<InvoiceMailer:0x00007fde0d7bfe88> 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 #<InvoiceMailer:0x00007fde0d7bfe88>
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(['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.deliver
end
end
Here is my Invoice Mailer :
class InvoiceMailer < ApplicationMailer
default :from => 'Test <no-reply@test.co>'
def send_invoice
attachments['test-bill'] = File.read(attachment)
mail(to: "steven@test.com",
subject: "check if it works")
end
end
And my view 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>
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['test-bill'] = 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 = Array.new
attachment = {filename: "nameoffile.something", 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论