哪里放ruby helper Sendgrid V3发送API

时间:2018-06-06 16:00:18

标签: ruby-on-rails ruby api sendgrid sendgrid-api-v3

我正在尝试使用Github documentation集成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

我没有在任何地方添加辅助类,因为我不确定要添加哪些位或放在哪里。在我执行此任务的那一刻,我收到了来自Sendgrid的400 Bad request错误,我相信它是因为我没有这些助手。

任何修复此问题的建议都会非常受欢迎,因为当我尝试在不使用帮助程序的情况下进行集成而写出JSON时,我可以成功发送电子邮件,但在尝试部署到Heroku时会收到TypeError: Mail is not a module

更新:使用以下答案收到的错误

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"]}

1 个答案:

答案 0 :(得分:1)

您需要使用Action Mailer

首先创建一个邮件程序类来添加您的邮件详细信息(即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.new(from, subject, to, content)

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