Comments (13)
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.
from mailgun.
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)
from mailgun.
@kJamesy I will definitely have a look at it.
from mailgun.
Great, thanks.
from mailgun.
@kJamesy It has been added.
There are now two ways to register the recipient variables
- The way you proposed with a
recipientVariables
method - Passing them as an array with the
to
method
$recipients = array(
"[email protected]" => array(
'first' => 'Foo',
'last' => 'Bar'
),
"[email protected]" => 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.
from mailgun.
Cool stuff!
Will the to field still show the user first name + last name when the email is received?
from mailgun.
If I understand your question correctly, yes you can:
$recipients = array(
"Foo Bar <[email protected]>" => array(
'first' => 'Foo',
'last' => 'Bar'
),
"John Doe <[email protected]>" => array(
'first' => 'John',
'last' => 'Doe'
)
);
from mailgun.
Yes, I didn't even read the code correctly, was 3AM when I saw this! That makes sense. Cheers
from mailgun.
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!
from mailgun.
@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!
from mailgun.
@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);
from mailgun.
I'll take a look at it.
from mailgun.
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.
from mailgun.
Related Issues (20)
- Clarification please regarding handling of http_response_body content and possibly guzzle and/or env setting? HOT 3
- Undefined Method error
- Type error: Argument 1 passed must be an instance of App\Http\Controllers\Message HOT 1
- Example send email to mailing list HOT 1
- Troubles with the API request, deprecated usable of mailgun-php lib HOT 2
- .
- Trying to get property of non-object HOT 1
- Update nesbot/carbon dependency HOT 1
- Cannot install php-http/guzzle6-adapter HOT 2
- Switching endpoint HOT 2
- Using full api from mailgun-php HOT 2
- Install on Laravel 5.8? HOT 4
- ERROR when passing a variable to to() HOT 1
- new release 5.1.0 with Carbon 2 is not available via composer and isn't on packagist HOT 1
- Error installing this package on Laravel 5.8 (clunky workaround included...can we fix the package?) HOT 1
- Upgrade Laravel 6 HOT 1
- Unable to install php-http / guzzle6-adapter in symfony 4.1
- Emails are not being sent or received.
- Mailgun custom notification channel
- specific attachment to each recipient in batch emails
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mailgun.