-1

I am working on a php script that collects data entered in html form and stores that data in array. When user press submit button the php script is called to capture that data store in array and save into CSV file and if all this has gone well there is a header(location:'') redirect to confirmation page.

Now in the mix of all that I also waht to send out a confirmation email to the user that registered, what i have done is created a file template.php that has a nice html template, and I am added a mail() function to do so.

But I get an error saying:

19th February 2014 Warning: Cannot modify header information - headers already sent by (output started at /home/content/24/12131124/html/php/template.php:36) in /home/content/24/12131124/html/php/form_to_csv.php on line 196 

template.php:

<?php

function getMailContent(){

$subject = "OPES Academy- Workshop confirmation";
$message = "
<body style='background-color: #eeeeee; margin: 0 auto; font-family: 'lato',sans-serif'>

<table style='background-color:#ffffff' width='600' heigth='auto' cellspacing='0' cellpadding='0' align='center'>
    <tr>
        <td>
            <table width='600' style='background-color: #5e8ab5;' align='center' cellpading='0' cellspacing='0'>
                <tr>
                    <td>
                        <p style='padding-left: 20px;'><img src='http://opesacademy.com/emails/images/logo.png'
                                                            width='100' alt='Opes Academy'></p>
                    </td>
                    <td style='text-align: right; padding-right: 10px; color: #ffffff'>
                        KNOWLEDGE | WEALTH | POWER
                    </td>
                </tr>
            </table>
        </td>
    </tr>

    <tr>
        <td style='padding: 10px;'>

            <h1 class='skinytxt text-center txtblue'>Thank you for reserving your place</h1>

                    <p>&nbsp;</p>

                    <p class='txt-white text-center'>Thanks for your registration, we will be looking forward to see you at the";

                     ?>
                     <?php
                        require('helper.php');
                        echo ConvertDate( $_SESSION['date'] );
                    ?>
                    <?php
 $message.="            
                    <p align='center'>Address: 6 Thomas More Square, London, E1W 1XZ</p>

                    </p>

                    <p class='txt-white text-center'>If you have any more questions we will be glad to help you, just call us on 020 3675 9000 or email us on
                        support@opesacademy.com</p>

        </td>
    </tr>

</table>

<table width='600' style='background-color: #5e8ab5;' align='center' cellpading='0' cellspacing='0'>
    <tr>
        <td>
            <p style='padding-left: 10px; padding-right: 10px; font-size: 10px'>Trading and investing often
                involves a very high degree of risk. Past results are
                not indicative of future returns and financial instruments can go down as well as up
                resulting
                in you receiving less than you invested. Do not assume that any recommendations, insights,
                charts, theories, or philosophies will ensure profitable investment. Spread betting, trading
                binary options and CFD's carry a high risk to your capital, can be very volatile and prices
                may
                move rapidly against you. Only speculate with money you can afford to lose as you may lose
                more
                than your original deposit and be required to make further payments. Spread betting may not
                be
                suitable for all customers, so ensure you fully understand the risks involved and seek
                independent advice if necessary</p>
        </td>
    </tr>
</table>

</body>";

$headers = "Content-type: text/html\r\n";

return compact('subject', 'message', 'headers');
}
?>

form_to_csv.php

$to = $data['email'];
require('template.php');
$mailContent = getMailContent();

//csv
if(@$_POST['land']=='fw'){
    $path='/home/content/24/12131124/html/files/Admin/CSV_Binary/';
    $fName=$path.'free_workshop-'.date( "F_j_Y" ).".csv";
    //mail($to, $subject, $message, $headers,"-f info@opesacademy.com");
  mail($to, $mailContent['subject'], $mailContent['message'], $mailContent['headers'],"-f info@opesacademy.com");


}

    if(@$_POST['land']=='fw')header("Location: http://www.o.com/free/b/confirmation.php?camp=".$data['camp']);
    else header("Location: http://www.o.com/free/f/confirmation.php?camp=".$data['camp']);

So looking at the error I understand there is some sort of problem with the header as both mail() function uses header and at the redirect I am using a header but how to solve why is this a problem..?

Shibu Thomas
  • 3,148
  • 2
  • 24
  • 26
Tomazi
  • 781
  • 9
  • 27
  • 46

3 Answers3

3

It's to do with the echo in template.php. You want to concatenate (period) rather than echo. If you echo, you're starting the output and so can't edit the header information later.

Michael
  • 41,989
  • 11
  • 82
  • 128
  • Thank you for your answer, but I dont fully understand what u mean, in template.php i echo a function from helper.php script which i really need, is they any other way to do this without echo...? – Tomazi Feb 03 '14 at 12:39
  • ok could you show me this using my live code – Tomazi Feb 03 '14 at 13:16
1

Upon your request, I think it should look something like this :

<?php
require('helper.php');

function getMailContent(){

$subject = "OPES Academy- Workshop confirmation";
$message = "
<body style='background-color: #eeeeee; margin: 0 auto; font-family: 'lato',sans-serif'>

// ... Blah Blah Blah   
// ...looking forward to see you at the";

 $message .= ConvertDate( $_SESSION['date'] );                  
 $message.="            
                    <p align='center'>Address: 6 Thomas More Square, London, E1W 1XZ</p>

 //... Blah Blah Blah   

</body>";

//... Rest of the code...

?>
Boris Milner
  • 111
  • 2
  • 10
  • ok thank you v.much for your help just one thing I had to change – Tomazi Feb 03 '14 at 14:21
  • I also had to add `$message.= require('helper.php') beofre $message.=ConvertDate($_SESSION['date']);` . There was no errors now just waiting to c if the emial will come into my inbox unfortinaly i am using my server mail services not SMTP so it might take some time – Tomazi Feb 03 '14 at 14:23
  • I don't think `$message.= require('helper.php')` before `$message.=ConvertDate($_SESSION['date']);` is necessary since you require it the first thing after opening ` – Boris Milner Feb 03 '14 at 15:43
0

Why do you have the following code inside your function?

<?php
    require('helper.php');
    echo ConvertDate( $_SESSION['date'] );
?>

You can't echo into $message because echoing refers to the content being sent to the users browser (as Michael mentioned).

try $message .= ConvertDate( $_SESSION['date'] );

Boris Milner
  • 111
  • 2
  • 10
  • this code adds the date to the email that the user selected on the form, – Tomazi Feb 03 '14 at 12:35
  • I see, so as Michael stated above : "If you echo, you're starting the output and so can't edit the header information later." You can't `echo` into `$Message` because echoing refers to the content being sent to the users browser. – Boris Milner Feb 03 '14 at 12:37
  • so how can i solve this...? – Tomazi Feb 03 '14 at 12:42
  • I've edited my reply. As Michael said, you want to concatenate the returned value from _ConvertDate_ which is done with a period. Also, I don't think you need to close and re-open the – Boris Milner Feb 03 '14 at 12:46
  • I'm new to StackOverflow so I'm not sure how the editing thingie works, just in case - I've edited my reply again and changed **+=** to **.=** as mentioned here : http://stackoverflow.com/questions/11441369/php-string-concatenation – Boris Milner Feb 03 '14 at 12:52
  • ok could you show me this using my live code – Tomazi Feb 03 '14 at 13:14
  • Yes - I couldn't paste it here, so I've pasted it as a separate reply. – Boris Milner Feb 03 '14 at 13:25