PHP发送确认电子邮件错误

时间:2014-02-03 12:08:33

标签: php

我正在开发一个php脚本,它收集以html格式输入的数据并将该数据存储在数组中。当用户按下提交按钮时,调用php脚本以捕获数组中的数据存储并保存到CSV文件中,如果所有这些都很顺利,则会有一个标题(位置:'')重定向到确认页面。

现在,我还要向注册的用户发送一封确认电子邮件, 我所做的是创建了一个文件template.php,它有一个很好的html模板,我添加了一个mail()函数来实现。

但是我收到一个错误说:

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']);

所以看看错误,我理解标题存在某种问题,因为mail()函数都使用标题,在重定向我使用标题,但如何解决为什么这是一个问题..?

3 个答案:

答案 0 :(得分:3)

这与template.php中的echo有关。你想连接(句点)而不是echo。如果您回显,则表示您正在开始输出,因此无法在以后编辑标题信息。

答案 1 :(得分:1)

根据您的要求,我认为应该是这样的:

<?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...

?>

答案 2 :(得分:0)

为什么函数中包含以下代码?

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

您不能echo进入$message,因为回显是指发送给用户浏览器的内容(正如迈克尔所说)。

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