将PHP中的大字符串组合用于HTML电子邮件

时间:2016-08-01 14:21:25

标签: php html json email

我正在处理一个项目,在那里我收到一个JSON数据的webhook,然后我将根据JSON发送电子邮件通知。我已经完成了JSON的所有排序。我知道如何通过PHP发送HTML电子邮件,但我迷失了如何构建这么大的电子邮件。

我需要使用一些逻辑来构建HTML电子邮件的一部分。但我知道这样的事情会失败,因为我试图在变量中使用 If 语句......

    $email = $vendorEmail; 
    $email_subject = "Order Slip ";
    $email_message = '
                   <table style="width:100%;">
                      <tbody>
                        <tr>
                          <td style="width:33%;">
                            <h5>Bill To:</h5>
                            <p style="font-size: 14px;">
                              <strong>' . $orderInfo->billing_address->first_name . ' ' . $orderInfo->billing_address->last_name . '</strong><br/>
                              ' . if(isset($orderInfo->billing_address->company)){$orderInfo->billing_address->company };. '<br>
                              ' . $orderInfo->billing_address->address1 . '<br/>
                              ' . $orderInfo->billing_address->address2 . '<br/>
                            </p>
                          </td>

这是我整个电子邮件的一小部分。电子邮件中的逻辑将变得更加复杂。一个示例是运行 for 语句来运行已完成订单的所有行项目。

是否有创建更大更复杂的HTML电子邮件的标准方法?或者,如果没有,有没有人建议更聪明的方式来解决这个问题?

2 个答案:

答案 0 :(得分:2)

你的问题不是关于撰写电子邮件,而是关于组合更大的字符串 - 这可能有助于更好地重命名问题,以便吸引“正确”的答案。

如果你唯一的问题是在变量中使用if / else,你可以使用两种方法。

首先使用普通的if / else子句,并使用。= 运算符

添加到变量
$string = 'xxx'
if($a) {
    $string .= 'yyy';
}
else {
    $string .= 'zzz';
}

或者使用三元运算符直接定义变量。第二个选项的输出与第一个选项的输出完全相同。

$string = 'xxx' . ($a ? 'yyy' : 'zzz');

Ofc你可以将这两种方法结合起来。

有关三元运算符的更多信息:http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary

编辑: 把它放到上下文中,你的代码应该是这样的(我会在这段代码中推荐的三元方法)

$email = $vendorEmail;
$email_subject = "Order Slip ";
$email_message = '
    <table style="width:100%;">
        <tbody>
            <tr>
                <td style="width:33%;">
                    <h5>Bill To:</h5>
                    <p style="font-size: 14px;">
                        <strong>' . $orderInfo->billing_address->first_name . ' ' . $orderInfo->billing_address->last_name . '</strong><br/>
                        ' . ((isset($orderInfo->billing_address->company) ? $orderInfo->billing_address->company : NULL). '<br>
                        ' . $orderInfo->billing_address->address1 . '<br/>
                        ' . $orderInfo->billing_address->address2 . '<br/>
                    </p>
                </td>

EDIT2: 在处理非常大的 HTML字符串时,我也使用输出缓冲方法,您可能会发现它也很有用。

<?php
ob_start();
$phpVariables = 'phpVariables';
?>
HERE GOES LARGE HTML, <?=$phpVariables?>, or <?php echo 'chunks of PHP that output stuff'?>
<?php
$string = ob_get_clean();
?>
然后

$ string将“在这里输出大量的HTML,phpVariables或者输出内容的PHP块。”。

当然你可以使用普通的php代码,包括输出中的if / else。

如果你想使用这种方法,我建议你先熟悉输出缓冲http://us2.php.net/manual/en/function.ob-start.php

答案 1 :(得分:0)

您可以在大字符串中使用if语句,但使用较短的版本,例如:

echo "-text-" . ▼                                 ▼
     "Name: " . ( ( isset( $name ) ) ? $name : "" ) .
     "-text-";

使用时&#34;短时间&#34;记得把它全部括在括号中(用箭头▼表示)。现在,在您的代码中,替换当前的if

if(isset($orderInfo->billing_address->company)){$orderInfo->billing_address->company }; .

by:

( (isset($orderInfo->billing_address->company)) ? $orderInfo->billing_address->company : "" ) .