Actionmailer - Sparkpost template and multilanguage

穿精又带淫゛_ 提交于 2019-12-07 08:30:36

问题


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 <noreply@foobar.com>"
  layout 'mailer'
end

signup_mailer.rb

class SignupMailer < ApplicationMailer
  def user_signed_up(user)
    receiver = user.email
    sender = 'myself@test.com'
    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.


回答1:


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: 'recipient@example.com' } }
  ],
  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: 'recipient@example.com' },
      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?



来源:https://stackoverflow.com/questions/35628731/actionmailer-sparkpost-template-and-multilanguage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!