哪里放ruby帮手Sendgrid V3发送API

Where to put ruby helpers Sendgrid V3 Send API

我正在尝试使用他们的 Github documentation 来集成 Sendgrid。

在他们的示例中,他们建议您创建一个邮件助手 class,但很少提供有关如何实际执行此操作的指导。

我有一个使用 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

我没有在任何地方添加助手 classes,因为我不确定要添加哪些位或将它们放在哪里。在我 运行 这个任务的那一刻,我从 Sendgrid 收到一个 400 Bad request 错误,我相信这是因为我没有这些助手。

任何解决此问题的建议将不胜感激,因为当我尝试在不使用助手的情况下进行集成而不是写出 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"]}

你需要使用Action Mailer:

首先创建一个邮件程序 class 以添加您的邮件详细信息(即 UserMailer):

$ bin/rails generate mailer UserMailer

它将创建以下文件:

create  app/mailers/user_mailer.rb
create  app/mailers/application_mailer.rb
invoke  erb
create    app/views/user_mailer
create    app/views/layouts/mailer.text.erb
create    app/views/layouts/mailer.html.erb
invoke  test_unit
create    test/mailers/user_mailer_test.rb
create    test/mailers/previews/user_mailer_preview.rb

现在编辑文件 app/mailers/user_mailer.rb:

require 'sendgrid-ruby'
include SendGrid

class UserMailer < ApplicationMailer
  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
end

然后您可以简单地使用此代码发送电子邮件:

UserMailer.send_task_complete_email.deliver_now

或来自 rake 任务:

desc "Testing Email Rake"
task :test_sendgrid => :environment do
  puts 'Starting Sendgrid Email Test'
  UserMailer.send_task_complete_email.deliver_now
  puts 'Sendgrid Email Test Complete'
end 

更新:

因为 Rails 中有 Mail 模块,您需要通过更改指定正确的 SendGrid Mail 模块:

 mail = Mail.new(from, subject, to, content)

mail = SendGrid::Mail.new(from, subject, to, content)