web-dev-qa-db-ja.com

PHP 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 = '[email protected]'; 
    $mail->Password = 'mypassword'; 
    $mail->SetFrom('[email protected]', 'Pricol Technologies'); 
    $mail->AddAddress('[email protected]'); 
    $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ファイルを変更できるようにすることです。

21
user3350885

これらの2つは他のものとは異なります...

$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の問題です...

22
Marc B