2015-03-02 109 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节中所述配置了您的应用程序?