电子邮件不会在RoR应用程序中发送

时间:2014-07-03 06:59:39

标签: ruby-on-rails ruby email

我想在rails应用程序上从我的ruby发送邮件,然后按照以下步骤操作:

首先,我使用以下命令创建邮件:

rails generate mailer UserMailer

我在user_mailer.rb页面中进行以下操作:

class UserMailer < ActionMailer::Base
  default from: "xxxxx@xxxxx.com"

  def schedule
    @mail_to = 'abc@gmail.com' #params[:Schedule][:Appointments]
    mail :to => @mail_to, :subject => "Hi"
  end
end

schedule是我的观看页面schedule.html.erb

<%= form_for(:Schedule,:html => {:id => "Schedule"}) do |f| %>
    <table class="table" width="100%">
        <th><font size="+1">Interview Schedule</font></th>
    </table>
    <table>
        <tr>
            <td>
                <label style="font-size:small; font-weight:bold;">Send Appointments to:</label>
            </td>
            <td>
                <%= f.text_field :mail_to_address, :style => "font-size:small;" %>
            </td>
        </tr>
        <tr>
            <td style="vertical-align:top;">
                <label style="font-size:small; font-weight:bold;">Message:</label>
            </td>
            <td>
                <%= f.text_area :message, :style => "font-size:small; width:300px; height:100px;" %>
            </td>
        </tr>
        <tr>
            <td>
            </td>
            <td>
                <%= f.submit "Send", { :style => "font-size:small" } %>
            </td>
        </tr>
    </table>
<% end %>

在上面的代码mail_to_address text_field中,我将电子邮件地址放在我要发送电子邮件的位置,并在message text_field中添加一些文本并单击Send按钮,但邮件不是发送。为什么呢?

以下是environment.rb页码:

# Load the Rails application.
require File.expand_path('../application', __FILE__)

# Initialize the Rails application.
VideoResume::Application.initialize!
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
    :address => "smtp.gmail.com",
    :port => 587,
    :domain => "gmail.com",
    :authentication => :login,
    :charset => 'utf-8',
    :user_name => "abc@gmail.com",
    :password => "password"
}

并在我的App / config / environments / development.rb中我有:

VideoResume::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 = false

  # 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

  # 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

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

请建议我应该做什么,等待回复。 感谢

2 个答案:

答案 0 :(得分:3)

从上面的评论中我认为问题是你没有打电话给你的邮件程序。由于@mixian945@Sachin指出您需要致电邮件。

让我们假设您想要从控制器中的 create_schedule方法发送邮件,然后您可以通过

发送邮件
def create_schedule
  # your method logic
  UserMailer.schedule.deliver #this will call your schedule method in UserMailer class and hence send your mail
end

def schedule
  mail to: "abc@gmail.com", subject: "Hi"
end

另外如果您想将用户ID或其他内容发送到您的投放方式,那么您可以调用您的日程安排方法,如

def create_schedule
  @user = User.find(params[:id]) #your logic to find your user
  # your method logic
  UserMailer.schedule.deliver(@user.email)
end

这将允许您拥有类似

的方法
def schedule (email)
  mail :to => email, :subject => "Hi"
end

有关详细信息,请参阅Action Mailer Basics

答案 1 :(得分:0)

投放

底线是您在ROR you'll have to deliver it somehow

中创建mailer的时间

正如你所能see here,你基本上必须打电话给邮件&amp;获取事物的.deliver方法。这与您对邮件本身的声明并行:

#app/controllers/your_controller.rb
Class YourController < ApplicationController
   def action
      @mailer = UserMailer.schedule
      @mailer.deliver
   end
end

-

设置

显然,这需要进行一系列设置才能使其正常工作。我们使用gmail的SMTP设置进行开发测试:

#config/environments/development.rb
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
config.action_mailer.smtp_settings = {
      :address              => "smtp.gmail.com",
      :port                 => 587,
      :domain               => "testdomain.com",
      :user_name            => "test@gmail.com",
      :password             => Rails.application.secrets.gmail,
      :authentication       => :plain,
      :enable_starttls_auto => true
}
config.action_mailer.default_url_options = {
      :host => "localhost"
}

这样您就可以通过gmail的服务器发送消息 - 以上是工作代码