首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >连接装置与Mailjet

连接装置与Mailjet
EN

Stack Overflow用户
提问于 2018-11-16 22:01:04
回答 2查看 668关注 0票数 1

我们正在尝试将我们的设计密码重置表单连接到mailjet宝石。我已经遵循了所有的说明,邮机回购读-我,但我仍然有困难连接API到设计‘重置密码’形式,这似乎是明显的东西是缺少的。

( a)首先,我在google、MailJet的支持站点以及Stackoverflow上到处搜索,但似乎在任何地方都找不到任何帮助连接MailJet的说明。我错过了什么吗?

( b)如果没有预先编写的指南说明如何做到这一点,是否有人能帮助我采取具体步骤,以确保在我们的设计登录页上按下“发送密码重置链接”,它就能正确地发送电子邮件?现在,我们收到了一个错误:电子邮件是必需的。但我的电子邮件地址在那里。所以它没能正常连接起来。我认为我们需要将特定的设计页面连接到mailjet,但我不确定如何做到这一点。

接下来,这种测试是在本地主机上进行的,还是在heroku上进行测试的唯一方法?也许这就是问题之一..。

( d)最后,我是否必须在Heroku上做任何事情才能连接到我的MailJet帐户?显然,您使用SendGrid,但我想,因为我已经有了mailjet和秘密密钥,我不需要直接通过Heroku连接,对吗?

提前感谢您的帮助。-Monroe

到目前为止,我的代码如下:

密码重置页

代码语言:javascript
复制
<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>

邮递员本身:

代码语言:javascript
复制
<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>

INITIALIZER

代码语言:javascript
复制
# 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

DEVELOPMENT.RB

代码语言:javascript
复制
  # 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

代码语言:javascript
复制
# 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

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-11-16 22:33:23

您需要的唯一需要是Gemfile中的Mailjet宝石:

代码语言:javascript
复制
gem 'mailjet'

和下面的配置,这是我写到config/initializers/mail.rb

代码语言:javascript
复制
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文件中发送电子邮件:

代码语言:javascript
复制
config.action_mailer.delivery_method = :mailjet

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

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

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

代码语言:javascript
复制
config.mailer_sender = 'GoRails <no-reply@gorails.com>'

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

代码语言:javascript
复制
class ApplicationMailer < ActionMailer::Base
  default from: 'GoRails <no-reply@gorails.com>'
  layout 'mailer'
end
票数 1
EN

Stack Overflow用户

发布于 2018-11-17 02:24:49

我们把它修好了。以下是一些你可能会发现的有用的额外提示:

  • 克里斯证实我的development.rb和production.rb是正确的,所以你可以用我的作为向导。
  • 我们发现有两个主要问题: 1)我们忘记了更新config.mailer_sender,正如Chris指出的那样;2)我们不得不删除error:email is required代码。
  • 一旦我们这样做,它就奏效了,甚至在发展中也是如此。然而,我们收到了一封被踢开的电子邮件,称我们没有授权电子邮件地址(我们在config.mailer_sender中设置的邮箱地址),因此我们遵循了它在电子邮件中提供的链接,这使我们无法在邮件机上找到其他页面,从而使我们可以从公司的域白名单所有的电子邮件地址。这就是克里斯上面所讨论的。他提供了上面的网址,但是你可以很容易地通过重新设置密码来获得它,如果一切都设置正确,你会收到一封电子邮件,上面写着发件人的电子邮件还没有被授权。
  • 然后,我们被要求通过域主机(即GoDaddy)安装SPF和DKIM记录:https://support.smtp2go.com/hc/en-gb/articles/223086887-SPF-and-DKIM-Setup-for-GoDaddy
  • 最后,我们又做了一次测试,结果成功了。密码重置电子邮件程序功能正确!

希望这能有所帮助!

更新:为了在本地主机上测试并使其工作,我们还必须更改以下内容:

代码语言:javascript
复制
config.action_mailer_default_url_options= { host: 'localhost:3000' }

对此:

代码语言:javascript
复制
config.action_mailer.default_url_options = { host: "localhost", port: 3000 }

实际上它不会从本地主机发送电子邮件,因为我们在mailjet上设置了安全设置。但是,如果您在请求重新设置密码时立即检查rails服务器日志,您应该看到电子邮件已经发送,以及电子邮件本身的主题和正文。想必,这也将适用于生产。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53346015

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档