PHP 确认邮件

Posted

技术标签:

【中文标题】PHP 确认邮件【英文标题】:PHP confirmation email 【发布时间】:2014-01-31 14:06:46 【问题描述】:

我的网站上有一个注册表单,完成后详细信息存储在 CSV 文件中,但我还想向用户发送一封确认电子邮件。问题是电子邮件到达空白,我所做的是创建了一个 template.php 文件,其中包含我要发送的电子邮件结构,在此模板结构中,我具有来自其他文件的功能,用于确定注册日期。 template.php 中的模板包含在一个函数中,我在 from_to_csv.php 中调用它作为 mail() atrebiutes 的一部分:

希望这是有道理的,你们理解我看看代码:

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");



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

【问题讨论】:

您是否尝试在浏览器窗口中打印template.php 的输出? 在我看来你已经用你自己的 mail() 函数重新声明了 PHP 的 mail() 函数。这不应该引发关于尝试重新声明 mail 的错误吗? 没有错误,只是电子邮件我空白,但如果我将电子邮件模板代码复制并粘贴到 form_to_csv 中。 php它的工作原理 【参考方案1】:

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

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

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

// formerly your mail function, renamed to avoid function name ***
// 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"
);
?>

【讨论】:

我应该在哪里添加 compact() 在 template.php 上,如下所示:$send = compact($header, $message, $subject) 和 form_to_csv ma​​il($to, $send).. ..? 我添加了一个小例子来演示它。 我已按照您的指示进行操作,请检查我的问题中的编辑,我收到此错误消息: 我们期待很快见到您警告:无法修改标头信息 - 标头已由(输出开始于 /home/content/24/12131124/html/php/template.php:36)发送/home/content/24/12131124/html/php/form_to_csv.php 在第 201 行 不确定这个错误来自哪里。如果您注释掉 include 语句并将其包装在 PHP 标记中,我发布的示例应该立即可用。【参考方案2】:

本地变量(在本例中为 $message 和 $header)不能在本地函数之外访问。这称为“可变范围”,您应该多阅读一些内容(此处:http://www.php.net/manual/en/language.variables.scope.php)。

在这种情况下,$message 和 $header 是在本地范围内声明的:函数 mail(),当您调用 PHP Mail 函数时,您正试图在全局范围内访问它们。

您必须将其从您的函数中传回,以便在 form_to_csv.php 文件中访问它们。

【讨论】:

【参考方案3】:

看起来您正试图从 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 info@opesacademy.com");


【讨论】:

这看起来很有趣我会尝试但只是为了确认 $message = 正是 html 模板代码正确...?所以函数 myMail() 'function myMail() $message = "

HELLO USER

" '
是的,本质上是更改函数名称,这样它就不会发生冲突,只需将返回数组放在底部【参考方案4】:

一个超级简单的解决方法就是让 template.php 不是一个函数。

删除该行

function mail()

然后是大括号


你的脚本会运行得很好。

查看http://www.php.net/manual/en/language.variables.scope.php了解更多信息

【讨论】:

以上是关于PHP 确认邮件的主要内容,如果未能解决你的问题,请参考以下文章

php表单邮件:显示确认页面后发送邮件

php 联系表格7的电子邮件确认

PHP 如何使用电子邮件确认对注册表单进行编码

php WordPress - 当管理员更改其电子邮件地址时,禁用确认通知

用户注册后如何发送验证/确认电子邮件

确认邮件未发送