Abhishek Singh
Abhishek Singh

Reputation: 197

Mandrill API email getting Queued with use of Template

I am trying to use mandrill api for sending emails with template. I am working on method documented here: https://mandrillapp.com/api/docs/messages.php.html#method=send-template Code is implemented in URL: http://ezaccom.com/mxl.php My problem is that all of my emails are getting queued instead of sending quickly as they should. Here is code (Note %TEMPLATE CODE% is where my email template goes):

 try {
$mandrill = new Mandrill('API_KEY_REDACTED');
$template_name = 'Welcome mail on email /FB sign-up to very email id';
$template_content = array(
    array(
        'name' => 'Welcome mail on email /FB sign-up to very email id',
        'content' => 'sign-up'
    )
);
$message = array(
    'html' => '%TEMPLATE CODE%',
    'text' => 'Example text content',
    'subject' => 'Welcome to Easyaccom',
    'from_email' => 'hello@easyaccom.com',
    'from_name' => 'Easyaccom',
    'to' => array(
        array(
            'email' => 'RECIPIENT@gmail.com',
            'name' => 'Jordan Belfort',
            'type' => 'to'
        )
    ),
    'headers' => array('Reply-To' => 'message.reply@example.com'),
    'important' => false,
    'track_opens' => null,
    'track_clicks' => null,
    'auto_text' => null,
    'auto_html' => null,
    'inline_css' => null,
    'url_strip_qs' => null,
    'preserve_recipients' => null,
    'view_content_link' => null,
    'bcc_address' => 'message.bcc_address@example.com',
    'tracking_domain' => null,
    'signing_domain' => null,
    'return_path_domain' => null,
    'merge' => true,
    'merge_language' => 'mailchimp',
    'global_merge_vars' => array(
        array(
            'name' => 'merge1',
            'content' => 'merge1 content'
        )
    ),
    'merge_vars' => array(
        array(
            'rcpt' => 'recipient.email@example.com',
            'vars' => array(
                array(
                    'name' => 'merge2',
                    'content' => 'merge2 content'
                )
            )
        )
    ),
    'tags' => array('password-resets'),
    'subaccount' => 'customer-123',
    'google_analytics_domains' => array('example.com'),
    'google_analytics_campaign' => 'message.from_email@example.com',
    'metadata' => array('website' => 'www.example.com'),
    'recipient_metadata' => array(
        array(
            'rcpt' => 'recipient.email@example.com',
            'values' => array('user_id' => 123456)
        )
    ),
    'attachments' => array(
        array(
            'type' => 'text/plain',
            'name' => 'myfile.txt',
            'content' => 'ZXhhbXBsZSBmaWxl'
        )
    ),
    'images' => array(
        array(
            'type' => 'image/png',
            'name' => 'IMAGECID',
            'content' => 'ZXhhbXBsZSBmaWxl'
        )
      )
    );
   $async = false;
$ip_pool = 'Main Pool';
$send_at = 'example send_at';
$result = $mandrill->messages->sendTemplate($template_name, $template_content, $message, $async, $ip_pool);
print_r($result);
/*
Array
(
    [0] => Array
        (
            [email] => recipient.email@example.com
            [status] => sent
            [reject_reason] => hard-bounce
            [_id] => abc123abc123abc123abc123abc123
        )

)
*/
} catch(Mandrill_Error $e) {
// Mandrill errors are thrown as exceptions
echo 'A mandrill error occurred: ' . get_class($e) . ' - ' . $e->getMessage();
// A mandrill error occurred: Mandrill_Unknown_Subaccount - No subaccount exists with the id 'customer-123'
throw $e;
  }
   ?>

Upvotes: 0

Views: 2552

Answers (2)

Neverbeen
Neverbeen

Reputation: 1

Just cut all unused parameters

try {
$mandrill = new Mandrill('API_KEY_REDACTED');
$template_name = 'Welcome mail on email /FB sign-up to very email id';
$template_content = array(
    array(
        'name' => 'Welcome mail on email /FB sign-up to very email id',
        'content' => 'sign-up'
    )
);
$message = array(
    'html' => '%TEMPLATE CODE%',
    'text' => 'Example text content',
    'subject' => 'Welcome to Easyaccom',
    'from_email' => 'hello@easyaccom.com',
    'from_name' => 'Easyaccom',
    'to' => array(
        array(
            'email' => 'RECIPIENT@gmail.com',
            'name' => 'Jordan Belfort',
            'type' => 'to'
        )
    ),
    'headers' => array('Reply-To' => 'message.reply@example.com'),
    'important' => false,
    'track_opens' => null,
    'track_clicks' => null,
    'auto_text' => null,
    'auto_html' => null,
    'inline_css' => null
    );
   $async = false;

$result = $mandrill->messages->sendTemplate($template_name, $template_content, $message, $async);
print_r($result);
/*
Array
(
    [0] => Array
        (
            [email] => recipient.email@example.com
            [status] => sent
            [reject_reason] => hard-bounce
            [_id] => abc123abc123abc123abc123abc123
        )

)
*/
} catch(Mandrill_Error $e) {
// Mandrill errors are thrown as exceptions
echo 'A mandrill error occurred: ' . get_class($e) . ' - ' . $e->getMessage();
// A mandrill error occurred: Mandrill_Unknown_Subaccount - No subaccount exists with the id 'customer-123'
throw $e;
  }
   ?>

Upvotes: 0

Kaitlin
Kaitlin

Reputation: 6235

You've got a lot of optional parameters that you're not setting explicitly, including attachments and images which will automatically result in the call being processed asynchronously (and the queued response). If you remove those, you should see errors returned to you, which should highlight things like having a subaccount that doesn't exist and an invalid send_at date. In general, you should remove all optional parameters except those that you're explicitly setting.

Also, if you're using a template (and sending with send-template), you don't need to provide the template code in the html parameter. That will be ignored if your stored template in Mandrill has HTML; you need to only provide the html if the stored Mandrill template doesn't already have HTML.

For your security, and for Mandrill's, since the API key was posted publicly, it's been disabled, and I've edited the original post to remove it. You should generate another one and not use the one posted here going forward.

Upvotes: 6

Related Questions