Ruby on Rails为什么我的电子邮件确认无效?

时间:2015-03-01 19:13:50

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

我想做什么:

向用户发送电子邮件,确认他们已成功注册。

我试图做的事情:

我已遵循本指南:http://guides.rubyonrails.org/action_mailer_basics.html

所以运行命令:'rails generate mailer UserMailer',并添加/编辑了以下文件,如下所示:

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

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

其他详细信息:

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

请注意我对Ruby很新。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

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

我将默认发件人更改为默认发件人,在测试期间,我还将deliver_later更改为deliver_now,而不是排队,它立即发生

将此app / views / user_mailer / welcome_email.txt更改为welcome_email.txt.erb

另外我设置了宝石开信刀,它设置起来非常简单,它会在浏览器标签上打开电子邮件,你会看到你的电子邮件,也许这会在你正在使用的rails版本中自动发生,但是很高兴知道。