我似乎找不到有关如何将 Sendgrid Web API 集成到 Ruby on Rails 应用程序中的分步教程。我对此还很陌生,所以也许我遗漏了一些明显的东西。
我想使用 Sendgrid Web API 而不是 smtp 传送方法(mailgun 在这里讨论了 Web API 相对于 SMTP 方法的好处: https://documentation.mailgun.com/quickstart-sending.html ,我认为 Sendgrid 也有相同的好处或者我稍后可能会切换到邮件枪)。
安装 sendgrid gem ( https://github.com/sendgrid/sendgrid-ruby ) 后,文档告诉我“使用您的 SendGrid API key 创建一个新客户端”,我可以通过两种方式完成此操作:
require 'sendgrid-ruby'
# As a hash
client = SendGrid::Client.new(api_key: 'YOUR_SENDGRID_APIKEY')
# Or as a block
client = SendGrid::Client.new do |c|
c.api_key = 'YOUR_SENDGRID_APIKEY'
end
我应该将此代码放在应用程序中的具体位置吗?我应该把它放在我的邮件程序、我的应用程序邮件程序中还是放在 config/environments/production.rb 文件中?
我查看了本教程,其中介绍了如何设置 Mailgun API:https://launchschool.com/blog/handling-emails-in-rails
根据本教程,看起来 client = SendGrid::Client.new(api_key: 'YOUR_SENDGRID_APIKEY')
行实际上应该进入邮件程序方法本身。请参阅下面的 launchschool.com 示例(大概用 sendgrid 信息替换 mailgun 特定信息):
class ExampleMailer < ActionMailer::Base
def sample_email(user)
@user = user
mg_client = Mailgun::Client.new ENV['api_key']
message_params = {:from => ENV['gmail_username'],
:to => @user.email,
:subject => 'Sample Mail using Mailgun API',
:text => 'This mail is sent using Mailgun API via mailgun-ruby'}
mg_client.send_message ENV['domain'], message_params
end
end
此外,如何让我的邮件程序方法发送邮件程序 View 而不是像 launchschool 示例中所述的简单文本?例如,我不想发送文本“此邮件是使用...发送的”,而是发送邮件程序 View (类似于 account_activation.html.erb)。
最后,我在我的应用程序中使用 Devise,我希望 Devise 使用 Web API 发送电子邮件(即密码重置等)。这是否意味着我需要为 Devise 创建自定义邮件程序?如果是这样,我该怎么做?
根据 Devise ( https://github.com/plataformatec/devise/wiki/How-To:-Use-custom-mailer ),我应该“创建一个扩展 Devise::Mailer 的类”。这是否意味着我只需在邮件程序文件夹中创建一个文件,其中包含文档中列出的信息?我是否需要为 Devise 提供单独的邮件程序,或者我可以从 Devise 邮件程序继承现有的邮件程序吗?最后,如何告诉 devise 使用 sendgrid Web api 发送电子邮件(而不是简单的 smtp 方法)?
很抱歉问了这么长的问题,但希望其他人发现它有用。
谢谢!
最佳答案
我会创建一个邮件程序类来执行此操作
class SendgridWebMailer < ActionMailer::Base
include Sendgrid
def initialize
@client = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']).client
end
def send_some_email(record, token)
mail = Mail.new
// do your mail setup here
mail = Mail.new
mail.from = Email.new(email: YOUR_EMAIL_HERE)
mail.subject = YOUR_SUBJECT
// I personally use sendgrid templates, but if you would like to use html -
content = Content.new(
type: 'text/html',
value: ApplicationController.render(
template: PATH_TO_TEMPLATE,
layout: nil,
assigns: IF_NEEDED || {}
)
mail.contents = content
personalization = Personalization.new
personalization.to = Email.new(email: EMAIL, name: NAME)
personalization.subject = SUBJECT
mail.personalizations = personalization
@client.mail._('send').post(request_body: mail.to_json)
end
end
使用调用它
SendgridWebMailer.send_some_email(record, token).deliver_later
对于设计
class MyDeviseMailer < Devise::Mailer
helper :application # gives access to all helpers defined within `application_helper`.
include Devise::Controllers::UrlHelpers # Optional. eg. `confirmation_url`
def reset_password_instructions(record, token, opts={})
SendgridWebMailer.send_some_email(record, token).deliver_later
end
end
在config/devise.rb中
# Configure the class responsible to send e-mails.
config.mailer = 'MyDeviseMailer'
关于ruby-on-rails - 用于 Ruby on Rails 的 Sendgrid API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36414469/