gpt4 book ai didi

ruby-on-rails - 使用 Rails 使用 SendGrid 将附件添加到电子邮件

转载 作者:行者123 更新时间:2023-12-05 04:06:09 28 4
gpt4 key购买 nike

我创建了一个 hello_world 方法,它使用 Sendgrid 发送电子邮件。我正在尝试包含附件。我在另一个 stackoverflow 答案中找到了以下行:mail.attachments['test.txt'] = File.read("#{Rails.root}/public/test.txt")

然而,这会产生以下错误:

Completed 500 Internal Server Error in 17ms (ActiveRecord: 3.4ms)



TypeError - no implicit conversion of String into Integer:
app/controllers/messages_controller.rb:32:in `hello_world'
app/controllers/messages_controller.rb:65:in `create'

Controller 中的邮件代码:

  def hello_world(company, message)
from = Email.new(email: "test+#{current_user.auth_token}@example.com")
to = Email.new(email: 'hello@pim.gg')

subject = 'TEST from dev'
content = Content.new(type: 'text/plain', value: "#{company.email} #{current_user} #{current_user.first_name} #{current_user.last_name} #{message.text}")

mail = SendGrid::Mail.new(from, subject, to, content)
mail.attachments['test.txt'] = File.read("#{Rails.root}/public/test.txt")

sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
response = sg.client.mail._('send').post(request_body: mail.to_json)
puts response.status_code
puts response.body
puts response.headers
end

最佳答案

根据 sendgrid-ruby gem 的文档 adding-attachments应该是这样的:

attachment = SendGrid::Attachment.new
attachment.content = Base64.strict_encode64(File.open(fpath, 'rb').read)
attachment.type = 'application/vnd.openxmlformatsofficedocument.spreadsheetml.sheet'
attachment.filename = fname
attachment.disposition = 'attachment'
attachment.content_id = 'Reports Sheet'
mail.add_attachment(attachment)

关于ruby-on-rails - 使用 Rails 使用 SendGrid 将附件添加到电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50215225/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com