2
votes

Well, I want to send an automated email every Friday using MailChimp/Mandrill.

Email Template :

Hi {{firstName}},

You weekly weekend pass is {{code}}.

Here, My email content is dynamically generated. So, before sending automated email, there should be some mechanism where I call the rest api from where I get the firstName and code. Then email is sent to different user.

{
   [{
      firstName: 'test',
      code: 'Xewetwerx'
   },
   {
      firstName: 'test2',
      code: 'Xewetwrtd'
   }]
}

I know we can write code in our backend server and call the API every Friday using cronjob(to send an email every Friday) but I don't want to use any cronjob.

Is it possible to make API call and send automated mail using MailChimp or mandrill ???

Outcome: 2 email should be sent with different code.

1

1 Answers

0
votes

Mandrill implements scheduled emails through the sent_at parameter.

It's possible to send an email using a template and specify date and time when the message should be sent as a UTC timestamp in YYYY-MM-DD HH:MM:SS format. If you specify a time in the past, the message will be sent immediately. Note that for the sent_at feature you will need a paid (non free) account

If you choose PHP as the programming language for your backend, you can use the official PHP API library.

example: (the code is not tested and could contain errors)

require_once '/path/to/MailchimpTransactional/vendor/autoload.php';

$mailchimp = new MailchimpTransactional\ApiClient();
$mailchimp->setApiKey('YOUR_API_KEY');
$msg = array(
    'subject' => 'My subject',
    'from_email' => '[email protected]',
    'to' => array(array('email' => '[email protected]', 'name' => 'Recipient 1'),array('email' => '[email protected]', 'name' => 'Recipient 2')),
    'merge_vars' => array(array(
        'rcpt' => '[email protected]',
        'vars' =>
        array(
            array(
                'name' => 'firstName',
                'content' => 'test'),
            array(
                'name' => 'code',
                'content' => 'Xewetwerx')
    )),
    
    array(
        'rcpt' => '[email protected]',
        'vars' =>
        array(
            array(
                'name' => 'firstName',
                'content' => 'test2'),
            array(
                'name' => 'code',
                'content' => 'Xewetwrtd')
    ))));



$response = $mailchimp->messages->sendTemplate([
    "template_name" => "template_name",
    "template_content" => [[]],
    "message" => $msg,
    "send_at" => "2021-06-23 09:05:00" ,
]);
print_r($response);