Send email with dynamic content in PHP

1

I have a .html email template with + - 700 lines (created in Mailchimp ) and I need to insert some values retrieved from the database (similar to what Velocity does) before sending it to the client.

Although I do not need to enter so much dynamic data like this, I'm looking for some solution on how to do this without having to define the content in php (the string would be huge and would take many lines from my class), I'm using PHPMailer .

Here's an example of what I want to do, in case I put some {VAR} tags to identify where and how I want to change the value of html.

<!doctype html>
<html class="no-js" lang="">
    <head>
        <meta charset="utf-8">
        <title></title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">
    </head>
    <body>
        <p>Ol&aacute;, {USER}.</p>
        <a href="www.dominio.com/validate/{VALIDATE_KEY}" target="_blank">Clique aqui para ativar sua conta.</a>
    </body>
</html>
    
asked by anonymous 13.03.2015 / 21:55

3 answers

4

You can replace keywords in your template in a simple way using something like this:

<?php
   //Aqui você define as variáveis na lógica do seu script
   $nome = "Joaquim Augusto";
   $quantidade = "100";

   // Abre o template...
   $corpoDoEmail = file_get_contents('template.txt');

   //E troca as variáveis
   $corpoDoEmail = str_replace( '%NOME%', $nome,       $corpoDoEmail );
   $corpoDoEmail = str_replace( '%QTD%',  $quantidade, $corpoDoEmail );
?>

For HTML, you may prefer extra protection with htmlentities :

$corpoDoEmail = str_replace( '%NOME%', htmlentities( $nome ), $corpoDoEmail );


The template.txt would look something like this:

<p>Olá, %NOME%!</p>

<p>Você acaba de ganhar %QTD% pontos de reputação!</p>


In short, it would be enough to edit your template and define special symbols to avoid conflicts with other parts of the text.

Note that the % I chose as an example, nothing prevents you from inventing as you want, as long as there is no danger of matching text that should not be changed.

For example, if it were a template for Orkut: P

Olá, ^_NomeDoMiguxo_^, ...

$corpoDoEmail = str_replace( '^_NomeDoMiguxo_^', $nome      , $corpoDoEmail );
    
13.03.2015 / 22:07
3

Not the most beautiful alternative, but it can be used:

<?php

$nome = "Joaquim Augusto";
$validateKey = "auihr43qorehq3brhuq3dfiqawndi==";


$str = <<<EOF
<!doctype html>
<html class="no-js" lang="">
    <head>
        <meta charset="utf-8">
        <title></title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">
    </head>
    <body>
        <p>Ol&aacute;, $nome.</p>
        <a href="www.dominio.com/validate/{$validateKey}" target="_blank">Clique aqui para ativar sua conta.</a>
    </body>
</html>
EOF;

?>
    
13.03.2015 / 22:14
2

Mailchimp already comes with this feature, it's called Merge Vars. ( Mailchimp uses Mandrill as email delivery)

Merge vars are available through API or SMTP Headers.

Email example:

Dear *|FNAME|*,
    Thank you for your purchase on *|ORDERDATE|* from ABC Widget Company. 
We appreciate your business and have included a copy of your invoice below.

*|INVOICEDETAILS|*

Please let us know if you have further questions.

     -- ABC Widget Co.

SMTP Header:

X-MC-MergeVars: {"_rcpt": "[email protected]", "orderdate": "01/12/2015", "fname":"Nome da criatura"}

Source:

link

link

    
13.03.2015 / 22:18