if语句在html电子邮件中生成已发送错误的标头

时间:2014-05-18 08:32:18

标签: php wordpress email

我正在尝试在wordpress cms中创建一个小的html电子邮件系统。下面给出的是我目前正在使用的代码的精简版本。它基本上抓取输入值并通过电子邮件发送给所需的收件人。在我尝试在代码中实现if语句之前,一切正常。请看看中间的php块。

  $to_whom = strip_tags($_POST ['client_mailid']);
  $to = $to_whom; // no spaces

  $subject = 'some subject here';

  $message = '
  <!DOCTYPE html.....
  <head>head stuff here</head>
  <body>
  <table>
  <tr><td><table>some stuff here</table></td></tr>


  ';?>
  <?php if($_POST['client_input'] !== '') : ?>
  <?php
  '<tr><td>'.$_POST['client_input'].'</td></tr>';?>
  <?php endif;?>
  <?php
  '<tr>
  </table>
  </body>
  </html>
  ';


  $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
  mail($to, $subject, $message, $headers);
  $httpReferer = $_SERVER['HTTP_REFERER'];
  header('Location: '. $httpReferer );

运行时,会在下面抛出一个警告并忽略重定向,在上面的代码末尾显示。以下警告中提到的第37行包含<?php if($_POST['client_input'] !== '') : ?>。上面的代码位于名为 single_email.php 的单独文件中,并放在电子邮件文件夹下的wordpress theme-directroy中。

  

警告:无法修改标头信息 - 已发送的标头   (输出始于   /home2/xxxx/public_html/xxxxxxxxxxx.com/wp-content/themes/xxxxx/email/single_email.php:37)

那么这里发生了什么,也许我的php语法错了?提前谢谢。

1 个答案:

答案 0 :(得分:1)

输出后,您无法呼叫headerSee this for more information。在这种情况下,从第37行开始的输出打破了它,我猜测?><?php<?php '<tr>...之间的空格 - 这不是正确的语法。用以下内容替换整个$message声明:

  $message = '
  <!DOCTYPE html.....
  <head>head stuff here</head>
  <body>
  <table>
  <tr><td><table>some stuff here</table></td></tr>';

  // if ($_POST['client_input']!=='') add input ...
  $message .= ($_POST['client_input'] !== '' ? '<tr><td>'.$_POST['client_input'].'</td></tr>':'');

  $message .= '
  </table>
  </body>
  </html>
  ';