本文介绍了如何在 swiftmailer 中决定检查字段是否为空然后不将`td`发送到邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to send email(s) after submitting a form, I want to achieve:

1) 如果字段为空,则无需将表格行发送到邮件.就像下面的字段 age 是可选的,用户可能会添加他/她的年龄,也可能不会,那么如何在 switmail $message->addPart 中添加('Message','text/html') 函数.

1) If field is empty then no need to send table row to mail. Just like the field age below is optional, user might add his/her age or might not, so how to do it in switmail $message->addPart('Message','text/html') function.

我试过但失败了说:解析错误:语法错误,意外的'if'(T_IF) in...问题仅在于 if.. 没有 if 语句一切正常.

I tried but failed saying:Parse error: syntax error, unexpected 'if' (T_IF) in...The issue is only with if.. without if statement everything works fine.

$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>';

这篇关于如何在 swiftmailer 中决定检查字段是否为空然后不将`td`发送到邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 12:00
查看更多