如何实施换人?

How to implement substitutions?

我正在使用 Email::SendGrid::V3 Perl 库,我的目标是通过使用他们的名字问候他们来向许多收件人发送一个 e-mail。但是我不知道如何使用他们的在线文档执行个性化:

https://metacpan.org/pod/Email::SendGrid::V3#$self-%3Eset_section($key,-$value);

我可以向两个不同的人发送一封电子邮件,但我缺少有关如何处理 body 替换的信息。

use Email::SendGrid::V3;

my $sg = Email::SendGrid::V3->new(api_key => 'ABCDE');

my $result = $sg->from('noreply@mydomain.com')
->subject('This is a subject line')
->add_envelope( to => [ {email => 'john@mydomain.com', name => 'John Smith' }] )
->set_section('-NAME-', 'John')
->add_content('text/html', 'Hello -NAME-, how are you?')
->send;

print $result->{success} ? "It worked" : "It failed: " . $result->{reason};

任何提示将不胜感激。

这只是模板;您有一个模板,并且想要填写值。 Text::Template is a straightforward implementation of this, though since your result is HTML, you want an HTML-aware template engine like Text::Xslate or Mojo::Template so you don't have to remember to HTML-escape each value. There is also Template::Toolkit 这是整体 most-used 和最可配置的模板系统。下面是一个使用 Mojo::Template.

的例子
use strict;
use warnings;
use Mojo::Template;

my $mt = Mojo::Template->new(vars => 1, auto_escape => 1);
my $template = 'Hello <%= $name %>, how are you?';
my $rendered = $mt->render($template, {name => 'John'});

我有机会从开发人员那里得到快速反馈,解决方案如下:

my $result = $sg->from('noreply@mydomain.com')
->subject('This is a subject line')
->add_content('text/html', 'Hello -NAME-, how are you?')
->add_envelope( to => [ {email => 'fred@mydomain.com', name => 'Fred Smith' }], substitutions => { '-NAME-' => 'Fred' } )
->add_envelope( to => [ {email => 'john@mydomain.com', name => 'John Smith' }], substitutions => { '-NAME-' => 'John' } )
->send;