Search code examples
rubyemailsinatramailer

How to pass variables into Mail body template?


I am trying to write simple mailer in Sinatra which sends email with params variables.

require 'sinatra'
require 'mail'

class App < Sinatra::Base
  post '/test_mailer' do
    company = params['Field6']
    email = params['Field5']

    puts "Company name: #{company}"
    puts "Email: #{email}"

    mail = Mail.new do
      from     'me@mydomain.com'
      to       'me@mydomain.com'
      subject  'Here is the image you wanted'
      text_part do
        body "Company Name \n === \n #{company} \n \n Email \n === \n #{email}"
      end
    end

    mail.deliver!
  end
end

How to move email template to test_mailer.txt with company and email variables ?


Solution

  • I'm not sure I understand you - you want an separate email template file, right? I'm thinking you could use an erb, or haml template and then do something like the following:

    text_part do
      body erb(:test_mailer)
    end
    

    Your test_mailer.erb file would then contain your email template.

    Here shows how something similar is done using pony.