【发布时间】:2015-06-07 18:39:44
【问题描述】:
我正在尝试向用户发送一封电子邮件,以确认他们已成功注册该网站。我已遵循指南并拥有以下文件。
开发.rb:
config.action_mailer.delivery_method = :smtp
# SMTP settings for gmail
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:user_name => ENV['email'],
:password => ENV['password'],
:authentication => "plain",
:enable_starttls_auto => true
}
应用程序.rb:
require File.expand_path('../boot', __FILE__)
require 'rails/all'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module ThorCinema
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
# Do not swallow errors in after_commit/after_rollback callbacks.
config.active_record.raise_in_transactional_callbacks = true
config.autoload_paths << Rails.root.join('app', 'validators')
gmail_username: 'email'
gmail_password: 'password'
end
end
users_controller:
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_now
UserMailer.welcome_email(@user).deliver
@user.add_default_preferences
redirect_to films_path
else
render action: "new"
end
end
mailers/user_mailer.rb
class UserMailer < ApplicationMailer
default from: 'no-reply@thorcinemas.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: @user.email, subject: 'Welcome to My Awesome Site')
end
end
mailers/application_mailer:
class ApplicationMailer < ActionMailer::Base
default from: "from@example.com"
layout 'mailer'
end
views/welcome_email.html.erb
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<h1>Welcome to Thor Cinemas, <%= @user.first_name %>!</h1>
<p>
You have successfully signed up to Thor Cinemas,
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>
但它不起作用,当我尝试运行 rails s 时,我得到以下输出:
C:\Sites\Thor\Under Construction\ThorCinema\new\Lab\Newu\ThorCinema>rails s
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.2.0/lib/rails/ra
iltie.rb:196:in `method_missing': undefined method `gmail_username' for ThorCine
ma::Application:Class (NoMethodError)
from C:/Sites/Thor/Under Construction/ThorCinema/new/Lab/Newu/ThorCinema
/config/application.rb:25:in `<class:Application>'
from C:/Sites/Thor/Under Construction/ThorCinema/new/Lab/Newu/ThorCinema
/config/application.rb:9:in `<module:ThorCinema>'
from C:/Sites/Thor/Under Construction/ThorCinema/new/Lab/Newu/ThorCinema
/config/application.rb:8:in `<top (required)>'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.2.0
/lib/rails/commands/commands_tasks.rb:78:in `require'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.2.0
/lib/rails/commands/commands_tasks.rb:78:in `block in server'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.2.0
/lib/rails/commands/commands_tasks.rb:75:in `tap'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.2.0
/lib/rails/commands/commands_tasks.rb:75:in `server'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.2.0
/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.2.0
/lib/rails/commands.rb:17:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
有人可以帮忙吗。
【问题讨论】:
-
向我们展示完整的
config/application.rb文件。 -
@Зелёный 我已经更新了问题
-
为什么 gmail_username 和 gmail_password 在 application.rb 中?
-
@JérémyButtice 我遵循了本指南:gotealeaf.com/blog/handling-emails-in-rails,这就是它所说的输入从 gmail 发送的内容。我还有什么方法可以做到?
-
在本教程中,他解释了将环境变量添加到 application.yml 而不是 application.rb,application.yml 用于 figaro gem。
标签: ruby-on-rails ruby-on-rails-3 email ruby-on-rails-3.2 smtp