【问题标题】:How to use php foreach loop in MailGun API如何在 MailGun API 中使用 php foreach 循环
【发布时间】:2017-06-20 20:15:16
【问题描述】:

我在 google VM 实例上安装了 mailgun,不久就知道我无法在传出端口(25,587 等)上向我的客户发送账单电子邮件,所以我注册了 MailGun,我创建并购买了基本计划...

如果我发送已发布的值,一切正常。 $_POST['email'] 和 $_POST['subject'],但是对于账单电子邮件,我想从数据库中获取数据并将其注入 MailGun 参数中,请注意我使用的是 PHP,我的代码看起来像那样

$mg->messages()->send('xxxx.co.uk', [
      'from'    => 'xxxx@xxxx.co.uk', 
      'to'      => ''.$_SESSION['user_email'].'',
      'bcc'    => 'xxxxx@xxxxxx.xxx', 
      'subject' => 'Your Treatment Order with xxxxxx', 
      'html'    => '
                    <!DOCTYPE HTML PUBLIC ""-//IETF//DTD HTML//EN"">
                    <html>
                    <head>
                    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
                    <title>Your Treatment Order with XXX</title>
                    </head>
                    <body>
                    <table width="550" height="200" cellpadding="0" cellspacing="0" align="center" style="border:1px solid #777777;  padding: 25px; margin-top: 25px;" bgcolor="#ffffff">
                    <tr><td align="center" colspan="5" style="background-color:#303f46; padding-top: 10px; padding-bottom: 10px;"><img src="http://www.xxxxx.co.uk/images/logo.png"></td></tr>
                    <tr><td height="1" bgcolor="#777777" colspan="5"></td></tr>
                    <tr><td height="10" colspan="5"></td></tr>
                    <tr><td height="1" bgcolor="#777777" colspan="5"></td></tr><tr><td height="5" colspan="5"></td></tr>
                    <tr><td align="left" class="hometitle" colspan="5">Your Treatment Order with XXXX</td></tr><tr><td height="5" colspan="5"></td></tr>
                    <tr><td height="1" bgcolor="#777777" colspan="5"></td></tr><tr><td height="5" colspan="5"></td></tr>
                    <tr><td align="left" class="hometext" colspan="5"><span style="color: #777777";>
                        <p>Hello,</p>
                        <p>Thanks for your order. We’ll let you know once your item(s) have <b>confirmed</b>. Your estimated confirmation time will not exceed few hours. You can view the details of your order by visiting <a href="http://www.xxxx.co.uk/orders.php?id='.$transactionId.'">My Orders</a> section on xxxx.co.uk.</p>
                        <p><b>ORDER DETAILS</b></p>
                        <p>Order Number: '.$transactionId.'</p>
                        <p>Placed on '.date('l dS F Y', strtotime($paymentDate)).'</p>
                        <p>Order Total:  £ '.$originalAmount.'.00</p>
                        <p>Coupon Used: '.$couponCode.'</p>
                        <p>Total After Discount: £ '.$amount.'.00</p>
                    </td></tr>'.foreach($orders as $order_real){ $order_real['transaction_id'] }.'<tr><td height="10" colspan="5"></td></tr>
                    <tr><td height="1" bgcolor="#777777" colspan="5"></td></tr>
                    <tr><td height="10" colspan="5"></td></tr>
                    <tr><td colspan="5"><p>Sending luck, good health and best regards as always<br>XXXX team</p></td></tr>
                    <tr><td height="10" colspan="5"></td></tr>
                    </table>
                    </body>
                    '
]);

如果我删除 foreach($orders as $order_real){ ... } 代码可以正常工作,但是当我像下面这样包含它时返回 HTTP ERROR 500,也许它是串联?怎么办?

提前谢谢你

【问题讨论】:

  • foreach() 不返回任何内容,您不能在这样的字符串连接中使用它。
  • 我知道它只是一个演示代码,我愿意将一些获取的数据放在一个 结构中......如何使用它?如何连接?
  • 在函数参数之外构建 HTML,您可以更好地控制它 - 并使用 foreach 单独构建一个变量并将其作为另一个普通变量包含在内。在一段/一行代码中做太多事情会变得比需要的更复杂。
  • 谢谢它现在几乎可以工作了,有趣的是我两年前做了很多类似的东西,现在我什么都不记得了......也许是因为我每天工作 16 个小时,每周 6 天,哈哈

标签: php api mailgun


【解决方案1】:

foreach() 不是一个函数——它不返回任何东西,所以你不能在这样的字符串连接中使用它。您可以使用 .= 运算符为循环的每次迭代不断地将 HTML 附加到变量:

$html = '... lots of HTML ... ';
foreach (...) {
    $html .= '... more HTML ...';
}
$html .= '... some more HTML ...';

【讨论】:

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签