使用Rails-4发送电子邮件时出现问题

时间:2015-01-07 05:32:18

标签: ruby-on-rails email ruby-on-rails-4

有人可以帮我解决这个问题。我正在尝试向一位用户发送电子邮件。代码中没有显示错误,但无法向用户发送电子邮件。我的代码片段如下所述。请检查并帮助我成功运行。

视图/用户/ index.html.erb

<h1>Send email to your friend</h1>
<%= form_for @user,:url => {:action => 'create'} do |f| %>
    <%= f.text_field:name,placeholder:"Enter your name" %><br>
    <%= f.email_field:email,placeholder:"Enter your email" %><br>
    <%= f.submit "Send" %>
<% end %>

视图/用户/ success.html.erb

<h1>Email sent successfully</h1>

视图/用户/ new.html.erb

<h1>Successfully registered</h1>

控制器/ user_controller.rb

class UsersController < ApplicationController
  def index
    @user=User.new
  end
  def create
    @user=User.new(users_params)
    if @user.save
      UserMailer.registration_confirmation(@user).deliver
      redirect_to :action => 'success'
    else
      redirect_to :action => 'index'
    end
  end
  def new

  end
  def success

  end
  private
  def users_params
    params.require(:user).permit(:name, :email)
  end
end

的routes.rb

Rails.application.routes.draw do
 root 'users#index'
  post "users/create" => "users#create"
 get "users/success" => "users#success"
  get "users/new" => "users#new"
end

视图/ user_mailer文件/ registration_confirmation.text.erb

<%= @user.name%>
<h1>Thank you for registering</h1>
Click <%= link_to "here",users_new_path %>

寄件人/ user_mailer.rb

class UserMailer < ApplicationMailer
  default :from => "w5call.w5rtc@gmail.com"
  def registration_confirmation(user)
    @user=user
    mail(:to => user.email, :subject => "Registered")
  end
end

配置/为setup_mail.rb

ActionMailer::Base.smtp_settings = {
    :address              => "smtp.gmail.com",
    :port                 => 587,
    :domain               => "gmail.com",
    :user_name            => "w5call.w5rtc@gmail.com",
    :password             => "w5rtc123@",
    :authentication       => "plain",
    :enable_starttls_auto => true
}
ActionMailer::Base.default_url_options[:host] = "localhost:3000"

development.rb

Rails.application.configure do
  # Settings specified here will take precedence over those in config/application.rb.

  # In the development environment your application's code is reloaded on
  # every request. This slows down response time but is perfect for development
  # since you don't have to restart the web server when you make code changes.
  config.cache_classes = false

  # Do not eager load code on boot.
  config.eager_load = false

  # Show full error reports and disable caching.
  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = false

  # Don't care if the mailer can't send.
  config.action_mailer.raise_delivery_errors = true

  # Print deprecation notices to the Rails logger.
  config.active_support.deprecation = :log

  # Raise an error on page load if there are pending migrations.
  config.active_record.migration_error = :page_load

  # Debug mode disables concatenation and preprocessing of assets.
  # This option may cause significant delays in view rendering with a large
  # number of complex assets.
  config.assets.debug = true

  # Asset digests allow you to set far-future HTTP expiration dates on all assets,
  # yet still be able to expire them through the digest params.
  config.assets.digest = true

  # Adds additional error checking when serving assets at runtime.
  # Checks for improperly declared sprockets dependencies.
  # Raises helpful error messages.
  config.assets.raise_runtime_errors = true

  # Raises error for missing translations
  # config.action_view.raise_on_missing_translations = true
end

我正在关注此link。我正在使用rails版本4.2.0和ruby 1.9.3。请帮我成功运行它。谢谢..

0 个答案:

没有答案