【问题标题】:PHP: Loading a html template from a .txt file and replacing the tag before sending an EmailPHP:从 .txt 文件加载 html 模板并在发送电子邮件之前替换标签
【发布时间】:2013-07-19 21:35:44
【问题描述】:

我目前正在我的网站上创建一个不错的联系表单,但在使 html 模板正常工作时遇到了一些问题。

模板本身是一个包含经过验证的 HTML 的 .txt 文件。在这个 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>

提交后,此表单使用 jQuery validate 进行验证,然后使用 serialize 函数通过 jQuery ajax 提交。然后将序列化后的数据发送到下面的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

【问题讨论】:

  • 您是否检查过file_get_contents 是否正确读取了$template?
  • 我该怎么做?该过程是使用 ajax 运行的,所以我不能真正做回声。
  • 不需要。我服务器上的错误日志告诉我该功能是否正确执行。还是谢谢。

标签: php templates html-email


【解决方案1】:

您缺少一些字符串连接运算符,请查看您的错误消息: 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);
 }

【讨论】:

    猜你喜欢
    • 2014-04-17
    • 2010-11-29
    • 2020-06-12
    • 2019-06-11
    • 1970-01-01
    • 1970-01-01
    • 2011-03-04
    • 1970-01-01
    相关资源
    最近更新 更多