Ruby on rails ActionMailer未发送电子邮件,尽管rails日志表明它已发送

Ruby on rails ActionMailer未发送电子邮件,尽管rails日志表明它已发送,ruby-on-rails,email,ruby-on-rails-4,actionmailer,Ruby On Rails,Email,Ruby On Rails 4,Actionmailer,我正在尝试实现actionmailer,以便在tasks controller中的show操作运行时通过电子邮件发送任务提醒。在本地主机上运行时,“我的日志”指示邮件已发送,但未到达目的地 My development.rb: Emailtest::Application.configure do config.action_mailer.delivery_method = :smtp config.action_mailer.smtp_settings = { address

我正在尝试实现actionmailer,以便在tasks controller中的show操作运行时通过电子邮件发送任务提醒。在本地主机上运行时,“我的日志”指示邮件已发送,但未到达目的地

My development.rb:

Emailtest::Application.configure do

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    address:              'smtp.gmail.com',
    port:                 587,
    domain:               'my_app.com',
    user_name:            ENV['xxx@gmail.com'],
    password:             ENV['xxx'],
    authentication:       'plain',
    enable_starttls_auto: true  }
  # 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

  # required for heroku
  config.action_mailer.default_url_options = { :host => 'localhost:3000' }

end
我的用户_mail.rb

class UserMail < ActionMailer::Base
  default from: "bri.lobdell@gmail.com"

  def task_reminder(task)
    @task = task
    @url = 'http://example.com/login'

    mail(to: @task.recipientemail, subject: "You've been sent a reminded")
  end

end

要在控制器中调用该行代码,可以创建一个表单,该表单将指向控制器中的某个操作,该操作将具有以下内容:

def send_mail
@task = Task.find(params[:task_id])
UserMail.task_reminder(@task).deliver
redirect_to :back
end
您还需要在routes文件中输入该控制器,如下所示:

post "tasks/send_mail", :to => "tasks#send_mail", :as => "send_mail"
这将是您的表单(非常简单),其中只有一个按钮。您可以在页面加载时触发该表单:

<%= form_tag("/tasks/send_mail", method: "post", url: send_mail_path) do  %>
<%= hidden_field_tag :task_id, "#{@task.id}" %>
<%= submit_tag "Submit" %>
<% end %>

如果它重定向到您,则会传递参数,如果没有,则会出现问题;)

a)请移动
UserMail.task\u提醒(@task)。将
发送给控制器。不要把它放在视图上。b) 测试时,@task.recipientemail是否与“发件人”电子邮件地址相同?如果是这种情况,请尝试其他电子邮件地址。我应该在控制器中的何处添加它?那么如何从视图中调用它呢?我在尝试用表单加载页面时收到一个错误。我不得不将“#{@task.id}”改为“task.id”,因为它是在一个循环中调用的,其中“@task.all”被定义为task。现在,我在任务控制器“@task”=task.find(params[:task_id])中的send_mail操作中得到一个关于这一行的错误。找不到id为Task.id的任务。有什么想法吗?*注意“@”任务行周围的“”只是为了使stackoverflow不认为我会将其发送给用户尝试检查隐藏字段的元素(或视图源)。
元素等于什么?很抱歉,我不太确定如何检查元素。在上面的代码块中编辑。只需在您的
发送邮件
方法中的任何其他内容之前添加它。您介意给我投票并接受答案吗?
def send_mail
    @task = Task.find(params[:id])
    UserMail.task_reminder(@task).deliver
    redirect_to :back
  end
def send_mail
@task = Task.find(params[:task_id])
UserMail.task_reminder(@task).deliver
redirect_to :back
end
post "tasks/send_mail", :to => "tasks#send_mail", :as => "send_mail"
<%= form_tag("/tasks/send_mail", method: "post", url: send_mail_path) do  %>
<%= hidden_field_tag :task_id, "#{@task.id}" %>
<%= submit_tag "Submit" %>
<% end %>
if(params.has_key?(:task_id)
redirect_to "/"
end