Gavin
Gavin

Reputation: 7976

Sending an email template with Mandrill gives me errors

I'm trying to switch my site over to Mandrill, however I'm having some problems with the PHP API.

There are two problems:

Here is my code:

require_once './libraries/Mandrill.php';

try {
    $mandrill = new Mandrill('myapikey');
    $template_name = 'my-template-slug';
    $template_content = '';
    $message = array(
        'to' => array(
            array(
                'email' => '[email protected]',
                'name' => 'RecipientsName',
                'type' => 'to'
            )
        ),
        'auto_text' => true,
        'merge_vars' => array(
            array(
                'rcpt' => '[email protected]',
                'vars' => array(
                    array(
                        'name' => 'USERNAME',
                        'content' => 'user1234'
                    ),
                    array(
                        'name' => 'CONFIRM_CODE',
                        'content' => '19874lahg62378hwsi'
                    )
                )
            )
        )
    );
    $result = $mandrill->messages->sendTemplate($template_name, $template_content, $message);
} catch(Mandrill_Error $e) {
    echo 'A mandrill error occurred: ' . get_class($e) . ' - ' . $e->getMessage();
    throw $e;
}

And here is the error:

A mandrill error occurred: Mandrill_HttpError - API call to messages/send-template failed: No URL set! Fatal error: Uncaught exception 'Mandrill_HttpError' with message 'API call to messages/send-template failed: No URL set!' in /Users/Gavin/Desktop/Web/mandrill-test/libraries/Mandrill.php:126 Stack trace: #0 /Users/Gavin/Desktop/Web/mandrill-test/libraries/Mandrill/Messages.php(160): Mandrill->call('messages/send-t...', Array) #1 /Users/Gavin/Desktop/Web/mandrill-test/index.php(70): Mandrill_Messages->sendTemplate('my-template-slug', Array, Array) #2 /Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/devappserver2/php/setup.php(131): require('/Users/Gavin/De...') #3 {main} thrown in /Users/Gavin/Desktop/Web/mandrill-test/libraries/Mandrill.php on line 126

Upvotes: 2

Views: 3524

Answers (2)

Ravi Chauhan
Ravi Chauhan

Reputation: 1477

<?php
require_once 'Mandrill.php';

$mandrill = new Mandrill('MY API KEY IS USUALLY HERE');
$message = array(
    'subject' => 'Test message',
    'from_email' => '[email protected]',
    'from_name' => 'Sender person',
    'html' => '<p>this is a test message with Mandrill\'s PHP wrapper!.</p>',
    'to' => array(array('email' => '[email protected]', 'name' => 'Recipient 1')),
    'merge_vars' => array(array(
        'rcpt' => '[email protected]',
        'vars' =>
        array(
            array(
                'name' => 'FIRSTNAME',
                'content' => 'Recipient 1 first name'),
            array(
                'name' => 'LASTNAME',
                'content' => 'Last name')
    ))));

//print_r($mandrill->messages->sendTemplate($template_name, $template_content, $message));
echo ("hello");

?>

and send message for

$mandrill->messages->send($message, $async=false, $ip_pool=null, $send_at=null);

Upvotes: 0

PHP Worm...
PHP Worm...

Reputation: 4224

Mandrill hijacks the links and injects its own URLs so that link is rerouted via their servers. That results with user seeing the mandrill URL in their browser before going to the the right page

There's an option on the Sending Defaults page in your account that is for click-tracking (should be a drop down menu, near the top, just below the checkbox for tracking opens). What you select there will be the default option applied to all messages, unless you provide a different per-message setting for click-tracking. With SMTP, you can set click-tracking on a per-message basis using custom SMTP headers. More information about using SMTP headers to customize this can be found in the Mandrill KB here.

Upvotes: 2

Related Questions