Send email with dynamic content in PHP

Asked

Viewed 751 times

1

I have a template .html email with +- 700 lines (created on Mailchimp) and need to insert some recovered database values into it (something similar to what the Velocity makes) before sending to the customer.

Although I don’t need to enter as much dynamic data as this, I look for some solution of how to do this without having to define the content in php (the string would be immense and would take many lines from my class), I’m using the Phpmailer.

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

<!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>

3 answers

4


You can easily replace keywords from your template 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 might prefer extra protection with htmlentities:

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


The.txt template would look something like this:

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

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


In short, simply 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 wish, as long as there is no danger of coinciding with text that should not be changed.

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

Olá, ^_NomeDoMiguxo_^, ...

$corpoDoEmail = str_replace( '^_NomeDoMiguxo_^', $nome      , $corpoDoEmail );

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;

?>

2

The very one Mailchimp already comes with this feature, it is called Merge Vars. (Mailchimp uses Mandrill as an 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:

http://help.mandrill.com/entries/21678522-How-do-I-use-merge-tags-to-add-dynamic-content-

http://help.mandrill.com/entries/21688056-using-smtp-headers-to-customize-your-messages

Browser other questions tagged

You are not signed in. Login or sign up in order to post.