如何在 swiftmailer 中决定检查字段是否为空然后不发送 `td` 到邮件

how to make if decision in swiftmailer to check if field is empty then do not send `td` to mail

我正在尝试在提交表单后发送电子邮件,我想实现:

1) 如果字段为空则无需发送 table 行到邮件。就像下面的字段 age 是可选的,用户可能会添加 his/her 年龄,也可能不会,所以如何在 switmail $message->addPart('Message','text/html') 函数中进行。

我试过但失败了说: Parse error: syntax error, unexpected 'if' (T_IF) in... 问题仅在于 if.. 没有 if 声明一切正常。

$content = '<table>
   ...
   <tr><td>' . $_POST["firstname"] . '<td></tr>
   ' . if(!empty($_POST["age"])) {
          . '<tr><td>' . $_POST["age"] . '</td></tr>' .
       }
     ...
<table>';
$message->addPart($content, 'text/html');

$content 变量之外进行。

$age = (!empty($_POST["age"])) ? '<tr><td>' . $_POST["age"] . '</td></tr>' : '';

$content = '<table>
   ...
   <tr><td>' . $_POST["firstname"] . '<td></tr>'
   . $age . '
     ...
<table>';