【问题标题】:Where to put ruby helpers Sendgrid V3 Send API在哪里放置 ruby​​ 助手 Sendgrid V3 发送 API
【发布时间】:2018-06-06 16:00:17
【问题描述】:

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

【问题讨论】:

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


    【解决方案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 = Mail.new(from, subject, to, content)
    

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

    【讨论】:

    • 感谢您的回答,但不幸的是,我在使用此功能时收到 400 错误消息。我的实现与您的完全一样,我已经用收到的错误更新了我的问题。再次感谢您提供的任何帮助。
    • @TomPinchen 你可以尝试用mail = SendGrid::Mail.new(from, subject, to, content)替换mail = Mail.new(from, subject, to, content)
    • 太棒了!这已经奏效了,非常感谢您的帮助:)
    • 欢迎更新 Mail 的命名空间冲突。该代码直接来自他们的 Ruby 集成页面,并且在 sendgrid-ruby 文档中也有重复,它不起作用。太烦人了。
    猜你喜欢
    • 1970-01-01
    • 2015-10-23
    • 1970-01-01
    • 1970-01-01
    • 2012-11-16
    • 1970-01-01
    • 2018-11-09
    • 2023-04-10
    • 1970-01-01
    相关资源
    最近更新 更多