Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActionMailer sending emails but with empty body

I don't know why, but e-mails sent by Rails Mailer using a Gmail account are being received with subject but with empty body...

Mailer:

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

Controller

  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

Contact form:

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

Message Template - mensagem.html.erb

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

Thanks!

like image 744
OtavioLipari Avatar asked Mar 24 '23 18:03

OtavioLipari


1 Answers

try add some params to mail function:

   mail(
    to: "[email protected]",
    subject: "Contato - Parceiro BRF",
    from: "Parceiro BRF",  
    return_path: "[email protected]",
    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 should be in my_app/app/views/contato_mailer/

like image 119
Rostyslav Diachok Avatar answered Apr 06 '23 08:04

Rostyslav Diachok