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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?