简体   繁体   中英

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 = 'example@mail.com';

I have use this code

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

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.

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM