25

Basically I'm trying to do this.

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

Here is my code

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> 

I'm getting the mail as follows:

    Account Details

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

Expected Output

        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.

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.

Alex Kulinkovich
  • 4,408
  • 15
  • 46
  • 50
user3350885
  • 739
  • 4
  • 16
  • 38
  • 2
    In your sample_mail.html put test before username and password. You just forgot the test part! – Jordan Jul 21 '14 at 15:40
  • excellent. helped a lot – user3350885 Jul 22 '14 at 09:52
  • 1
    Thanks for asking this question so nicely with code. Even though you had a typo in your variable names, I got my answer from your question (%fieldname% in template) as I didn't know how to transfer a form field from my registration form, into the emailer. – Mario Apr 30 '18 at 20:31

1 Answers1

26

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"

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

Marc B
  • 356,200
  • 43
  • 426
  • 500