2014-01-31 211 views
0

我在我的网站上有一个注册表格,完成后详细信息存储在CSV文件中,但我也想发送确认电子邮件给用户。问题是电子邮件到达空白,我所做的是创建一个template.php文件,其中包含我想要发送的电子邮件结构,在此模板结构中,我具有决定注册日期的其他文件的功能。PHP确认电子邮件

希望这个有意义,你们了解我看看代码:

的template.php内template.php模板,我在from_to_csv.php称作为邮件的一部分()atrebiutes函数内包裹

<?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 
         [email protected]</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 [email protected]"); 
    mail($to, $mailContent['subject'], $mailContent['message'], $mailContent['headers'],"-f [email protected]"); 


} 

$to变量包含用户输入表单的电子邮件.....

+0

它看起来像你对我已经重新声明PHP的'邮件()'函数与自己的'邮件()'函数。这不应该抛出一个关于尝试重新声明'mail'的错误吗? – crush

+0

您是否尝试在浏览器窗口中输出“template.php”的输出? –

+0

不是没有错误,只是电子邮件我抛出空白,但如果我复制并通过电子邮件模板代码到form_to_csv。 php它的工作原理 – Tomazi

回答

2

您的邮件方法不会返回任何东西。 请加上compact('headers','message','subject');

然后在您的其他函数中使用返回的数组。

<?php 
// you might place the following method into mail.php 

// formerly your mail function, renamed to avoid function name clash 
// with PHP's own function mail 
function getMailContent(){ 
    $subject = "OPES Academy- Workshop confirmation"; 
    $message = "Message"; 
    $headers = "Content-type: text/html\r\n"; 

    // return the things :) 
    return compact('subject', 'message', 'headers'); 
} 

// Usage: 

// from_to_csv.php 

// you need to include the file mail.php, 
// if you want to access the function getMailContent() 
// uncomment this line, if you split the PHP code into the files. 
// include 'mail.php';  

// fetch mail content as array 
$mailContent = getMailContent(); 

// access array values 
mail($to, 
    $mailContent['subject'], $mailContent['message'], 
    $mailContent['headers'], 
    "-f [email protected]" 
); 
?> 
+0

我应该在template.php中添加compact(),如下所示:$ send = compact($ header,$ message,$ subject)和form_to_csv ma​​il($ to,$ send)....? – Tomazi

+0

我已经添加了一个小例子来演示它。 –

+0

我有点按照你的指示,请检查编辑我的问题,我得到这个错误信息: – Tomazi

0

本地变量(本例中为$ message和$ header)不能在本地函数之外访问。这被称为'可变范围',你应该做更多的阅读(这里:http://www.php.net/manual/en/language.variables.scope.php)。

在这种情况下,在本地范围函数mail()中声明了$ message和$ header,并且您在尝试在调用PHP邮件函数时在全局范围中访问它们。

为了在form_to_csv.php文件中访问它们,必须将它传回您的函数。

0

看起来你试图从你的Mail()函数中访问$ subject,$ message和$ headers变量,除非你先返回它们,否则它将不可访问......试试这个,在你的模板PHP放:

function MyMail() { 
    // Your variables and content here 


    return Array(
    "subject" => $subject, 
    "message" => $message, 
    "headers" => $headers; 
    ); 
} 

在你的主文件:

$to = $data['email']; 

require('template.php'); 
$content = MyMail(); 

//csv 
if(@$_POST['land']=='fw'){ 
    $path=='/home/content/CSV/'; 
    $fName=$path.'free_workshop-'.date("F_j_Y").".csv"; 
    mail($to, $content["subject"], $content["message"], $content["headers"],"-f [email protected]"); 

} 
+0

这看起来很有趣我会尝试它,但只是为了确认$ message =完全正确的html模板代码正确...?所以功能myMail(){} – Tomazi

+0

“功能myMail(){$消息=‘

HELLO用户

’}” – Tomazi

+0

是的,本质上的变化的函数的名称,因此不会发生冲突,只是把返回数组中底端 –