我正在尝试使用他们的Github 文档集成 Sendgrid 。
在他们的示例中,他们建议您创建一个 Mail Helper 类,但很少提供有关如何实际执行此操作的指导。
我有一个使用 Heroku Scheduler 运行的计划 Rake 任务,我想在任务完成时发送一封电子邮件,并希望为此使用 Sendgrid。
目前我有以下代码/lib/tasks/scheduler.rake
require 'sendgrid-ruby'
include SendGrid
desc "Testing Email Rake"
task :test_sendgrid => :environment do
puts 'Starting Sendgrid Email Test'
send_task_complete_email()
puts 'Sendgrid Email Test Complete'
end
def send_task_complete_email
from = Email.new(email: 'test@example.com')
to = Email.new(email: 'test@example.com')
subject = 'Sending with SendGrid is Fun'
content = Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby')
mail = Mail.new(from, subject, to, content)
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
我没有在任何地方添加辅助类,因为我不确定要添加哪些位或将它们放在哪里。在我运行这个任务的那一刻,我400 Bad request
从 Sendgrid 收到一个错误,我相信这是因为我没有这些助手。
任何解决此问题的建议都将不胜感激,因为当我尝试在不使用帮助程序的情况下进行集成而是写出 JSON 时,我可以成功发送电子邮件,但是TypeError: Mail is not a module
当我尝试部署到 Heroku 时会收到一个。
更新:使用下面的答案收到错误
400
{"errors":[{"message":"Invalid type. Expected: object, given: string.","field":"(root)","help":"http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#-Request-Body-Parameters"}]}
{"server"=>["nginx"], "date"=>["Thu, 07 Jun 2018 09:02:42 GMT"], "content-type"=>["application/json"], "content-length"=>["191"], "connection"=>["close"], "access-control-allow-origin"=>["https://sendgrid.api-docs.io"], "access-control-allow-methods"=>["POST"], "access-control-allow-headers"=>["Authorization, Content-Type, On-behalf-of, x-sg-elas-acl"], "access-control-max-age"=>["600"], "x-no-cors-reason"=>["https://sendgrid.com/docs/Classroom/Basics/API/cors.html"]}