Rails 4 向其他用户发送电子邮件

Rails 4 sending email to other users

我正在尝试创建一个表单,用户可以在其中向其他用户发送电子邮件。当用户单击 'link_to' 电子邮件时,他们将被发送到联系表单,在那里他们可以相互联系。它工作正常,除了我不知道如何在 user_mailer.rb 中动态设置 'default to'。

我希望用户能够发送到他们点击的任何电子邮件 link。那么如何使默认方法动态化?

user.html.erb

<% if user.email.present? %>
    <b>Email:</b>
     <%= link_to "email", contact_path %>
<% end %>

user_mailer.rb

class UserMailer < ActionMailer::Base
  default to: #user email

  def contact_email(name, email, body)
      @name = name
      @email = email
      @body = body

      mail(from: email, subject: '')
  end
end

contact.html.erb

<div class="container-content">
    <div class="container">
        <%= form_tag(send_mail_path) do %>
            <div class="form-group">
                <%= label_tag 'name', 'Name' %>
                <%= text_field_tag 'name', nil, class: 'form-control', placeholder: 'Your Name' %>
            </div>
           <div class="form-group">
               <%= label_tag 'email', 'Email' %>
               <%= email_field_tag 'email', nil, class: 'form-control', placeholder: 'Your Email Address' %>
           </div>
           <div class="form-group">
               <%= label_tag 'comments', 'Comments' %>
               <%= text_area_tag 'comments', nil, class: 'form-control', rows: 4, placeholder: 'Comments...' %>
           </div>
           <%= submit_tag nil, class: 'btn btn-default btn-about pull-right' %>
       <% end %>
  </div>
</div>

user_controller.rb

 def send_mail
    name = params[:name]
    email = params[:email]
    body = params[:comments]
    UserMailer.contact_email(name, email, body).deliver_now
    redirect_to contact_path, notice: 'Message sent'
  end

只是不要使用 default to 并像这样写邮件:

class UserMailer < ActionMailer::Base

  def contact_email(name, email, body, user)
      @name = name
      @email = email
      @body = body

      mail(from: email, to: user.email, subject: '')
  end
end

更新

user.html.erb

<% if user.email.present? %>
    <b>Email:</b>
     <%= link_to "email", contact_path(user: user) %>
<% end %>

user_controller.rb

def send_mail
    name = params[:name]
    email = params[:email]
    body = params[:comments]
    user = params[:user]
    UserMailer.contact_email(name, email, body, user).deliver_now
    redirect_to contact_path, notice: 'Message sent'
  end

我看不到为 default to: 设置值的方法,因为不同用户的收件人电子邮件会发生变化(即,它们是动态的)。如果我没记错的话 default to: 的值应该是静态的。

I want the user to be able to send to whichever email link they click on

改为将您的代码更改为以下内容

<% if user.email.present? %>
  <b>Email:</b>
  <%= link_to "email", contact_path(to_email: user.email) %> #pass an extra parameter
<% end %>

然后在 form 中创建一个 隐藏字段 如下所示

<%= hidden_field_tag 'to_email', params[:to_email] %>

所以现在您可以在控制器操作中像 params[:to_email] 一样访问它。

def send_mail
  name = params[:name]
  email = params[:email]
  body = params[:comments]
  to_email = params[:to_email]
  #pass to_email as argument to this method
  UserMailer.contact_email(name, email, body, to_email).deliver_now
  redirect_to contact_path, notice: 'Message sent'
end

最后邮寄方法将如下所示

class UserMailer < ActionMailer::Base

  def contact_email(name, email, body, to_email)
    @name = name
    @email = email
    @body = body

    mail(from: email, subject: '', to: to_email)
  end
end