Manish Tiwari
Manish Tiwari

Reputation: 1866

how to pass multiple variable to html template Using PhpMailer?

I want to pass multiple variable to Mail template page and echo to that page using phpmailer library.

I have two variables to pass welcome.php page and echo there.

$name = 'example';
$email = '[email protected]';

I have use this code

 $mail->MsgHTML(str_replace('[emailhere]', $email, file_get_contents('welcome.php')), dirname(__FILE__));

Upvotes: 0

Views: 2111

Answers (2)

Sheidin
Sheidin

Reputation: 41

You may use output buffering.

Echo variables on your template file ($template): for ex:

<p><?=$name?></p>

Then include it and pass through output buffering

ob_start(); //Start output buffering
include('welcome.php'); //include your template file
$template = ob_get_clean(); //Get current buffer contents and delete current output buffer
$mail->msgHTML($template); // Add html content into your PHP MAILER class 

Upvotes: 1

jeroen
jeroen

Reputation: 91734

You can send arrays to str_replace to replace multiple values.

For example:

$message = str_replace(
    array(
      '[emailhere]',
      '[namehere]'
    ),
    array(
       $email,
       $name
    ),
    file_get_contents('welcome.php')
);

By the way, you should probably not give your template a php extension as it will parse any php if you call or include it directly. So that could be a security risk if users can modify templates.

Upvotes: 3

Related Questions