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 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配