带有 HTML 模板和发送变量的 PHP Mailer [英] PHP Mailer with HTML template and sending variables

查看:25
本文介绍了带有 HTML 模板和发送变量的 PHP Mailer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我正在尝试这样做.

Basically I'm trying to do this.

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

这是我的代码

    <?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

我哪里出错了.我检查了一些论坛.但是没用.

Where did I went wrong. I have checked some forum. But no use.

我之前问过一些问题.但我的项目要求是让 html 模板带有 % 变量名 % 以便任何人都可以在不触及代码部分的情况下对 html 文件进行更改.

I have previously asked some question. But my project requirement is to have the html template with % variable name % so that anyone could make changes in the html file without touching the code part.

推荐答案

其中两件事情与其他事情不同......

Two of these things are not like the others...

$message = str_replace('%testusername%', $username, $message); 
$message = str_replace('%testpassword%', $password, $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 问题...

Your script is working perfectly as is, and it's a PEBKAC issue...

这篇关于带有 HTML 模板和发送变量的 PHP Mailer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆