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?
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
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 : "" ) .