简体   繁体   English

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

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

I was building a web app with account activation feature. 我正在构建一个具有帐户激活功能的Web应用程序。 I am following Michael Hartl ROR tutorial and I am not getting any account activation email to my inbox. 我正在关注Michael Hartl ROR教程,但没有收到任何帐户激活电子邮件到我的收件箱。 I tried to search online on how to verify if at all an email was sent. 我尝试在线搜索如何验证是否已发送电子邮件。 Like there should be an SMTP server we configure to send emails right? 就像应该配置一个SMTP服务器来发送电子邮件,对不对? Did not find anything helpful. 没有发现任何帮助。 Can someone help pls? 有人可以帮忙吗?

My account_activations_controller.rb 我的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 User_mailer.rb

class UserMailer < ApplicationMailer

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

application_mailer.rb application_mailer.rb

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

development.rb 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 }

Sandeep, 桑迪普,

Keep reading the tutorial. 继续阅读教程。 I used an older version Michael's tutorial to learn rails, but came back when I wanted to send confirmation emails as well. 我使用了较旧版本的Michael教程来学习Rails,但是当我也想发送确认电子邮件时回来了。 I don't think you should expect an actual email to be sent (at least not in development mode), rails 4.1.x has mailer previews that you can view, which I think is what the tutorial covers. 我认为您不应该期望发送实际的电子邮件(至少不是在开发模式下),Rails 4.1.x具有您可以查看的邮件预览,我认为这是本教程涵盖的内容。

When you get to a production environment, then you will need to set up an external server to send the actual email, but that will be in your production.rb file. 当进入生产环境时,您将需要设置一个外部服务器来发送实际的电子邮件,但这将在您的production.rb文件中。 If you are just learning, then you have many options, including setting up a gmail account (separate from your private account) in which to send a limited number of emails per day. 如果您只是在学习,则有很多选择,包括设置gmail帐户(与您的私人帐户分开),每天可以在其中发送有限数量的电子邮件。

Here is the link I think you should look at: listing 10.15 这是我认为您应该查看的链接: 清单10.15

My experience with Michael Hartl's tutorial was that it was almost flawless, you may have an issue or two with your particular system setup, but I think for the most part any issues I had were related to me not following the text properly. 我对Michael Hartl的教程的经验是,它几乎是完美无缺的,您的特定系统设置可能有一个或两个问题,但是我认为大部分与我有关的问题都与我没有正确遵循本文有关。

Hope this help, good luck! 希望有帮助,祝您好运!

Add this to your config/environments/development.rb file 将此添加到您的config/environments/development.rb文件

config.action_mailer.raise_delivery_errors = true

so you get notified if there is an error on email delivery. 这样一来,如果您在发送电子邮件时遇到错误,就会收到通知。

Other than that you can add a field to the users table like activation_sent_at and populate it when sending the email so you can refer to it later. 除此之外,您还可以向users表中添加一个字段,例如activation_sent_at ,并在发送电子邮件时填充该字段,以便以后参考。

Reading from the bottom of page 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." 从第491页的底部开始阅读:“请注意,在开发环境中您不会收到实际的电子邮件,但会在服务器日志中显示。第10.3节讨论了如何在生产环境中实际发送电子邮件。”

So if you're wanting to follow the text explicitly, I'd checkout the goodies in Section 10.3. 因此,如果您想显式地遵循本文,我将在10.3节中介绍这些优点。 Hope that helps! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM