【问题标题】:How do I replace static text with dynamic variable in Mandrill script如何在 Mandrill 脚本中用动态变量替换静态文本
【发布时间】:2015-11-10 23:34:16
【问题描述】:

我想在用户填写并提交表单后向他们发送电子邮件,我使用 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;

【问题讨论】:

    标签: php mandrill


    【解决方案1】:

    也许是这样的:

    $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 对其进行转换。

    【讨论】:

    • 感谢 Raphael,您非常乐于助人,它按我想要的方式工作
    • 不客气!不要忘记将问题标记为已解决!
    【解决方案2】:

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

    $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"
        }
    ],
    

    【讨论】:

      【解决方案3】:

      通过 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"
                              }
                          ]
                      }
                  ],
          ...
      

      格式

      合并标签使用以下格式,在合并标签名称的两边各有一个竖线 (|) 和星号 (*):

      |合并标签|

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

      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.
      

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

      参考:

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

      【讨论】:

        猜你喜欢
        • 2018-07-09
        • 2013-05-09
        • 1970-01-01
        • 2012-10-18
        • 2023-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多