ActionMailer发送电子邮件但空体

时间:2013-06-01 07:27:54

标签: ruby-on-rails actionmailer

我不知道为什么,但是Rails Mailer使用Gmail帐户发送的电子邮件正在收到主题但空身......

邮件程序:

class ContatoMailer < ActionMailer::Base
  default from: "contato@parceirobrf.com.br" 
  def send_email(user_info)
      @user_info = user_info   
       mail(
        to: "contato@parceirobrf.com.br",
        subject: "Contato - Parceiro BRF",
        from: "Parceiro BRF",  
        return_path: "contato@parceirobrf.com.br",
        date: Time.now,
        content_type: "text/html"
      )
    end
end

控制器

  def dispatch_email
  user_info = params[:user_info]
  if ContatoMailer.send_email(user_info).deliver
    flash[:notice] = "Mensagem enviada."
  else
    flash[:notice] = "Oops. Sua mensagem não pôde ser enviada."
  end
  redirect_to "/contato"
end

联系表格:

<%= form_tag dispatch_email_path, method: :post do %>
        <dt>E-mail:</dt>
        <dd><%= usuario_atual.email%></dd>
        <%= hidden_field_tag "user_info[email]", usuario_atual.email  %><br />
        <dt>Nome:</dt>
        <dd><%= usuario_atual.nomefantasia%></dd>
        <%= hidden_field_tag "user_info[name]", usuario_atual.nomefantasia  %><br />            
        <dt>Fone Contato:</dt>
        <dd><%= usuario_atual.fone1_prop%></dd>
    </dl>
    <label for="lbl-txt">Mensagem:</label>
    <div class="area">
        <%= text_area_tag "user_info[message]", "", size: "60x10" %>
    </div><!-- / area -->
    <div class="btn-row">
        <%= submit_tag "Enviar" , :class => 'btn'%>
    </div>
    <% end %>

消息模板 - mensagem.html.erb

<p>Nome: <%= @user_info["name"] %></p>
<p>Email: <%= @user_info["email"] %></p>
<p>Mensagem: <%= @user_info["message"] %></p>

谢谢!

1 个答案:

答案 0 :(得分:6)

尝试在mail函数中添加一些参数:

   mail(
    to: "contato@parceirobrf.com.br",
    subject: "Contato - Parceiro BRF",
    from: "Parceiro BRF",  
    return_path: "contato@parceirobrf.com.br",
    date: Time.now,
    content_type: "text/html",
    template_path: 'contato_mailer', # folder in views where are mail templates
                                     # by default its snakifyed controller name           
    template_name: 'mensagem'        # name of template 
                                     # by default its snakifyed action name
  )   

mensagem.html.erb应该在my_app/app/views/contato_mailer/