简体   繁体   中英

Name in Sendgrid Email with Rails

I'm using careerfoundry's tutorial on using Sendgrid to send emails with Heroku. I'm using their exact code for the user mailer and thank_you action in the pages controller. This code is working for the most part. The issue is that the subject line I receive in my inbox is this:

A new contact form message from #{name}

User_Mailer.rb

class UserMailer < ActionMailer::Base
  default from: "from@example.com"

  def contact_form(email, name, message)
    @message = message
    mail(:from => email, 
            :to => 'your-email@example.com', 
            :subject => "A new contact form message from #{name}")
  end
end

Contact Form

<%= form_tag("/thank_you") do %>
    <div class="row">
      <div class="col-md-4">
        <div class="input-group">
            <%= text_field_tag :name, nil, class: 'form-control', placeholder: 'Name' %>
        </div>
      </div>
      <div class="col-md-8">
        <div class="input-group">
          <span class="input-group-addon" id="basic-addon1">@</span>
          <%= text_field_tag :email, nil, class: 'form-control', placeholder: 'Email' %>
        </div>
      </div>
    </div>
    <br>
    <div class="row">
      <div class="col-md-12">
        <div class="input-group text-area-wide">
            <%= text_area_tag :message, nil, class: 'form-control text-area-wide', placeholder: 'Message' %>
        </div>
      </div>
    </div>
    <br>
    <%= submit_tag 'Send Message', class: 'btn btn-success' %>
<% end %>

Pages Controller

def thank_you
    @name = params[:name]
    @email = params[:email]
    @message = params[:message] || "Hello!"
    # Only try to send this email on a POST
    # if request.post?
    # or make a feedback form controller and send in the create action

    # ActionMailer::Base.mail( FOR TEST PURPOSES
    #     :from => @email, 
     #  :to => 'erikvdw@comcast.net', 
     #  :subject => "A new contact form message from #{@name}", 
     #  :body => @message).deliver
    UserMailer.contact_form(@email, @name, @message).deliver

  end

A quick fix would be to just split the string into two parts as such

class UserMailer < ActionMailer::Base
  default from: "from@example.com"

  def contact_form(email, name, message)
    @message = message
    mail(:from => email, 
            :to => 'your-email@example.com', 
            :subject => "A new contact form message from " + name)  <----
  end
end

This way the notation is clearer and it will avoid the manual concatenation problem.

After looking up a quick thing, it seems like this might also work. The single quotes are needed internally apparently according to a couple sources I found. I don't use that notation so let me know if it helped!

:subject => "A new contact form message from '#{name}'")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM