为什么我的电子邮件确认不起作用?

时间:2015-03-02 19:08:38

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

在我的Ruby on Rails应用程序中,我试图在用户在网站上注册时向用户发送确认电子邮件。我已关注此链接:http://guides.rubyonrails.org/action_mailer_basics.html

并创建了以下文件:

应用程序/邮寄者/ application_mailer.rb:

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

应用程序/邮寄者/ 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

应用程序/视图/ 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>

应用程序/视图/ 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!

应用程序/控制器/ 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有关吗?

有没有人有任何想法?

2 个答案:

答案 0 :(得分:0)

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

或者您可以使用类似letter_opener gem的内容来模拟电子邮件发送,但它会将您的电子邮件打开为新的浏览器标签。这对于开发来说非常方便,您可以使用真实的邮件服务器进行生产。

答案 1 :(得分:0)

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