GithubHelp home page GithubHelp logo

Comments (13)

azamat-sharapov avatar azamat-sharapov commented on June 4, 2024 1

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.

kJamesy avatar kJamesy commented on June 4, 2024

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.

Bogardo avatar Bogardo commented on June 4, 2024

@kJamesy I will definitely have a look at it.

from mailgun.

kJamesy avatar kJamesy commented on June 4, 2024

Great, thanks.

from mailgun.

Bogardo avatar Bogardo commented on June 4, 2024

@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(
    "[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.

kJamesy avatar kJamesy commented on June 4, 2024

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

from mailgun.

Bogardo avatar Bogardo commented on June 4, 2024

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.

kJamesy avatar kJamesy commented on June 4, 2024

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

from mailgun.

elambro avatar elambro commented on June 4, 2024

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.

kJamesy avatar kJamesy commented on June 4, 2024

@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.

elambro avatar elambro commented on June 4, 2024

@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.

Bogardo avatar Bogardo commented on June 4, 2024

I'll take a look at it.

from mailgun.

azamat-sharapov avatar azamat-sharapov commented on June 4, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.