Sendgrid 电子邮件中的名称 Rails

Name in Sendgrid Email with Rails

我正在使用 careerfoundry's 关于使用 Sendgrid 通过 Heroku 发送电子邮件的教程。我在页面控制器中将他们的确切代码用于用户邮件程序和 thank_you 操作。此代码大部分都有效。问题是我在收件箱中收到的主题行是这样的:

来自#{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

联系表

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

页面控制器

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

一个快速的解决方法是将字符串分成两部分

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

这样表示法更清晰,也避免了手动拼接的问题。

快速查找了一下,好像这​​个也可以。根据我发现的几个来源,显然内部需要单引号。我不使用该表示法,所以如果它有帮助,请告诉我!

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