dsajkdadsa14222 2016-08-01 14:21
浏览 44
已采纳

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

Im working on a project where I receive a webhook of JSON data that I will then send an email notification based on the JSON. I have all the sorting of the JSON worked out. I know how to send an HTML email via php, but I'm lost on how to build such a large email.

I will need to use some logic to build out parts of the HTML email. But I know something like this will fail because I'm trying to use an If statement inside a variable...

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

This is a small section of my overall email. The logic will become much more complex further in the email. An example would be running a for statement to run through all the lines items of a completed order.

Is there a standard way of creating larger more complex HTML emails? Or if not, Does anyone have a suggestion on the smarter way to go about this?

  • 写回答

2条回答 默认 最新

  • duanlvxing7707 2016-08-01 14:27
    关注

    Your question is not about composing emails, but about combining larger strings - it might help to rename the question better so it attracts the "correct" answers.

    If your only problem is using if/else inside a variable you can use two approaches.

    First uses normal if/else clause with adding to a variable using the .= operator

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

    OR use ternary operators to straightforward define the variable in one go. Output of the second option is exactly the same as the first one.

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

    Ofc you can combine these two approaches.

    More about ternary operators: http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary


    EDIT: to put it into context, your code should look like this (ternary approach which I would recommend in this piece of code)

    $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: When dealing with really large HTML strings I also use output buffering approach, you might find it useful too.

    <?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 would then be "HERE GOES LARGE HTML, phpVariables, or chunks of PHP that output stuff.".

    Of course you can use normal php code, including your if/else in the output.

    If you want to use that approach though, I suggest you first get familiar with output buffering http://us2.php.net/manual/en/function.ob-start.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • dongwo2772 2016-08-01 14:44
    关注

    You can use if statements in your large strings but using the shorter version, example:

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

    When using this "short if" remember to enclose it all in parenthesis (pointed by arrows ▼). Now, in your code, replace your current if :

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

    by :

    ( (isset($orderInfo->billing_address->company)) ? $orderInfo->billing_address->company : "" ) .
    
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 PCDN如何使用宽带的电视业务通道拨号叠加带宽?
  • ¥15 遇到这种校园宽带网络应该怎么样解决?
  • ¥30 AXI VIP验证多余打印问题
  • ¥15 利用加权最小二乘法求某品牌手机价格指标,已按照总销量计算出权重,各类型号手机价格已知,如何求得价格指标?
  • ¥15 如何自制一个硬件钱包,有兴趣的朋友一起交流
  • ¥15 (关键词-聊天软件)
  • ¥15 求大家看看这个编程的编法没有思路啊
  • ¥20 WSL打开图形化程序子窗口无法点击
  • ¥15 Jupyter Notebook 数学公式不渲染
  • ¥20 ERR_CACHE_MISS 确认重新提交表单