Skip to content
This repository has been archived by the owner on Dec 22, 2019. It is now read-only.

Support for recipient variables when batch-sending #11

Closed
kJamesy opened this issue Mar 15, 2014 · 13 comments
Closed

Support for recipient variables when batch-sending #11

kJamesy opened this issue Mar 15, 2014 · 13 comments
Assignees

Comments

@kJamesy
Copy link

kJamesy commented Mar 15, 2014

From the manual: http://documentation.mailgun.com/user_manual.html#batch-sending when batch-sending, Mailgun only knows to send emails to individual recipients (i.e. one recipient in the 'to' field) when you use recipient variables. Batch sending has an advantage over using the Mailgun::send in a loop since there's only one API call with the former therefore less loading time for the user. And then there's the advantage of using the same to personalise the emails.
Could you kindly add this feature?

Great work!

@kJamesy
Copy link
Author

kJamesy commented Mar 15, 2014

Here's my two pence:
Message.php

public function recipientVariables($variables)
{
    if ( is_array($variables) ) {
        $this->{'recipient-variables'} = json_encode($variables);
    }
}

Usage in Controller:

$users = User::take(2)->get();

$variables = [], $data = [];
foreach ($users as $user) {
    $variables[$user->email] = ['id' => $user->id, 'first_name' => $user->first_name];
}

Mailgun::send('emails.newsletter', $data, function($message) use ($users,$variables)
{
    foreach ($users as $user) {
        $message->to($user->email, $user->first_name . ' ' . $user->last_name);
    }

    $message->recipientVariables($variables);
});

(Apologies, can't get Github to format it nicely)

@Bogardo
Copy link
Owner

Bogardo commented Mar 15, 2014

@kJamesy I will definitely have a look at it.

@Bogardo Bogardo self-assigned this Mar 15, 2014
@kJamesy
Copy link
Author

kJamesy commented Mar 15, 2014

Great, thanks.

@Bogardo
Copy link
Owner

Bogardo commented Mar 16, 2014

@kJamesy It has been added.
There are now two ways to register the recipient variables

  1. The way you proposed with a recipientVariables method
  2. Passing them as an array with the to method
$recipients = array(
    "foo@bar.com" => array(
        'first' => 'Foo',
        'last'  => 'Bar'
    ),
    "johndoe@example.com" => array(
        'first' => 'John',
        'last'  => 'Doe'
    )
);
Mailgun::send('emails.welcome', $data, function($message) use ($recipients)
{
    $message->to($recipients);
});

And in your emails you can use the variables like this:

 Hi there %recipient.first% %recipient.last%

I'll try to update the docs tomorrow.
Please let me know if this works for you.

@kJamesy
Copy link
Author

kJamesy commented Mar 16, 2014

Cool stuff!
Will the to field still show the user first name + last name when the email is received?

@Bogardo
Copy link
Owner

Bogardo commented Mar 16, 2014

If I understand your question correctly, yes you can:

$recipients = array(
    "Foo Bar <foo@bar.com>" => array(
        'first' => 'Foo',
        'last'  => 'Bar'
    ),
    "John Doe <johndoe@example.com>" => array(
        'first' => 'John',
        'last'  => 'Doe'
    )
);

@kJamesy
Copy link
Author

kJamesy commented Mar 16, 2014

Yes, I didn't even read the code correctly, was 3AM when I saw this! That makes sense. Cheers

@elambro
Copy link

elambro commented Aug 14, 2014

Hi!

Could you please make this work with ->cc and ->bcc as well? I receive the Mailgun error "The parameters passed to the API were invalid. Check your inputs!".

Thank you!

@kJamesy
Copy link
Author

kJamesy commented Aug 14, 2014

@elambro, this is a very good coincidence because I am having the exact same error this today! Not seen it before. Not using CC or BCC either!

@elambro
Copy link

elambro commented Aug 14, 2014

@kJamesy, It must be for a different reason though. Mine works perfectly fine with recipient variables as

    $message->to($variables); $message->bcc($email_address);

But it fails if I try to use variables in the bcc field. e.g.

    $message->to($variables); $message->bcc($other_variables);

@Bogardo
Copy link
Owner

Bogardo commented Aug 14, 2014

I'll take a look at it.

@azamat-sharapov
Copy link

I can't get this work. Has anybody used this feature? I tried both ways like described above, but variables don't get replaced in message body.

@azamat-sharapov
Copy link

Okay, important note to anyone who can't get this work: Make sure you are not using catch_all config option! It won't work with it.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants