rails中使用邮件激活账户
2016-08-11 16:32
267 查看
生成邮件程序模版
开发环境中的邮件配置
生成application的邮件程序
app/mailers/application_mailer.rb
设置默认的发件人地址
生成user的邮件程序
app/mailers/user_mailer.rb
发送账户激活链接
账户激活的纯文本视图
app/views/user_mailer/account_activation.text.erb
账户激活的HTML视图
app/views/user_mailer/account_activation.html.erb
生成的邮件预览程序
test/mailers/previews/user_mailer_preview.rb
电子邮件测试
test/mailers/user_mailer_test.rb
执行
错误原因:
rails generate mailer UserMailer account_activation password_reset
开发环境中的邮件配置
config.action_mailer.raise_delivery_errors = true config.action_mailer.delivery_method = :test host = 'localhost:3000' config.action_mailer.default_url_options = { host: host }
生成application的邮件程序
app/mailers/application_mailer.rb
设置默认的发件人地址
class ApplicationMailer < ActionMailer::Base default from: "noreply@example.com" layout 'mailer' end
生成user的邮件程序
app/mailers/user_mailer.rb
发送账户激活链接
class UserMailer < ApplicationMailer def account_activation(user) @greeting = "Hi" @user = user mail to: user.email, subject: "Account activation" end def password_reset @greeting = "Hi" mail to: "to@example.org" end
账户激活的纯文本视图
app/views/user_mailer/account_activation.text.erb
Hi <%= @user.name %>, Welcome to the Sample App! Click on the link below to activate your account: <%= edit_account_activation_url(@user.activation_token, email: @user.email) %>
账户激活的HTML视图
app/views/user_mailer/account_activation.html.erb
<h1>Sample App</h1> <p>Hi <%= @user.name %>,</p> <p> Welcome to the Sample App! Click on the link below to activate your account: </p> <%= link_to"Activate", edit_account_activation_url(@user.activation_token, email: @user.email) %>
生成的邮件预览程序
test/mailers/previews/user_mailer_preview.rb
class UserMailerPreview < ActionMailer::Preview def account_activation user = User.first user.activation_token = User.new_token UserMailer.account_activation(user) end def password_reset UserMailer.password_reset end end
电子邮件测试
test/mailers/user_mailer_test.rb
require 'test_helper' class UserMailerTest < ActionMailer::TestCase test "account_activation" do user = users(:michael) user.activation_token = User.new_token mail = UserMailer.account_activation(user) assert_equal "Account activation", mail.subject assert_equal [user.email], mail.to assert_equal ["noreply@example.com"], mail.from assert_match user.name, mail.body.encoded assert_match user.activation_token, mail.body.encoded assert_match CGI.escape(user.email), mail.body.encoded end end
执行
rails test,发现测试无法通过,错误如下:
ActionView::Template::Error: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true
错误原因:
config/environments/test.rb中缺少
host配置,添加
config.action_mailer.default_url_options = { host: 'localhost:3000' }
相关文章推荐
- 使用Cucumber+Rspec玩转BDD(2)——邮件激活
- rails 3使用ActionMailer通过163发送邮件
- linux 使用msmtp登陆指定账户发送邮件
- 使用ssm框架实现用户账户邮箱激活功能(一)
- 使用 Indy 的 TIdSMTP, TIdPOP3收发邮件在验证账户的时候出现错误乱码解决方法
- 使用rails收发邮件
- java代码实现注册发送邮件激活账户
- SSH网上商城之实现邮件激活用户账户功能
- 使用Rails 4.2+ 测试异步邮件系统
- 使用ssm框架实现用户账户邮箱激活功能(三)
- .NET实现注册发送邮件激活账户
- Rails使用Action Mailer发送邮件的问题
- MVC使用ASP.NET Identity 2.0实现用户身份安全相关功能,比如通过短信或邮件发送安全码,账户锁定等
- 使用Laravel发送账号激活邮件
- 使用phpmailer发送邮件,且需要激活完整代码
- 使用exchange做为outlook2003的服务器,现在我想添加一个新的邮件账户进去,怎么操作
- rails3使用ActionMail发送邮件
- rails使用QQ邮箱发送邮件蛋疼的经历
- Rails中使用邮箱验证及激活用户
- PHP用户注册邮件激活账户的实现代码