Actionmailer-Sparkpost模板和多语言 [英] Actionmailer - Sparkpost template and multilanguage

查看:104
本文介绍了Actionmailer-Sparkpost模板和多语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次在Rails项目中设置邮件。
被告知要使用SparkPost并为几种操作创建不同语言的模板。



为简单起见,我们假设一个user_signed_up(user)邮件。



当前我正在使用此设置:



安装了宝石:'sparkpost'



mail.rb

  ActionMailer :: Base.smtp_settings = {
地址: smtp.sparkpostmail.com,
端口:587,
enable_starttls_auto:true,
用户名: SMTP_Injection,
密码:SPARKPOST_API_KEY,
域:'foo-bar.com'
}

ActionMailer :: Base.delivery_method =:smtp
ActionMailer :: Base.default字符集: utf-8

application_mailer.rb

 需要'sparkpost'
类ApplicationMailer< ActionMailer :: Base
默认来自:封条通知< noreply@foobar.com>
布局邮件程序
结尾

signup_mailer.rb

 类SignupMailer< ApplicationMailer 
def user_signed_up(user)
接收者= user.email
发送者='myself@test.com'
标题='感谢注册'
body ='这是一个测试体'
sparky = SparkPost :: Client.new(SPARKPOST_API_KEY)
sparky.transmission.send_message(接收者,发送者,标题,正文)
结束
结束

我可以成功发送电子邮件。



虽然,由于多种语言和无法适应样式的原因,这绝对不可扩展。



现在,我需要设置模板,以允许非技术人员调整电子邮件模板。





但这是我被困住的地方,回答以下问题将极大地帮助我:



1)如何发送特定的电子邮件模板?



2)如何将变量传递给这些模板?



3)如何处理多种语言支持?



谢谢。

解决方案

< a href = https://support.sparkpost.com/customer/en/portal/articles/1929890-creating-a-template?b_id=7411>此处是介绍文章,内容涉及在SparkPost中创建模板。



这里是有关预览模板和发送测试消息的信息-包括

以Ruby为中心的长格式答案如下:



一对夫妇对代码的观察:首先,您似乎都在全局配置SMTP,但在注册邮件中使用了REST API。我建议使用SMTP上的REST API,因为它具有所需的模板功能和其他丰富功能。



1)您可以通过SparkPost用户界面或直接通过API调用记录在这里此处记录了模板语法。



一旦创建并发布了模板,就可以使用SparkPost客户端发送邮件(假设您的模板ID为'your-template-en'):

 需要'sparkpost'

host ='https://api.sparkpost.com'
SparkPost :: Request.request(#{host } / api / v1 / transmissions,API_KEY,{
收件人:[
{地址:{电子邮件:'recipient@example.com'}}}
],
内容: {
template_id:'your-template-en'
}
})

2)SparkPost支持消息级别和收件人级别的 substitution_data,它们是JSON格式的变量,可在模板中使用。这是一个传输请求示例:

  SparkPost :: Request.request(#{host} / api / v1 / transmissions, API_KEY,{
位收件人:[
{
地址:{电子邮件:'recipient@example.com'},
替代数据:{
first_name:'Recip',
的最爱:{
颜色:橙色,
ice_cream:香草
}
}
}
],
内容:{
template_id:'your-template-en'
},
替代数据:{
标题:'Daily News'
}
} )

您现在在模板中使用替换数据。例如:

 < h1> {{title}}< / h1> 
< p>您好{{first_name or’there}}< / p>
< p>此公告{{favorites.color}}与{{favorites.ice_cream}}冰淇淋有关。

注意:收件人替换数据优先于消息级字段。



3)对于多语言用例,您可以考虑像其他许多客户一样为每种语言创建模板。



这似乎是几个问题-我们应该考虑将它们拆分吗?


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.

解决方案

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?

这篇关于Actionmailer-Sparkpost模板和多语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆