Search code examples
phpemailcsvgmailmailing-list

Dynamic email content using Gmail


There is any way (maybe via Chrome extensions) to send a few of messages with dynamic content via Gmail?

For example, if I have in a csv file:

NAME                EMAIL                   PET     FOOD BRAND DISCOUNT
Charlie Brown       [email protected]   dog     Alpo       5
Dr. Claw            [email protected]     cat     Iams       10
Jim Henson          [email protected]         frog    Easy Green 15
Shaggy Rogers       [email protected]    dog     Alpo       5
Walt Disney         [email protected]         mouse   Magic      20
Mike Tyson          [email protected]       cat     Iams       10

and in the body of email I need to insert something like that:

Dear {NAME}, For a limited time, we are offering {DISCOUNT}% discount
on {FOOD BRAND} food for your {PET}.

Also each email recipient must be included by {EMAIL} field from csv file.

I have not a large number of emails to send that. It is about 30 of them, but I would like to automatize that since I already have all needed data in csv file.

Any suggestions?

Thank you in advance!


Solution

  • You have to compile the html page (which will be sent to the recipient) with PHP and send it then and do it for each recipient.

    Here's simple example:

    <?php
    $file = fopen('youcsvfile.csv', 'r');
    
    while ($client = fgetscsv($file))
        mail($client[1], 'Special offer', compileHtml($client));
    
    /**
     * Build HTML page to be sent to clients
     * @param array $client
     * @return string
     */
    function compileHtml($client) {
        return "<html>...Dear " . $client[0] . ", for a limited time, we are offering " . $client[4] . " discount on " . $client[3] . " food for your " . $client[2] . " <more html code></html>";
    }
    

    Read mail function documentation for more info.