【发布时间】: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 = true和config.action_mailer.perform_deliveries = true吗?你能发布你的config.action_mailer.smtp_settings设置吗?
标签: ruby-on-rails ruby ruby-on-rails-3 email smtp