2

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

There are two problems:

  • First, it is sending the email twice. The code at the bottom is all the code I have (except for the PHP opening and closing tags) and I cannot figure out why it would be sending the email twice every time.
  • Second, I get an error from cURL saying the URL is not set. The email is being sent, so obviously there is a URL set. The error is below.

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' => 'a_test@emailaddress.com',
                'name' => 'RecipientsName',
                'type' => 'to'
            )
        ),
        'auto_text' => true,
        'merge_vars' => array(
            array(
                'rcpt' => 'a_test@emailaddress.com',
                '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

PJ Bergeron
  • 2,788
  • 4
  • 25
  • 42
Gavin
  • 7,544
  • 4
  • 52
  • 72
  • Is throwing the exception AND sending the email twice at the same time? Or is that happening after invoking multiple times? I'd try to debug/print to see if your method is somehow called twice. Also, here: http://stackoverflow.com/questions/22647687/sending-email-with-mandrill-using-php?rq=1 they suggest using send instead of sendTemplate – marianosimone Mar 17 '15 at 14:47
  • The code above is the only code used. And yes, it's throwing the exception AND sending the email twice. – Gavin Mar 17 '15 at 15:30
  • I should also add that since this problem is happening, I switched to using Mandrill's SMTP server instead of the API (with my own templates hosted on my web server) and it's only sending once. – Gavin Mar 17 '15 at 15:39
  • In that case, it seems like a bug in their API. I'd still try the suggestions in http://stackoverflow.com/questions/22647687/sending-email-with-mandrill-using-php?rq=1 – marianosimone Mar 17 '15 at 15:41
  • Please check this link:- http://stackoverflow.com/questions/19222562/error-in-send-email-using-mandrill-php – Alive to die - Anant Mar 23 '15 at 07:09

2 Answers2

2

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.

PHP Worm...
  • 4,109
  • 1
  • 25
  • 48
0
<?php
require_once 'Mandrill.php';

$mandrill = new Mandrill('MY API KEY IS USUALLY HERE');
$message = array(
    'subject' => 'Test message',
    'from_email' => 'jwjody@yahoo.com',
    'from_name' => 'Sender person',
    'html' => '<p>this is a test message with Mandrill\'s PHP wrapper!.</p>',
    'to' => array(array('email' => 'jwjody@yahoo.com', 'name' => 'Recipient 1')),
    'merge_vars' => array(array(
        'rcpt' => 'recipient1@domain.com',
        '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);
Ravi Chauhan
  • 1,409
  • 13
  • 26