简体   繁体   English

PHP确认电子邮件

[英]PHP confirmation email

I have a registration form on my website, Upon completion the details are stored on CSV file but I also want to send a confirmation Email to the User. 我的网站上有注册表格,完成后,详细信息存储在CSV文件中,但我也想向用户发送确认电子邮件。 The issue is that the email arrives blank, what i have done is created a template.php file that contains the email structure that i want to send out, within this template structure i have function from other file that determines the registration date. 问题是电子邮件到达空白,我所做的是创建了一个template.php文件,其中包含我想发送的电子邮件结构,在此模板结构中,我有来自确定注册日期的其他文件的功能。 the template within template.php is wrapped within a function which i call in from_to_csv.php as part of mail() atrebiutes: template.php中的template.php包含在一个函数中,我调用from_to_csv.php作为mail()atrebiutes的一部分:

Hopefully this makes sense and you guys understand me have a look at the code: 希望这是有道理的,你们明白我看看代码:

template.php : 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 : 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");


}

$to variable contains the email that the user has inputed into the form..... $to variable包含用户输入表单的电子邮件.....

Your mail method does not return a thing. 您的邮件方法不会返回任何内容。 Please add compact('headers', 'message', 'subject'); 请添加压缩('标题','消息','主题');

Then use the returned array in your other function. 然后在其他函数中使用返回的数组。

<?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 info@opesacademy.com"
);
?>

Local variables (in this case $message and $header) can't be accessed outside of the local function. 无法在本地函数之外访问局部变量(在本例中为$ message和$ header)。 This is called 'variable scope' and you should do some more reading on it (Here: http://www.php.net/manual/en/language.variables.scope.php ). 这被称为“变量范围”,您应该对其进行更多阅读(此处: http//www.php.net/manual/en/language.variables.scope.php )。

In this case $message and $header are being declared in the Local Scope: function mail() and you are trying to access them in the Global Scope when you call the PHP Mail function. 在这种情况下,在本地作用域:函数mail()中声明了$ message和$ header,并且当您调用PHP Mail函数时,您正试图在全局作用域中访问它们。

You must pass this back out of your function in order to access them in the form_to_csv.php file. 您必须将其从函数中传回,才能在form_to_csv.php文件中访问它们。

It looks like you are trying to access the $subject, $message and $headers variables from within your Mail() function, the will not be accessible unless you return them first... try this, In your template PHP put: 看起来你试图从你的Mail()函数中访问$ subject,$ message和$ headers变量,除非你先返回它们,否则将无法访问...试试这个,在你的模板中PHP放:

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


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

In your main file: 在您的主文件中:

$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 info@opesacademy.com");

}

A super easy fix for this would be to just make template.php NOT a function. 一个非常简单的解决方法就是让template.php不是一个函数。

delete the line 删除该行

function mail(){

and then the ending curly bracket 然后是结束的花括号

}

and your script will run just fine. 并且你的脚本运行得很好。

Check http://www.php.net/manual/en/language.variables.scope.php for more information 有关更多信息,请查看http://www.php.net/manual/en/language.variables.scope.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM