【问题标题】:Linking Devise to Mailjet将设计链接到 Mailjet
【发布时间】:2018-11-16 22:01:04
【问题描述】:

我们正在尝试将我们的设计密码重置表单链接到 mailjet gem。我已按照 mailjet 存储库自述文件中的所有说明进行操作,但我仍然无法将 API 连接到 Devise“重置密码”表单,而且似乎很明显缺少某些内容。

a) 首先,我在 google、mailjet 的支持网站和 Stackoverflow 上到处搜索,但似乎找不到任何帮助将 MailJet 连接到 Devise 的说明。我错过了什么吗?

b) 如果没有关于如何执行此操作的预先编写的指南,是否有人可以帮助我执行我需要采取的特定步骤,以确保当我在我们的设计登录页面上按“发送密码重置链接”时,它正确地发送电子邮件?现在,我们收到一个错误:需要电子邮件。但我的电子邮件地址在那里。所以它没有正确连接。我认为我们需要将该特定设计页面连接到 mailjet api,但我不确定如何做到这一点。

c) 接下来,这是在 localhost 上可测试的,还是在 heroku 上进行测试的唯一方法?也许这可能是问题之一......

d) 最后,我是否必须在 Heroku 本身上做任何事情才能链接到我的 MailJet 帐户?显然,您使用 SendGrid,但我认为因为我已经有了 mailjet API 和 Secret Key,我不需要直接通过 Heroku 连接,是吗?

提前感谢您的帮助。 -梦露

到目前为止,这是我的代码:

密码重置页面

<main class="form forgot-form">
  <section class="form-container">
    <h1>Reset password</h1>
    <p>Enter the email address associated with your account, and we’ll email you a link to reset your password.</p>
    <%= simple_form_for(resource, as: resource_name, url: password_path(resource_name), html: { method: :post }, :defaults => {  wrapper: false }) do |f| %>
      <%= f.error_notification %>
      <%= f.input :email, required: true, autofocus: true, error: 'Email is required', 
      input_html: { class: 'form__field' }, label_html: { class: 'sr-only' }, placeholder: 'Email Address' %>
      <%= f.button :submit, "Send reset link", class: 'form__button mt-4' %>
    <% end%>
    <%= link_to '< Back to Login', new_user_session_path, class: "link go-page" %>
  </section>
</main>

邮寄者本身:

<p>Hello <%= @resource.email %>!</p>

<p>Someone has requested a link to change your password. You can do this through the link below.</p>

<p><%= link_to 'Change my password', edit_password_url(@resource, reset_password_token: @token) %></p>

<p>If you didn't request this, please ignore this email.</p>
<p>Your password won't change until you access the link above and create a new one.</p>

初始化器:

# kindly generated by appropriated Rails generator
Mailjet.configure do |config|
  config.api_key = 'my-api-key--removed for safety'
  config.secret_key = 'my-secret-key--removed for safety'
  config.default_from = 'my-email--removed for safety'
  # Mailjet API v3.1 is at the moment limited to Send API.
  # We’ve not set the version to it directly since there is no other endpoint in that version.
  # We recommend you create a dedicated instance of the wrapper set with it to send your emails.
  # If you're only using the gem to send emails, then you can safely set it to this version.
  # Otherwise, you can remove the dedicated line into config/initializers/mailjet.rb.
  config.api_version = 'v3.1'
end

开发.RB

  # Specify that we are using the MailJet API for transactional emails
  config.action_mailer.delivery_method = :mailjet

  # Don't care if the mailer can't send.
  config.action_mailer.raise_delivery_errors = false

  config.action_mailer.perform_caching = false

PRODUCTION.RB

# Use a real queuing backend for Active Job (and separate queues per environment)
  # config.active_job.queue_adapter     = :resque
  # config.active_job.queue_name_prefix = "bdcommunity_#{Rails.env}"
  config.action_mailer.perform_caching = false
  # Ignore bad email addresses and do not raise email delivery errors.
  #Set this to true and configure the email server for immediate delivery to raise delivery errors.
  #NOTE FROM MONROE: We should look into this after we launch and as we develope the site further
  #config.action_mailer.raise_delivery_errors = false
  # Specify that we are using the MailJet API for transactional emails
  config.action_mailer.delivery_method = :mailjet
  # config.action_mailer.perform_deliveries = true

上面的“perform_deliveries”是我在 2011 年的一篇文章中发现的,所以它很可能不起作用。我只是把它放在那里(注释掉),以防它可以帮助你找出我们做错了什么。

【问题讨论】:

    标签: ruby-on-rails heroku devise mailjet


    【解决方案1】:

    您只需要 Gemfile 中的 Mailjet gem:

    gem 'mailjet'
    

    以及我写给config/initializers/mail.rb的以下配置:

    Mailjet.configure do |config|
      config.api_key      = Rails.application.secrets.mailjet_username
      config.secret_key   = Rails.application.secrets.mailjet_password
      config.default_from = 'user@example.com'
    end
    

    然后您将配置您的环境以使用 mailjet 在您的 config/environments 文件中发送电子邮件:

    config.action_mailer.delivery_method = :mailjet
    

    您可能还需要在您的 Mailjet 帐户中检查“发件人地址”以发现任何问题:https://app.mailjet.com/account/sender

    对于https://gorails.com,我在此处添加了 gorails.com 域,以批准该域的所有电子邮件。

    然后对于 Devise,您可以编辑 config/initializers/devise.rb 以更改其使用的电子邮件地址:

    config.mailer_sender = 'GoRails <no-reply@gorails.com>'
    

    您还可以自定义 ApplicationMailer 以使用相同的默认值。

    class ApplicationMailer < ActionMailer::Base
      default from: 'GoRails <no-reply@gorails.com>'
      layout 'mailer'
    end
    

    【讨论】:

    • 感谢克里斯的回复!
    • 我不需要调整设计邮件和“重置密码表单”吗?
    • 最后,这也适用于开发,还是仅适用于生产?
    • 只要您在 dev 中设置 API 密钥,它就可以在开发中工作。是的,我会更新我的帖子以改变线路的设计。
    • 最后一个问题:您能否澄清最后一点建议,RE:“从生产线改变设计”?所以我需要在设计中修改一些东西?
    【解决方案2】:

    我们让它工作了。以下是一些您可能会觉得方便的额外提示:

    • Chris 确认我的 development.rb 和 production.rb 是正确的,因此您可以使用我的作为指导。
    • 我们发现有两个主要问题:1) 正如 Chris 指出的那样,我们忘记更新 config.mailer_sender 以及 2) 我们必须删除 error:email is required 代码。
    • 一旦我们这样做了,它就起作用了,即使在开发中也是如此。但是,我们收到了一封被退回的电子邮件,说我们没有授权电子邮件地址(我们在 config.mailer_sender 中设置的那个),所以我们按照它在电子邮件中提供给我们的链接进行操作,这使我们无法找到其他页面在 mailjet 上,它允许我们将公司域中的所有电子邮件地址列入白名单。这就是克里斯在上面讨论的内容。他在上面提供了该网址,但您可以通过尝试重置密码轻松获取它,如果一切设置正确,您将收到一封电子邮件,说明发件人电子邮件尚未授权。
    • 然后我们被要求通过我们的域名托管服务商(GoDaddy)安装 SPF 和 DKIM 记录:https://support.smtp2go.com/hc/en-gb/articles/223086887-SPF-and-DKIM-Setup-for-GoDaddy
    • 最后,我们再次测试,它成功了。密码重置电子邮件功能正常!

    希望这会有所帮助!

    更新: 为了测试并使其在本地主机上运行,​​我们还必须更改它:

    config.action_mailer_default_url_options= { host: 'localhost:3000' }
    

    到这里:

    config.action_mailer.default_url_options = { host: "localhost", port: 3000 }
    

    由于我们在 mailjet 上设置的安全设置,它实际上并没有从 localhost 发送电子邮件。但是,如果您在请求重置密码后立即检查您的 Rails 服务器日志,您应该会看到电子邮件已发送,以及电子邮件本身的主题和正文。据推测,这现在也适用于生产。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-01
      • 2013-01-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多