PHP:从.txt文件加载一个html模板,并在发送电子邮件之前替换标记


PHP: Loading a html template from a .txt file and replacing the tag before sending an Email

我目前正在我的网站上创建一个很好的联系人表单,在获取html模板时遇到了一些问题。

模板本身是一个.txt文件,包含经过验证的HTML。在这个html中有一个标签

     [email_content]. 

这个标签应该替换为电子邮件的内容,该内容是从我的网站上的联系表格中生成的:

     <form class="form-horizontal" id="contact_form" name="contact_form">
        <div id="form_name_ctrl_group" class="control-group">
            <input class="input-xlarge" type="text" id="inputName" name="inputName" placeholder="Name">
        </div>
        <div class="control-group">
            <input class="input-xlarge" type="text" id="inputEmail" name="inputEmail" placeholder="Email">
        </div>
        <div class="control-group">
            <textarea class="input-xlarge" id="inputMessage" name="inputMessage" placeholder="Insert message here..."></textarea>
        </div>
        <button type="submit" class="btn btn-sky">Send</button>
        <button type="button"  class="btn btn-sky">Reset</button>
    </form>

提交后,此表单将使用jQueryvalidate进行验证,然后使用serialize函数通过jQueryajax进行提交。然后将序列化的数据发送到以下php函数以供使用:

<?php
    function mailer_send($mailer_recipient, $mailer_subject, $mailer_message){
        $mailer_headers = 'From: webmaster@example.co.uk' . "'r'n" .
        'X-Mailer: PHP/' . phpversion() . "'r'n"
        'MIME-Version: 1.0'r'n' . "'r'n" 
        'Content-Type: text/html; charset=ISO-8859-1'r'n';
        mail($mailer_recipient, $mailer_subject, $mailer_message, $mailer_headers);
     }
$name = $_POST['inputName'];
$email = $_POST['inputEmail'];
$message = strip_tags($_POST['inputMessage']);
$template = file_get_contents('email_templates/template.txt');
$template = str_replace('[email_content]',$message, $template);

     mailer_send('enquiries@example.co.uk','Test Email',$template);
 ?>

正如你所看到的,我正试图用用户在联系人表单中输入的消息替换html模板中的标记[email_content]。然后应该使用该模板向我的电子邮件帐户发送电子邮件。我目前的问题是我实际上没有收到任何东西。

脚本在没有模板部分的情况下工作(如果我在mailer_send中使用$message),那么这里可能出了什么问题?

我的目录结构如下:

       /fnc
           mailer.php
           /email_templates
                template.txt

附加:我在服务器日志中收到以下错误消息:

   [19-Jul-2013 13:00:43 UTC] PHP Parse error:  syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /home/username/public_html/example/fnc/mailer.php on line 5

您缺少一些字符串串联运算符,请查看错误消息:PHP Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /home/username/public_html/example/fnc/mailer.php on line 5

function mailer_send($mailer_recipient, $mailer_subject, $mailer_message){
    $mailer_headers = "From: webmaster@example.co.uk" . "'r'n" .
    "X-Mailer: PHP/" . phpversion() . "'r'n" .
    "MIME-Version: 1.0'r'n" .
    "Content-Type: text/html; charset=ISO-8859-1'r'n";
    mail($mailer_recipient, $mailer_subject, $mailer_message, $mailer_headers);
 }