0

I am trying create a little html email system in a wordpress cms. Given below is the stripped down version of the code I am having currently. It basically grabs the input values and emails them to desired recipients. It was all working fine till I tried to implement an if statement in the code. Please take a look at the php block in the middle.

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

When run this throws an warning below and ignores the redirection, apparent at end of code above. Line 37 as mentioned in warning below contains <?php if($_POST['client_input'] !== '') : ?>. The above code is in a seperate file named single_email.php and is put in a wordpress theme-directroy under the folder email.

Warning: Cannot modify header information - headers already sent by (output started at /home2/xxxx/public_html/xxxxxxxxxxx.com/wp-content/themes/xxxxx/email/single_email.php:37)

So what is going on here, perhaps my php syntax is wrong ? Thanks in advance.

gurung
  • 628
  • 2
  • 11
  • 33

1 Answers1

1

You can't call header after you any output. See this for more information. In this case the output that starts on line 37 is breaking it, I'm guessing the space between ?> and <?php, or <?php '<tr>... - which is not the right syntax. Replace the entire $message declaration with this:

  $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>
  ';
Community
  • 1
  • 1
Mark Miller
  • 7,442
  • 2
  • 16
  • 22
  • Does it meet the condition i intend which is if input not empty do this? – gurung May 18 '14 at 08:54
  • @gurung Yes ... if `$_POST['client_input'] !== ''` it adds that additional row. That's what the middle part is doing - it's called a ternary operator and is just shorthand for an if statement. – Mark Miller May 18 '14 at 08:57
  • Flawless. I need to read about these shorthands. – gurung May 18 '14 at 09:38