title: How can I perform several actions in PHP and only send headers back to the client once?
body: <p>I have a form on my site which submits to PHP using <code>POST</code>. Among other things, PHP then sends a mail using PHPMailer with SMTP. Determining if the mail was successful is done using <code>$success = $mail->send();</code>. Then based on that result, I want to save certain details in a cookie.</p>
  
  <p>When I come to set the cookie with <code>setcookie($cName, $cVal, time()+(60*60*24*365), "/", "",  0);</code>, I get the error telling me <code>Cannot modify header information - headers already sent by...</code></p>
  
  <p>PHPMailer works as expected - an email is sent. When I comment out the PHPMailer line, setcookie() works as expected - a cookie is set. I'm guessing that both PHPMailer and setcookie() automatically send headers back to the client.</p>
  
  <p>Is there an option in PHPMailer that will send the mail without writing headers back to the client? I thought perhaps <code>$mail->SMTPDebug = 2;</code> might be causing the header to be sent but I changed it to 0 and still the same issue.</p>
  
  <p>Can I get setcookie() to only update the header information and not actually send the header? </p>
  
  <p>Basically, I want PHP to perform all its actions before any headers are sent back to the client. Can I take control of when the headers are sent back? ...and while we're at it, the actual information that's sent back?</p>