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

时间:2015-10-27 11:43:28

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

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

我希望用户能够发送到他们点击的任何电子邮件链接。那么如何将默认设置为方法动态?

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

2 个答案:

答案 0 :(得分:1)

不要使用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

答案 1 :(得分:1)

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

  

我希望用户能够发送到他们点击的任何电子邮件链接   上

而是将代码更改为

<% 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