0

我正在尝试使用他们的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"]}
4

1 回答 1

3

您需要使用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 = Mail.new(from, subject, to, content)

mail = SendGrid::Mail.new(from, subject, to, content)
于 2018-06-07T05:59:11.133 回答