0

Why mail chimp is not working in ZF1 and ZF2 with following function?

class TestController extends Zend_Controller_Action {

  public function indexAction() {
   echo  $this->Mailb(
                  "from@gmail.com", 
                  "to@gmail.com", 
                  "Mail sucks",
                  "PING PINGO",
                  'me@gmail.com,me1@gmail.com,me2@gmail.com', 
                  );
  }

  public static function Mailb($from, $to, $subject, $htmlBody, 
          $bcc = '') {
    $uri = 'https://mandrillapp.com/api/1.0/messages/send.json';

    $postString = '{
    "key": "erewrrwerewrewrewrewrewr",
    "message": {
        "html": "' .$htmlBody. '",
        "text":  "' .$htmlBody. '",
        "subject": "' .$subject.'",
        "from_email": "' .$from. '",
        "from_name": "BLA 1",
        "to": [
            {
                "email": "' . $to . '",
                "name": "BLA 2"
            }
        ],
        "headers": {

        },
        "track_opens": true,
        "track_clicks": true,
        "auto_text": true,
        "url_strip_qs": true,
        "preserve_recipients": true,

        "merge": true,
        "global_merge_vars": [

        ],
        "merge_vars": [

        ],
        "tags": [

        ],
        "google_analytics_domains": [

        ],
        "google_analytics_campaign": "...",
        "metadata": [

        ],
        "recipient_metadata": [

        ],
        "attachments": [

        ]
    },
    "async": false
    }';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $uri);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);

    $result = curl_exec($ch);    
  }

}

EDIT : Main error

NOW: [{"email":"to@gmail.com","status":"sent","_id":"1cbe2f9a2d","reject_reason":nul‌​l}]

  • What's the error, what have you tried? What have you debugged? Etc etc, more info! – Jurian Sluiman Jul 19 '13 at 10:20
  • This is always showing: `{"status":"error","code":-1,"name":"ValidationError","message":"You must specify a key value"}` –  Jul 19 '13 at 10:24
  • Also sometimes: `{"status":"error","code":-2,"name":"ValidationError","message":"Validation error: {\"message\":{\"from_email\":\"The domain portion of the email address is invalid (the portion after the @: gmail.com')\"}}"}` –  Jul 19 '13 at 10:25
  • Sometimes: `[{"email":"to@gmail.com","status":"sent","_id":"1cbe2f9a2d","reject_reason":null}]` –  Jul 19 '13 at 10:27

1 Answers1

1

Just a guess.

You are just dumping the naked params - $htmlBody, $subject, etc - into a JSON template. Perhaps there are some slashes or quotes that need to be encoded in order to create valid JSON payload. MailChimp may be detecting that invalid payload and poorly reporting back to you.

I'd probably create a PHP array and then use json_encode($arr) to construct the payload. This way, all the encoding of slashes and quotes an other madness inside those vars is handled by json_encode itself.

Specifically:

$postData = array(
    'key' => 'mykey',
    'message' => array(
        'html' => $htmlBody,
        'text' => $htmlBody,
        'subject' => $htmlBody,
        'subject' => $subject,
        'from_email' => $from,
        // etc
    ),
);

$postString = json_encode($postData);

// Then post via `curl_xxx()` as before

A secondary thought, probably more style than substance: the method Mailb() is declared statically, but is called using $this->Mailb(). I'd probably call it using self::Mailb().

David Weinraub
  • 14,144
  • 4
  • 42
  • 64