2015-03-02 39 views
0

在我的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有關嗎?

有沒有人有任何想法?

+0

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

回答

0

您應該檢查文檔的this part。 Rails不能也不會在您的開發環境中默認發送電子郵件。您需要配置郵件服務器。

或者您可以使用類似letter_opener的寶石,它可以模擬電子郵件發送,但它會將您的電子郵件作爲新的瀏覽器標籤打開。這對於開發非常方便,您可以使用真實郵件服務器進行生產。

+0

謝謝,現在看着寶石 – 2015-03-02 19:24:28

0

您是否按照6.1節和6.2節中所述配置了您的應用程序?