【问题标题】:Why does my email confirmation not work?为什么我的电子邮件确认不起作用?
【发布时间】:2015-05-03 06:20:20
【问题描述】:

在我的 Ruby on Rails 应用程序中,我试图在用户在网站上注册时向他们发送一封确认电子邮件。我点击了这个链接:http://guides.rubyonrails.org/action_mailer_basics.html

并创建了以下文件:

app/mailers/application_mailer.rb:

class ApplicationMailer < ActionMailer::Base
  default sender: "ben@thorcinemas.com"
  layout 'mailer'
end

app/mailers/user_mailer.rb:

class UserMailer < ApplicationMailer
  default from: 'notifications@example.com'

  def welcome_email(user)
    @user = user
    @url  = 'http://localhost:3000/users/login'
    mail(to: @user.email, subject: 'Welcome to My Awesome Site')
  end
end

app/views/user_mailer/welcome_email.html.erb:

<!DOCTYPE html>
<html>
  <head>
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
  </head>
  <body>
    <h1>Welcome to example.com, <%= @user.first_name %></h1>
    <p>
      You have successfully signed up to example.com,
      your username is: <%= @user.first_name %><%= @user.last_name %>.<br>
    </p>
    <p>
      To login to the site, just follow this link: <%= @url %>.
    </p>
    <p>Thanks for joining and have a great day!</p>
  </body>
</html>

app/views/user_mailer/welcome_email.txt.erb:

Welcome to example.com, <%= @user.first_name %>
===============================================

You have successfully signed up to example.com,
your username is: <%= @user.first_name %><%= @user.last_name %>.

To login to the site, just follow this link: <%= @url %>.

Thanks for joining and have a great day!

app/controller/users_controller.rb:

  def create
    @user = User.new(user_params)
    if @user.save
      # login is achieved by saving a user's 'id' in a session variable, 
      # accessible to all pages
       session[:user_id] = @user.id
       UserMailer.welcome_email(@user).deliver_later
       redirect_to films_path
    else
       render action: "new"
    end
  end

我已尝试多次编辑此代码,包括:

application_mailer.rb:

class ApplicationMailer < ActionMailer::Base
  # default sender: "ben@thorcinemas.com"
  default from: "ben@thorcinemas.com"
  layout 'mailer'
end

users_controller.rb:

UserMailer.welcome_email(@user).deliver_now

并且还尝试过其他的东西。最有趣的是,当我将 user_mailer.rb 更改为:

class UserMailer < ApplicationMailer
  default from: 'notifications@example.com'

  def welcome_email(user)
    @user = user
    @url  = 'http://localhost:3000/users/login'
    # mail(to: @user.email, subject: 'Welcome to My Awesome Site')
    mail(to: 'p13226664@myemail.dmu.ac.uk', subject: 'Welcome to My Awesome Site')
  end
 end

其中包含硬编码的电子邮件地址,但仍然没有任何作用。我没有收到任何错误消息,用户仍然可以注册,但没有收到任何电子邮件。

我正在使用以下内容:

Rails 版本:rails 4.2.0

操作系统:Windows 7

我通过 localhost:3000 连接到 rails 服务器 - 它在我自己的机器上运行。

我曾尝试向 gmail 和 tiscali 帐户发送电子邮件,但均未成功。

Schema.rb:

create_table "users", force: :cascade do |t|
    t.string   "password_digest"
    t.string   "role"
    t.datetime "created_at",      null: false
    t.datetime "updated_at",      null: false
    t.string   "first_name"
    t.string   "last_name"
    t.string   "house_no"
    t.string   "street"
    t.string   "town"
    t.string   "postcode"
    t.string   "email"
    t.date     "date_of_birth"
  end

有人可以帮忙吗?是因为我试图通过 localhost:3000 在我自己的机器上运行电子邮件系统而不使用服务器吗?和 SMTP 有关系吗?

有人有什么想法吗?

【问题讨论】:

  • config/environments/development.rb 中有config.action_mailer.raise_delivery_errors = trueconfig.action_mailer.perform_deliveries = true 吗?你能发布你的config.action_mailer.smtp_settings 设置吗?

标签: ruby-on-rails ruby ruby-on-rails-3 email smtp


【解决方案1】:

您可能应该查看this part 的文档。默认情况下,Rails 不能也不会在您的开发环境中发送电子邮件。您需要配置一个邮件服务器。

或者您可以使用类似letter_opener gem 的东西来模拟电子邮件发送,但它会将您的电子邮件作为新的浏览器选项卡打开。这对于开发来说相当方便,生产环境可以使用真实的邮件服务器。

【讨论】:

  • 谢谢,现在看宝石
【解决方案2】:

您是否按照第 6.1 节和第 6.2 节所述指南配置您的应用程序?

【讨论】:

    猜你喜欢
    • 2015-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-27
    • 2018-04-18
    • 1970-01-01
    • 2014-12-12
    相关资源
    最近更新 更多