-
Notifications
You must be signed in to change notification settings - Fork 115
Support for recipient variables when batch-sending #11
Comments
Here's my two pence: 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) |
@kJamesy I will definitely have a look at it. |
Great, thanks. |
@kJamesy It has been added.
$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:
I'll try to update the docs tomorrow. |
Cool stuff! |
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'
)
); |
Yes, I didn't even read the code correctly, was 3AM when I saw this! That makes sense. Cheers |
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! |
@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! |
@kJamesy, It must be for a different reason though. Mine works perfectly fine with recipient variables as
But it fails if I try to use variables in the bcc field. e.g.
|
I'll take a look at it. |
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. |
Okay, important note to anyone who can't get this work: Make sure you are not using |
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!
The text was updated successfully, but these errors were encountered: