Adding Data to MailGun Bulk Email

I send several hundred emails via Mailgun like this:

// Send to Mailgun
$mailgunResult = $mgClient->sendMessage($domain, array(
  'from'    => $fromEmail,
  'to'      => $toListString,
  'subject' => $emailSubject,
  'text'    => $textEmail,
  'html'    => $htmlEmail,
  'recipient-variables' => $recipientJSON
));

      

This works great, but now I want to attach custom data to every email, like my ID for every email, etc. The docs show how to add json data to one email, but I can't figure out how to get Mailgun to match my list of data for each outgoing email, for example variable recipients.

Has anyone done this? My ticket with Mailgun just made me point to the docs I already referenced.

+3


source to share


2 answers


You can use Mailgun template tags in your custom variables. I use this to track recipients by unique ID for my sites. Add a variable to your post API payload using style v:

and then use template ( %recipient.value%

) to set a value for that variable.

Sample JSON payload:

{
    'v:Recipient-Id': '%recipient.id%'
    'recipient-variables:': { 'email@example.com': {'id': '123'}}
}

      



Then in the webhooks payload you will see a variable similar to the following:

'Recipient-Id': '123'

I learned about this from a helpful blog post on the Mailgun site: http://blog.mailgun.com/closing-the-loop-between-your-customer-data-and-your-email-data/

+6


source


I am not sure if I understand you correctly as you say that you have already looked through the documents.

To me it looks like the docs give the answer. https://documentation.mailgun.com/user_manual.html#attaching-data-to-messages :

Attaching data to messages

When submitting, you can attach data to your messages by passing custom data to API or SMTP endpoints. The data will be presented as a header in the letter, X-Mailgun-Variables. The data is formatted in JSON and included in any web hosting events associated with email containing user data. Several of these headers can be included and their values ​​will be combined.

Example:

X-Mailgun-Variables: {"first_name": "John", "last_name": "Smith"}
X-Mailgun-Variables: {"my_message_id": 123}

      

To add this header to your post:

API: Pass next parameter "v:my-custom-data" => "{"my_message_id": 123}"

.

SMTP: Add the following header to your address "X-Mailgun-Variables: {"my_message_id": 123}"

.

Note

The header value "X-Mailgun-Variables"

must be a valid JSON string, otherwise Mailgun will not be able to parse it. If your Header is X-Mailgun-Variables

longer than 998 characters, you must use folding to spread the variables across multiple lines.



When you say "your id" you don't mean what Mailgun calls "tag" or "campaign", right?

NTN

0


source







All Articles