Overwrite issue in merge_vars of Mandrill API - php

I am sending email using merge_vars for dynamic contents. Here is how my merge_vars look like:
$message['merge_vars'][$index] = array(
'rcpt' => $email,
'vars' => array(
array(
'name' => 'url',
'content' => $url
),
array(
'name' => 'sname',
'content' => $sname
),
array(
'name' => 'lname',
'content' => $lname,
),
array(
'name' => 'email',
'content' => $email
)
),
);
Everything is working fine. But when same recipient should receive multiple different email in a single API call, then the problem occurs. That time same recipient don't receive different email, he receives same email multiple times.

In a single api call you cannot send different emails to same email address with different merge vars.
You need to fire multiple api calls having target recipient in each call with respective merge_vars to meet your requirement.

Related

Mandrill's attachment without extension on Hotmail/Outlook.com

I guess there is a issue with Mandrill attachments x Hotmail/Outlook.com.
I can send e-mail attachments with Mandrill using base64_encode(). I am using the PHP library.
If I send an e-mail to Gmail or any other provider, the attachment looks fine. Downloading it and opening normally.
But sending to a Hotmail/Outlook.com, the attachments came with no extension/file format. By downloading, it can only be opened by adding the extension manually (for example, .pdf).
The code:
$file_data = file_get_contents($_FILES["test_file"]["tmp_name"])
$file_type = $_FILES["test_file"]["type"]
$mandrill = new Mandrill('MANDRILL_API_KEY_HERE');
$message = array(
'html' => $html,
'subject' => 'Testing',
'from_email' => 'sender#test.com',
'from_name' => 'Sender',
'attachments' => array(
array(
'name' => 'Test PDF document',
'type' => $file_type,
'content' => base64_encode($file_data)
)
),
'to' => array(
array(
'email' => 'to#test.com',
'name' => 'John Doe',
'type' => 'to'
)
),
'headers' => array('Reply-To' => 'sender#test.com')
);
$result = $mandrill->messages->send($message);
Any ideas? Is it a Mandrill or Hotmail issue?
Thanks!
I have found the solution. Gmail and other email services recognizes the type attribute as above, but Hotmail only recognizes the file type by adding the extension to the attachment name (name attribute).
For example:
'name' => 'Test PDF document.pdf',
It look obvious, but Hotmail requires it. Sharing my simple knowledge, since I received no answers.

Using Guzzle with GetResponse API to save custom field?

I am sending a post request to the GetResponse API. Everything works fine until I add a custom field (customFieldValues) to save along with my new email contact.
$body_data =
[
'name' => $input['name'],
'email' => $input['email'],
'campaign' => [
'campaignId' => $campaign_id
],
'customFieldValues' => ['customFieldId' => 'LDe0h', 'value' => ['Save this test string.'] ]
];
When I send the request I get the following error message:
"errorDescription": "CustomFieldValue entry must be specified as array"
I have tried a few things now and not sure how to format this properly to have the API accept it.
Reference link:
http://apidocs.getresponse.com/v3/case-study/adding-contacts
I found the solution on github in an example for their php api here:
https://github.com/GetResponse/getresponse-api-php
I suppose I had to wrap an array inside an array inside of an array...geez:
'customFieldValues' => array(
array('customFieldId' => 'custom_field_id_obtained_by_API',
'value' => array(
'Y'
)),
array('customFieldId' => 'custom_field_id_obtained_by_API',
'value' => array(
'Y'
))
)
For GetResponse V3
'customFieldValues' => [
'customFieldId' => 'custom_field_id_from_API',
'name' => 'Website',
'value' => ['https://scholarshipspsy.com']
]
Please note the 'name' field is optional. The site sampled is an international scholarship platform.

Mandrill and PHP not sending transactional email out

So I've made a template called 'Basic' within Mandrill. I did a test send and it looks great. I put Mandrill into test mode and used the test API key in my code. I'm just trying to get the php to send out a test transactional email, but no email gets sent. Here is the printed response I get:
Array ( [0] => Array ( [email] => amiecrutchley02#gmail.com [status] => sent [_id] => 89bfab4c3938486eb9e36564f79a3e9f [reject_reason] => ) )
So I'm really not sure why I'm not receiving anything.
Here is my code:
<?php
require_once('includes/mandrill/Mandrill.php');
$mandrill = new Mandrill('my_api_key');
$message = array(
'subject' => 'Thank You For Your Purchase',
'from_email' => 'no-reply#acq.com',
'from_name' => 'ACQ',
'to' => array(array('email' => 'amiecrutchley02#gmail.com', 'name' => 'Amie')),
'merge_vars' => array(array(
'rcpt' => 'amiecrutchley02#gmail.com',
'vars' =>
array(
array(
'name' => 'FIRSTNAME',
'content' => 'Amie'),
array(
'name' => 'LASTNAME',
'content' => 'Crutchley')
))));
$template_name = 'Basic';
$template_content = array(
array(
'name' => 'main',
'content' => 'Hi *|FIRSTNAME|* *|LASTNAME|*, your profile has been updated!'),
array(
'name' => 'footer',
'content' => 'ACQ, Copyright 2014')
);
$response = $mandrill->messages->sendTemplate($template_name, $template_content, $message);
print_r($response);
?>
a test API key is specifically designed to not send email. It's designed so you can mimic sending email, but doesn't actually send. You're also not charged for sending tests. Here's the Mandrill KB about what test mode is, and how it works: Does Mandrill have a test mode or sandbox?
Well, weird, but my test API was the problem. I tried the public API and boom! It works!

How to generate an array of Multiple recipients using mandrill

I need to send emails to multiple recipients. The number of recipients will vary depending on the data in the db.
Mandrill allows me to only add multiple recipients using an array.
Below is what works for multiple recipients
//email array that needs to be added to the 'to' key
$emailArray = ["example#example.com","test#test.com","hello#test.com","world#test.com"];
$mandrill = new Mandrill('xxxxxxxxxxxxxxx');
$message = array(
'subject' => 'Thanks for signing up',
'from_email' => 'support#test.com',
'to' => array(
array(
'email' => 'hello#test.com',
'name' => 'Hello Test'
),
array(
'email' => 'goodbye#test.com',
'name' => 'Goodbye Test',
)
),
'global_merge_vars' => array(
array(
'name' => 'FIRSTNAME',
'content' => 'JOHN'
),
array(
'name' => 'LASTNAME',
'content' => 'DOE')
));
//print_r($message);
$template_name = 'hello-world';
print_r($mandrill->messages->sendTemplate($template_name, $template_content, $message));
Below is what i need to generate dynamically depending on the length of the emailArray
to' => array(
//the below array should be dynamically generated
array(
'email' => 'hello#test.com',
'name' => 'Hello Test'
),
array(
'email' => 'goodbye#test.com',
'name' => 'Goodbye Test',
)
)
Here is the array. Currently with fixed values.
//email array that needs to be added to the 'to' key
$emailArray = ["example#example.com","test#test.com","hello#test.com","world#test.com"];
My questions is How do generate the 'To' values based on the length of the email array?
is there a way i can implode the entire array script?
An effective way would be to use array_map I have added in some code that also takes an array of names as well (I couldn't see where you were getting the names from in your code).
$toAddresses = array('example#example.com','test#test.com','hello#test.com','world#test.com');
$names = array('Exmaple', 'Test', 'Hello', 'World');
$mandrillTo = array_map( function ($address, $name) {
return array(
'email' => $address,
'name' => $name
);
},
$toAddresses,
$names
);
This passes the 0th element from each array into the function and returns an array of two values, then passes the 1st, 2nd etc and returns each result in a new array ($mandrillTo)

How to create this SOAP XML request?

we are using SAOP clients for a while now without a problem. But now we are facing the following challenge and I can't find the answer. We need to send the following XML structure:
<Cards>
<CardDetails>
<Name>string</Name>
<Address>string</String>
</CardDetails>
<CardDetails>
<Name>string</Name>
<Address>string</String>
</CardDetails>
</Cards>
As you van see we need two instances of 'CardDetails'. Creating a PHP array will only allow me to send 1.
$data = array(
'Cards' => array(
'CardDetails' => array(
'Name' => 'test name',
'Address' => 'test address'
),
'CardDetails' => array(
'Name' => 'second test name',
'Address' => 'second test address'
)
)
));
Of course, only the second address will be used. But what would be the solution to make this work?
Thanks a lot!
Use php dom to create xml from an array , here is a good example of it http://www.ibm.com/developerworks/library/os-xmldomphp/
And what function/class are you using to turn array to xml?
Try this structure:
$data = array(
'Cards' => array(
'CardDetails' => array(
array(
'Name' => 'test name',
'Address' => 'test address'
),
array(
'Name' => 'second test name',
'Address' => 'second test address'
)
)
)
);
If this doesn't work and you can modify serializing function, just check if current key is numerical. If it is, use parent key name for tag.

Categories