PHP Mailer与HTML模板和发送变量

基本上我试图这样做

http://www.xeweb.net/2009/12/31/sending-emails-the-right-way-using-phpmailer-and-email-templates/

这是我的代码

的index.PHP

<?PHP 
    include('class.PHPmailer.PHP'); // Retrieve the email template required 
    $message = file_get_contents('mail_templates/sample_mail.html'); 
    $message = str_replace('%testusername%',$username,$message); 
    $message = str_replace('%testpassword%',$password,$message); 
    $mail = new PHPMailer(); $mail->IsSMTP(); // This is the SMTP mail server 

    $mail->SMTPSecure = 'tls';
    $mail->Host = "smtp.gmail.com";
    $mail->Port = 587; 
    $mail->SMTPAuth = true; 
    $mail->Username = 'mygmailid@gmail.com'; 
    $mail->Password = 'mypassword'; 
    $mail->SetFrom('fromgmail@gmail.com','Pricol Technologies'); 
    $mail->AddAddress('addaddress@gmail.com'); 
    $mail->Subject = 'Your account information';
    $mail->MsgHTML($message);
    $mail->IsHTML(true); 
    $mail->CharSet="utf-8";
    //$mail->AltBody(strip_tags($message)); 
    if(!$mail->Send()) {  
    echo "Mailer Error: " . $mail->ErrorInfo;
    } 
    ?>

mail_templates / sample_mail.html

<html>
    <body>
    <h1>Account Details</h1>
    <p>Thank you for registering on our site,your account details are as follows:<br>
    Username: %username%<br>
    Password: %password% </p>
    </body>
    </html>

我收到邮件如下:

Account Details

    Thank you for registering on our site,your account details are as follows:
    Username: %testusername%
    Password: %testpassword%

预期产量

Account Details

        Thank you for registering on our site,your account details are as follows:
        Username: testusername
        Password: testpassword

我哪里错了我已经检查过一些论坛.但没有用.

我以前问过一些问题.但是我的项目要求是使用%变量名%的HTML模板,以便任何人都可以在html文件中进行更改,而不用触摸代码部分.

其中两件事不像别人那样
$message = str_replace('%testusername%',$message); 
$message = str_replace('%testpassword%',$message); 
                         ^^^^---note "test"


<p>Thank you for registering on our site,your account details are as follows:<br>
Username: %username%<br>
Password: %password% </p>
           ^---note the LACK of "test"

您的脚本正常工作,这是一个PEBKAC问题…

相关文章

Hessian开源的远程通讯,采用二进制 RPC的协议,基于 HTTP 传输。可以实现PHP调用Java,Python,C#等多语...
初识Mongodb的一些总结,在Mac Os X下真实搭建mongodb环境,以及分享个Mongodb管理工具,学习期间一些总结...
边看边操作,这样才能记得牢,实践是检验真理的唯一标准.光看不练假把式,光练不看傻把式,边看边练真把式....
在php中,结果输出一共有两种方式:echo和print,下面将对两种方式做一个比较。 echo与print的区别: (...
在安装好wampServer后,一直没有使用phpMyAdmin,今天用了一下,phpMyAdmin显示错误:The mbstring exte...
变量是用于存储数据的容器,与代数相似,可以给变量赋予某个确定的值(例如:$x=3)或者是赋予其它的变...