I have a form on my site which submits to PHP using POST
. Among other things, PHP then sends a mail using PHPMailer with SMTP. Determining if the mail was successful is done using $success = $mail->send();
. Then based on that result, I want to save certain details in a cookie.
When I come to set the cookie with setcookie($cName, $cVal, time()+(60*60*24*365), "/", "", 0);
, I get the error telling me Cannot modify header information - headers already sent by...
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.
Is there an option in PHPMailer that will send the mail without writing headers back to the client? I thought perhaps $mail->SMTPDebug = 2;
might be causing the header to be sent but I changed it to 0 and still the same issue.
Can I get setcookie() to only update the header information and not actually send the header?
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?