【问题标题】:Insert php code inside sendgrid mail content html [duplicate]在sendgrid邮件内容html中插入php代码[重复]
【发布时间】:2018-05-06 03:18:48
【问题描述】:

我正在使用 Sendgrid 通过 PHP 发送电子邮件,但我需要在 HTML 内容中包含一个 PHP 脚本。我不知道如何执行连接,看起来真的很微妙。 这是我的代码:

$content = new SendGrid\Content("text/html", '

/*
some HTML content
*/

/*PHP script starts here*/

'.myPHP Script {


      }//end of script
    .'

/*some more HTML content
        ');

'..' 连接不起作用,整个页面无法加载。我该怎么做?我不想输出代码。我希望它实际执行。

【问题讨论】:

    标签: php concatenation sendgrid


    【解决方案1】:

    不要让生活变得困难。你需要连接什么字符串,把它放在一个变量中并连接它。另请注意,您可以在 PHP 中使用双引号 "$variable" 进行字符串插值。

    $concatHTML = '';
    $prodList = [...]; // assume we have products list here
    for ($i = 0, $max = count($prodList); $i < $max; $i++) {
        $concatHTML += '<div>'. $prodList[$i] .'</div>';
    }
    
    
    $content = new SendGrid\Content("text/html", "
      //some html content here
      $concatHTML
    ");
    

    编辑

    可以使用操作符进行连接。 (例如)echo 'hi ' . $name;其中 $name = 'some string' 或使用双引号和放置(例如)echo "hi $name" 的字符串插值,您可以添加花括号以将变量与其他字符串(如 echo "hi Mr.{$name}")分开。

    对于您想要包含在电子邮件中的 php 代码可能无效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-07
      • 2013-08-04
      • 1970-01-01
      相关资源
      最近更新 更多