spatie / mailcoach-support

Questions and support for Mailcoach
https://mailcoach.app
31 stars 2 forks source link

Can I send extra attributes via a form? #287

Closed christophrumpel closed 3 years ago

christophrumpel commented 3 years ago

Hey,

I'd like to store some additional info with my subscriber who is subscribed via an external form. I was wondering if it is possible to do that with an input field?

freekmurze commented 3 years ago

Absolutely: here are the docs on that: https://mailcoach.app/docs/v3/package/advanced-usage/working-with-extra-attributes-on-subscribers

christophrumpel commented 3 years ago

I've been there but these are just for adding a subscriber "manually". I subscribe my user from an external form. (https://mailcoach.app/docs/v3/package/working-with-lists/using-subscription-forms) Does that work too? Tried to add an input for those values but probably difficult to define the key of extra attributes in there.

freekmurze commented 3 years ago

It seems like this is not documented yet.

On your email list you should put the names of the extra form fields that you allow in the allowed_form_extra_attributes attribute on the email list: https://github.com/spatie/laravel-mailcoach/blob/5081c59032d1d5bcdb737db9c8e0da9fbd9da810/src/Models/EmailList.php#L85-L88

You can then add hidden fields with those names to your form. The values will be saved in extra_attributes of a new subscriber: https://github.com/spatie/laravel-mailcoach/blob/9f848c99b47ee498dde895287269c64ffa3134ed/src/Http/Front/Controllers/SubscribeController.php#L34

christophrumpel commented 3 years ago

Thanks Freek, just gave it a try. I have now a field demands in my list allowed_form_extra_attributes. Also see the allowed field now here wen retrieving it: https://github.com/spatie/laravel-mailcoach/blob/master/src/Http/Front/Requests/CreateSubscriptionRequest.php#L42-L46

But the problem now is in the foreach loop only attributes from the request data are being checked. But my sent field from the form is called demands. So it seems this does only works when I send an attributes array with data which I think is not possible through an external form, right?

Btw, here is my form: (without the redirects)

<form name="newsletter" method="post" action="my-list-url">
    <div>
        <label for="email">Email</label>
        <input id="email" name="email">
    </div>

    <input id="demands" type="hidden" name="demands" value="demand1,demand2,demand2">

    <div>
        <button type="submit">Subscribe</button>
    </div>
</form>
christophrumpel commented 3 years ago

PS: If we figure this out, I can add it to the docs of course.

freekmurze commented 3 years ago

@riasvdv could you take a look at this one?

riasvdv commented 3 years ago

You should be able to send it like this if I remember correctly:

<input id="demands" type="hidden" name="attributes[demands]" value="demand1,demand2,demand2">
christophrumpel commented 3 years ago

thx @riasvdv it did store the data indeed. Thanks. I think the structure of the extra_attributes json content looks a little bit strange though. But I'll give it another look if this could be related to my form.

Screen Shot 2020-12-04 at 11 35 37 Screen Shot 2020-12-04 at 11 36 27
christophrumpel commented 3 years ago

So I just dug a little deeper and I think there is a problem with the extra_attributes field on the subscriber's table. Even if I don't store any "extra" data (HTML form just with my email), it stores data in extra_attributes.

SubscribeController Line 34 where extra $subscriber->extra_attributes gets merged with $request->attributes(), $subscriber->extra_attributesis an object of SchemalessAttributes with lots of data in it. Is this on purpose? (see screenshot)

Screen Shot 2020-12-07 at 09 41 59
riasvdv commented 3 years ago

That is expected, it's part of this package https://github.com/spatie/laravel-schemaless-attributes that allows you to do a bit more with the data structure than you could otherwise do with a flat array

christophrumpel commented 3 years ago

Hm ok, I see. I still find it strange that extra_attributes field is now full of all kinds of different data like all the other fields of the same row. But most important to me, I made it work now as you said:

<input id="demands" type="hidden" name="attributes[demands]" value="demand1,demand2,demand2"> + making sure this new attribute is allowed in allowed_form_extra_attributes.

And after the user is subscribed I have access to my values through $subscriber->extra_attributes->demands. I'll try to add it to the docs. Thanks @riasvdv