2014-01-31 209 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

是的,本質上的變化的函數的名稱,因此不會發生衝突,只是把返回數組中底端 –