Ruby on Rails - 一个用户向另一个用户发送电子邮件 - 邮件程序

时间:2016-12-14 16:33:34

标签: ruby-on-rails email mailer

我希望一位用户能够通过点击按钮向其他用户发送电子邮件。他们将收到一封自动生成的电子邮件,上面写着“您已被[用户]挑战!”因此,在视图上会有一个下拉菜单,可以从用户列表中进行选择,然后是一个“挑战”按钮,它会将此电子邮件发送给该用户。我已经设置了发送注册邮件的邮件。

这是电子邮件:

<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
</head>
 <body>
<h1>You have been challeneged by, <%= @opponent.student_name %>.</h1>

 </body>
</html> 

这是我的usermailer.rb(我的用户被称为'学生')

  def welcome(student)
    @student = student

    mail(:to => student.email, :subject => "Welcome to the DIT Judo club!")
  end

  def challenge(student, opponent)
    @student = student
    @opponent = opponent
    mail(:to => student.email, :subject => "You have been challeneged to a Judo match!")
  end
end

这是配置邮件程序的东西,以防你想看到它:

  config.action_mailer.raise_delivery_errors = true

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings ={
        :enable_starttls_auto => true,
        :address => 'smtp.gmail.com',
        :port => 587,
        :domain => 'smtp.gmail.com',
        :authentication => :plain,
        :user_name => 'xxxxx',
        :password => 'xxxxxx'
  }

所以基本上在一个视图中我想要一个下拉当前用户(学生)选择另一个用户(学生),然后你可以点击提交来触发挑战电子邮件。我是铁道新手,有人可以帮我做吗?

这是我的微弱尝试:

在路线文件夹中:

  get 'challenge', :to=>'usermailer#challenge'

  resources :matches do
        put :challenge
  end

在我的索引中:

<div>
Challenge another student to a match!
  <div class="field">
    <%= f.label :opponent %><br> 

    <%= f.collection_select :opponent, Student.all, :id, :student_name %> 
  </div>
    <%= link_to 'challenge', match challenge_path([current studentID ??], [opponent from  collection]), method: :put %>

</div>

1 个答案:

答案 0 :(得分:0)

您的welcomechallenge方法只是创建并返回邮件对象。您需要在邮件对象上调用deliver来实际发送它。请参阅&#34;呼叫邮件程序&#34;在the documentation了解更多详情。