Search code examples
phpapachephpmailerphp-7.2

PHP template if statement being ignored with PHPMailer


I'm trying to send an order confirmation email HTML template (order_confirmation.html.php) from my Apache (7.2) backend that has PHP tags present for conditional rendering that is sent with the phpmailer library (^6.0.2). I'm sending this through in the email body, but the php if statements aren't being interpreted as I'd expect. For example these lines from the template file render even though the $data['order_shipping'] key doesn't exist:

<? if ($data['order_shipping']) { ?>
<tr>
  <td>
      <div style="color:#808080;font-size:12px;line-height:20px;font-weight:bold;font-family:Arial,sans-serif;">Ship To</div>
      <div style="padding-bottom:3px;font-size:12px;color:#000;font-family:Helvetica,Arial,sans-serif;"><?= stripslashes($data['order_shipping']['address_line_1']); ?></div>
      <? if ($data['order_shipping']['address_line_2']) { ?>
        <div style="padding-bottom:3px;font-size:12px;color:#000;font-family:Helvetica,Arial,sans-serif;"><?= stripslashes($data['order_shipping']['address_line_2']); ?></div>
      <? } ?> 
      <div style="padding-bottom:3px;font-size:12px;color:#000;font-family:Helvetica,Arial,sans-serif;"><?= stripslashes($data['order_shipping']['city']).", ".stripslashes($data['order_shipping']['state'])." ".stripslashes($data['order_shipping']['zip']); ?></div>
      <div style="padding-bottom:3px;font-size:12px;color:#000;font-family:Helvetica,Arial,sans-serif;"><span style="font-weight:bold;">Phone:</span> <?= stripslashes($data['order_shipping']['phone']); ?></div>
      <div style="padding-bottom:3px;font-size:12px;color:#000;font-family:Helvetica,Arial,sans-serif;"><span style="font-weight:bold;">Requested ETA:</span> <?= stripslashes($data['order_shipping']['requested_eta']); ?></div>
  </td>
</tr>
<? } ?>

Here's the rendered email:

enter image description here

And my SystemMailer.php file that uses the PHPMailer library:

class SystemMailer extends Controller {
    
    var $container;
    
    function __construct($container) {
        $this->container = $container;
        parent::__construct($container);
        $this->mailer = new \PHPMailer\PHPMailer\PHPMailer;
    }

    function clearAddresses() {
        $this->mailer->clearAddresses();
    }
    
    function initEmail($email_config) {
        $this->log->debug("SystemMailer", "initEmail");
        $this->log->debug("SystemMailer", "adding address");
        $this->log->debug("SystemMailer", $email_config['to_email']);
        if (is_array($email_config['to_email'])) {
            foreach ($email_config['to_email'] AS $email) {
                $this->mailer->addAddress($email);
                $this->container->log->debug("addAddress", $email);
            }
        } else {
            $this->mailer->addAddress($email_config['to_email']);
        }
        
        if (is_array($email_config['bcc_email'])) {
            foreach ($email_config['bcc_email'] AS $email) {
                $this->mailer->addBCC($email);
            }
        } else {
            $this->mailer->addBCC($email_config['bcc_email']);
        }

        if ($email_config['from_name'] && $email_config['from_email']) {
            $this->mailer->setFrom($email_config['from_email'], $email_config['from_name']);
        } elseif ($email_config['from_email']) {
            $this->mailer->setFrom($email_config['from_email']);
        }
        if ($email_config['reply_to_name'] && $email_config['reply_to_email']) {
            $this->mailer->addReplyTo($email_config['reply_to_email'], $email_config['reply_to_name']);
        } elseif ($email_config['reply_to_email']) {
            $this->mailer->addReplyTo($email_config['reply_to_email']);
        }
        $this->mailer->isHTML(true); // Set email format to HTML
        $this->mailer->Subject = $email_config['subject'];
        // the following must be done in this order.
        // 1) add HTML content to email
        // 2) add TEXT content to email
        if ($email_config['html_content']) {
            $this->mailer->Body    = $email_config['html_content'];
        }
        if ($email_config['text_email']) {
            $this->mailer->AltBody = $email_config['text_email'];
        }
    }
    
    function sendEmail() {
        // smtp auth
        ob_start();
        $this->mailer->SMTPDebug    = 2;
        $this->mailer->isSMTP();                                                    // Set mailer to use SMTP
        $this->mailer->SMTPAuth     = true;                                         // Enable SMTP authentication
        $this->mailer->Host         = $this->config->get_value('smtp_host_1');      // Specify main and backup SMTP servers
        $this->mailer->Username     = $this->config->get_value('smtp_user_1');      // SMTP username
        $this->mailer->Password     = $this->config->get_value('smtp_password_1');  // SMTP password
        $this->mailer->SMTPSecure   = $this->config->get_value('smtp_encryption_1');  // SMTP encryption
        $this->mailer->Port         = $this->config->get_value('smtp_port_1');       // TCP port to connect to
        $success                    = $this->mailer->send();
        $output                     = ob_get_contents();
        ob_end_clean();

        if ( ! $success ) {
            $this->log->debug("SystemMailer", "Error sending mail");
            $this->log->debug("SystemMailer", $this->mailer->ErrorInfo);
            $this->log->debug("SystemMailer : output", $output);
            return false;
        } else {
            $this->log->debug("SystemMailer", "Sent mail");
            return true;
        }
    }

}

There are lines of php code that do get rendered. For example, for troubleshooting I've added this line to the email template; which shows up correctly in the email:

Changes: <?= count($data['changes']) ? "true" : "false" ?>

enter image description here


Solution

  • I am using Docker/docker-compose to containerize the app. So you want to add this line to the Dockerfile that is used to build the image:

    RUN sed -i "s/short_open_tag = Off/short_open_tag = On/" "$PHP_INI_DIR/php.ini"
    

    Use this GitHub link as reference