2016-08-01 93 views
-1

我正在接收JSON数据的webhook项目,我将发送基于JSON的电子邮件通知。我已经完成了JSON的所有排序。我知道如何通过PHP发送HTML电子邮件,但我迷失在如何构建如此庞大的电子邮件。在PHP中为HTML电子邮件组合大字符串

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

$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> 

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

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

+0

您的代码更改为使用PHP作为模板语言。我认为这很容易。 https://eval.in/615346 –

回答

2

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

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

首先使用正常的,如果使用所述。=操作者

$string = 'xxx' 
if($a) { 
    $string .= 'yyy'; 
} 
else { 
    $string .= 'zzz'; 
} 

,或者使用三元运营商直截了当一气呵成定义变量添加到一个可变/ else子句。第二个选项的输出与第一个选项完全相同。

$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> 

编辑2: 当处理真的很大 HTML st戒指我也使用输出缓冲的方法,你可能会发现它也很有用。然后

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

$字符串将是 “在这里不用大的HTML,phpVariables,或者PHP是输出的东西块。”

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

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

+0

感谢您提出改变问题的建议。它已被编辑,希望其他人能够使用您的有用信息! – Brendin

+0

@brendin如果我的答案帮助你解决了问题,请将其标记为“已接受的答案”。如果没有,请让我知道还有什么你不明白,所以我可以帮你;) – Kalko

+0

@Brendin我编辑了我的答案,并添加了一个选项,可能对你有用 - 输出缓冲。 – Kalko

0

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

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

当使用这个“short if”时,记得把它全部放在圆括号中(用箭头▼指出)。现在,在你的代码,替换当前if

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

由:

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