如何在MailGun API中使用php foreach循环

时间:2017-06-20 20:15:16

标签: php api mailgun

我在google VM Instance上安装了mailgun,很快就知道我无法在外发端口(25,587等等)上向我的客户发送帐单邮件,所以我注册了MailGun,我创建并购买了基本计划...

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

$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,代码可以正常工作吗?怎么做?

提前谢谢

1 个答案:

答案 0 :(得分:2)

foreach()不是函数 - 它不会返回任何内容,因此您不能在字符串连接中使用它。您可以使用.=运算符在循环的每次迭代中将HTML连续附加到变量:

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