如何在 Rails Web 应用程序的 Ruby 中验证是否已发送帐户激活电子邮件

How to verify if an account activation email was sent or not in Ruby on Rails web application

我正在构建一个具有帐户激活功能的网络应用程序。我正在关注 Michael Hartl ROR 教程,但我的收件箱没有收到任何帐户激活电子邮件。我试图在线搜索如何验证是否已发送电子邮件。就像我们应该配置一个 SMTP 服务器来发送电子邮件吧?没有找到任何帮助。有人可以帮忙吗?

我的account_activations_controller.rb

class AccountActivationsController < ApplicationController

  def edit
    user = User.find_by(email: params[:email])
    if user && !user.activated? && user.authenticated?(:activation, params[:id])
      user.activate
      log_in user
      flash[:success] = "Account activated!"
      redirect_to user
    else
      flash[:danger] = "Invalid activation link"
      redirect_to root_url
    end
  end
end

User_mailer.rb

class UserMailer < ApplicationMailer

  def account_activation(user)
    @user = user
    mail to: user.email, subject: "Account activation"
  end
end

application_mailer.rb

class ApplicationMailer < ActionMailer::Base
  default from: "noreply@example.com"
  layout 'mailer'
end

development.rb

config.action_mailer.raise_delivery_errors = true
  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.delivery_method = :test
  host = 'localhost:3000'
  config.action_mailer.default_url_options = { :host=> host }

将此添加到您的 config/environments/development.rb 文件

config.action_mailer.raise_delivery_errors = true

因此,如果电子邮件发送有误,您会收到通知。

除此之外,您可以在 users table 中添加一个字段,例如 activation_sent_at 并在发送电子邮件时填充它,以便您稍后参考。

桑迪普,

继续阅读教程。我使用旧版本 Michael 的教程来学习 rails,但当我也想发送确认电子邮件时又回来了。我认为您不应该期望发送实际的电子邮件(至少不是在开发模式下),rails 4.1.x 有您可以查看的邮件预览,我认为这就是教程封面。

进入生产环境后,您将需要设置一个外部服务器来发送实际的电子邮件,但这将在您的 production.rb 文件中。如果您只是在学习,那么您有很多选择,包括设置一个 gmail 帐户(与您的私人帐户分开),每天发送有限数量的电子邮件。

这是 link 我认为你应该看看: listing 10.15

我对 Michael Hartl 的教程的体验是它几乎完美无缺,您的特定系统设置可能会遇到一两个问题,但我认为大多数情况下我遇到的任何问题都与我没有按照文本进行操作有关正确。

希望对您有所帮助,祝您好运!

从第 491 页底部开始阅读:"Note that you will not receive an actual email in a development environment, but it will show up in your server logs. Section 10.3 discusses how to send email for real in a production environment."

因此,如果您想明确地遵循文本,我会查看第 10.3 节中的内容。希望对您有所帮助!