putyourlightson / craft-campaign

Send and manage email campaigns, contacts and mailing lists in Craft CMS.
https://putyourlightson.com/plugins/campaign
Other
63 stars 25 forks source link

How to fetch data from contacts dynamically #359

Closed samuelreichor closed 1 year ago

samuelreichor commented 1 year ago

Hello, I would like to send personalized emails. It is about a newsletter and I want to send it simultaneously to all contacts in a mailing list. For this I have created fields in the contacts (firstname, lastname, gender) and I would like to use this data in the email template.

I followed the docs here and managed to fetch a contact with :

{% set contact = craft.campaign.contacts.email('jim@bean.com').one() %}

{% if contact %}
  <p>{{ contact.firstname }}</p>
  <p>{{ contact.lastname }}</p>
{% endif %}

Logically I can't hardcode the Email, so I'm looking for a way to retrieve the data dynamically and insert it into the template.

Any ideas or solutions?

bencroker commented 1 year ago

The contact variable will be available in your campaign emails already, so you can just do it as follows.

<p>{{ contact.firstname }}</p>
<p>{{ contact.lastname }}</p>
samuelreichor commented 1 year ago

I already tried that, unfortunately it didn't work with contact.handleOfTheField.

bencroker commented 1 year ago

Please provide some details of what "didn't work" so I can help you.

samuelreichor commented 1 year ago

Got it now thank you :)