如何在 Mandrill 脚本中用动态变量替换静态文本

How do I replace static text with dynamic variable in Mandrill script

我想在用户填写并提交表单后向他们发送电子邮件,我使用 POST 方法来捕获数据,现在我想用从表单中捕获的变量替换静态文本。我从表格中提供了一些可变数据样本。

$name=$_POST['name'];
$email=$_POST['email'];
$message=$_POST['message'];


$uri = 'https://mandrillapp.com/api/1.0/messages/send.json';

$postString = '{
"key": "xxxxxxxxxxxxx",
"message": {
    "html": "this is the emails html content",
    "text": "this is the emails text content",
    "subject": "this is the subject",
    "from_email": "me@gmail.com",
    "from_name": "John",
    "to": [
        {
            "email": "me@yahoo.com",
            "name": "Bob"
        }
    ],
    "headers": {

    },
    "track_opens": true,
    "track_clicks": true,
    "auto_text": true,
    "url_strip_qs": true,
    "preserve_recipients": true,

    "merge": true,
    "global_merge_vars": [

    ],
    "merge_vars": [

    ],
    "tags": [

    ],
    "google_analytics_domains": [

    ],
    "google_analytics_campaign": "...",
    "metadata": [

    ],
    "recipient_metadata": [

    ],
    "attachments": [

    ]
},
"async": false
}';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uri);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);

$result = curl_exec($ch);

echo $result;

也许是这样的:

$postString = '{
"key": "xxxxxxxxxxxxx",
"message": {
    "html": "'.$yourHTML.'",
    "text": "'.$yourTEXT.'",
    "subject": "this is the subject",
    "from_email": "me@gmail.com",
    "from_name": "John",
    "to": [
        {
            "email": "me@yahoo.com",
            "name": "Bob"
        }
    ],
    ...

但是您没有指定要放置动态数据的位置。

但对于您的情况,最好创建一个 PHP 数组,然后使用 json_encode.

对其进行转换

只需简单地用变量替换静态数据:

$postString = '{
"key": "xxxxxxxxxxxxx",
"message": {
"html": "",
"text": "$message",
"subject": "this is the subject",
"from_email": "me@gmail.com",
"from_name": "John",
"to": [
    {
        "email": "$email",
        "name": "$name"
    }
],

通过API

提供合并数据
...
    "message": {
            "global_merge_vars": [
                {
                    "name": "var1",
                    "content": "Global Value 1"
                }
            ],
            "merge_vars": [
                {
                    "rcpt": "emailadress@domain.com",
                    "vars": [
                        {
                            "name": "fname",
                            "content": "John"
                        },
                        {
                            "name": "lname",
                            "content": "Smith"
                        }
                    ]
                }
            ],
    ...

格式

合并标签使用以下格式,合并标签名称的每一侧都有一个竖线 (|) 和一个星号 (*):

|MERGETAG|

在您的模板或内容中,合并标签可能如下所示:

Dear *|FNAME|*,

Thanks for your purchase on *|ORDERDATE|* from ABC Widget Company. We appreciate your business and have included a copy of your invoice below.

*|INVOICEDETAILS|*

-- ABC Widget Co.

前面的示例中包含三个合并标签。在发送时,为每个合并标签提供全局值 and/or 收件人特定数据。

参考:

https://mandrill.zendesk.com/hc/en-us/articles/205582487-How-to-Use-Merge-Tags-to-Add-Dynamic-Content