It's my first time setting up mails in a rails project. I was told to use SparkPost and to create templates for different languages for several actions.
For simplicity lets say a user_signed_up(user) mail.
Currently I have this setup working:
Gem installed: 'sparkpost'
mail.rb
ActionMailer::Base.smtp_settings = {
address: "smtp.sparkpostmail.com",
port: 587,
enable_starttls_auto: true,
user_name: "SMTP_Injection",
password: SPARKPOST_API_KEY,
domain: 'foo-bar.com'
}
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.default charset: "utf-8"
application_mailer.rb
require 'sparkpost'
class ApplicationMailer < ActionMailer::Base
default from: "Seal Notification <[email protected]>"
layout 'mailer'
end
signup_mailer.rb
class SignupMailer < ApplicationMailer
def user_signed_up(user)
receiver = user.email
sender = '[email protected]'
title = 'Thanks for registering'
body = 'This is a test body'
sparky = SparkPost::Client.new(SPARKPOST_API_KEY)
sparky.transmission.send_message(receiver,sender,title,body)
end
end
And I can successfully send emails.
Although, this is definitely not scaleable due to multi language and body not style-able.
Now I need to setup templates to allow non-technical people to adjust email templates.
But here is where I am stuck and an answer to following questions would help me tremendously:
1) How can I send specific email templates?
2) How do I pass variables to these templates?
3) How do I handle multiple language support?
Thank you.
Here's an intro article on creating templates in SparkPost.
Here's one on previewing your templates and sending test messages - including how variables work (aka 'substitution data').
Long form Ruby-centric answers follow:
A couple of observations on your code first: It looks like you are both configuring SMTP globally but using the REST API in your signup mailer. I'd recommend the REST API over SMTP since it has the templating and other rich capabilities you require.
1) You can manage email templates either the SparkPost UI here or directly by API call as documented here. The template syntax is documented here.
Once you have a created and published a template, you can send using the SparkPost client like this (assuming your template ID is 'your-template-en'):
require 'sparkpost'
host = 'https://api.sparkpost.com'
SparkPost::Request.request("#{host}/api/v1/transmissions", API_KEY, {
recipients: [
{ address: { email: '[email protected]' } }
],
content: {
template_id: 'your-template-en'
}
})
2) SparkPost supports message-level and recipient-level 'substitution_data' which are JSON-formatted variables for use in your templates. Here's a sample transmission request:
SparkPost::Request.request("#{host}/api/v1/transmissions", API_KEY, {
recipients: [
{
address: { email: '[email protected]' },
substitution_data: {
first_name: 'Recip',
favorites: {
color: 'Orange',
ice_cream: 'Vanilla'
}
}
}
],
content: {
template_id: 'your-template-en'
},
substitution_data: {
title: 'Daily News'
}
})
You now use substitution data in your templates. For example:
<h1>{{title}}</h1>
<p>Hi {{first_name or 'there'}}</p>
<p>This {{favorites.color}} bulletin is all about {{favorites.ice_cream}} ice cream</p>
Note: recipient substitution data takes precedence over message-level fields.
3) For the multi-lingual use case, you might consider creating a template per language as many of our other customers do.
Incidentally, this looks like several questions - should we consider splitting them up?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With