在我的Ruby on Rails應用程序中,我試圖在網站上註冊時向用戶發送確認電子郵件。我跟着這個鏈接:http://guides.rubyonrails.org/action_mailer_basics.html。爲什麼我的電子郵件確認無效?
,並已創建以下文件:
應用程序/郵寄/ application_mailer.rb:
class ApplicationMailer < ActionMailer::Base
default sender: "[email protected]"
layout 'mailer'
end
應用程序/郵寄/ user_mailer.rb:
class UserMailer < ApplicationMailer
default from: '[email protected]'
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: "[email protected]"
default from: "[email protected]"
layout 'mailer'
end
users_controller.rb:
UserMailer.welcome_email(@user).deliver_now
而且還試過其他的東西。最有趣的事情是,當我改變user_mailer.rb到:
class UserMailer < ApplicationMailer
default from: '[email protected]'
def welcome_email(user)
@user = user
@url = 'http://localhost:3000/users/login'
# mail(to: @user.email, subject: 'Welcome to My Awesome Site')
mail(to: '[email protected]', subject: 'Welcome to My Awesome Site')
end
end
其中有硬編碼的電子郵件地址,還沒有任何作品。我沒有收到錯誤消息,用戶仍然可以註冊,但沒有收到電子郵件。
我使用了以下內容:
Rails的版本:4.2.0鋼軌
操作系統:Windows 7的
我連接到導軌服務器通過本地主機:3000 - 它在運行上我自己的機器。
我曾嘗試發送電子郵件到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有關嗎?
有沒有人有任何想法?
in'config/environments/development.rb'你有'config.action_mailer.raise_delivery_errors = true'和'config.action_mailer.perform_deliveries = true'嗎?你可以發佈你的'config.action_mailer.smtp_settings'設置嗎? – basiam 2015-03-02 19:15:06