### Campaign Plugin Getting Started Source: https://putyourlightson.com/plugins/campaign/index Provides instructions on creating campaigns, mailing lists, adding contacts through various methods, and setting up sendouts. ```APIDOC Campaign Plugin Getting Started: 1. Create Campaign: Initiate a new campaign via Campaign → Campaigns. 2. Create Mailing List: Set up a new mailing list in Campaign → Mailing Lists. 3. Add Contacts: - Manual Creation: Create contacts in Campaign → Contacts and subscribe them in the Mailing Lists tab. - CSV Import: Import contacts from a CSV file via Campaign → Contacts → Import. - User Group Import: Import contacts from a user group via Campaign → Contacts → Import. - User Sync (Pro): Sync contacts from users by linking a mailing list to a user group in Campaign → Contacts → Sync. 4. Create Sendout: Schedule a new sendout in Campaign → Sendouts. ``` -------------------------------- ### Campaign Plugin Setup Steps Source: https://putyourlightson.com/plugins/campaign/index Guides users through the essential configuration steps for the Campaign plugin, covering test mode, email settings, custom fields, and campaign/mailing list types. ```APIDOC Campaign Plugin Setup: 1. Enable Test Mode: Disable live email sending in Campaign → Settings → General Settings. 2. Configure Email Settings: Adjust email preferences in Campaign → Settings → Email Settings. 3. Add Custom Fields: Define custom contact fields in Campaign → Settings → Contact Settings. 4. Enable GeoIP: Geolocation contacts by IP address via Campaign → Settings → GeoIP Settings. 5. Create Campaign Types: Define campaign categories in Campaign → Settings → Campaign Types. 6. Create Mailing List Types: Define mailing list categories in Campaign → Settings → Mailing List Types. 7. Set Up Cron Job: Configure a server cron job as described in Campaign → Settings → General Settings. ``` -------------------------------- ### Install Campaign Plugin Source: https://putyourlightson.com/plugins/campaign/index Install the Campaign plugin for Craft CMS using Composer. ```bash composer require putyourlightson/craft-campaign ``` -------------------------------- ### Campaign Plugin Testing Methods Source: https://putyourlightson.com/plugins/campaign/index Explains how to test campaign setup before mass sending, including enabling test mode, saving emails locally, and using external testing services. ```APIDOC Campaign Plugin Testing: - Enable Test Mode: Disables live sending; emails are saved to storage/runtime/mail. - Test Email Sending: Available on campaign and sendout edit pages. - Recommended Service: Mailtrap (https://mailtrap.io/) for email testing. ``` -------------------------------- ### Get Campaigns in PHP Source: https://putyourlightson.com/plugins/campaign/index Retrieves campaigns using the `CampaignElement::find()` static method in PHP. This example filters campaigns by type 'newsletter' and returns all matching elements. ```php use putyourlightson\campaign\elements\CampaignElement; $campaigns = CampaignElement::find()->campaignType('newsletter')->all(); ``` -------------------------------- ### Schedule Pending Sendouts with Cron Source: https://putyourlightson.com/plugins/campaign/index An example cron job entry to schedule the execution of pending sendouts for the campaign plugin. This command runs every 10 minutes to process queued sendouts. ```shell */10 * * * * php /path/to/project/craft campaign/sendouts/run ``` -------------------------------- ### Query String Parameters for Analytics Source: https://putyourlightson.com/plugins/campaign/index Example query string parameters for appending to campaign links, useful for tracking with analytics platforms like Google Analytics. Includes a Twig variable for dynamic campaign titles. ```twig utm_source=campaign-plugin&utm_medium=email&utm_campaign={{ campaign.title }} ``` -------------------------------- ### Twig Template Condition Example Source: https://putyourlightson.com/plugins/campaign/index Provides an example of a Twig template that can be used for template-based contact conditions. The template should output a string that evaluates to true (non-zero, non-empty) to satisfy the condition. ```twig {{ contact.dateOfBirth|date('Y') == 1980 ? 1 : 0 }} ``` -------------------------------- ### Get Sendouts using PHP Source: https://putyourlightson.com/plugins/campaign/index Retrieves sendout elements programmatically using PHP. It shows how to use the `SendoutElement::find()` method with filtering capabilities similar to the Twig example, allowing for programmatic access to sendout data. ```php use putyourlightson\campaign\elements\SendoutElement; $sendouts = SendoutElement::find()->sendoutType('sent')->campaignId(5)->all(); ``` -------------------------------- ### Get Campaigns in Craft Twig Source: https://putyourlightson.com/plugins/campaign/index Retrieves campaigns from Craft CMS using the `craft.campaign.campaigns` element query. This example filters campaigns by type 'newsletter' and displays them as links. ```twig {% set campaigns = craft.campaign.campaigns.campaignType('newsletter').all() %} {% for campaign in campaigns %} {{ campaign.title }} {% endfor %} ``` -------------------------------- ### Get or Create Contact using PHP Source: https://putyourlightson.com/plugins/campaign/index Demonstrates how to retrieve an existing contact by email or create a new one if it doesn't exist, using the Campaign plugin's contact service. It handles setting custom field values before saving the contact element. ```php use putyourlightson\campaign\Campaign; use putyourlightson\campaign\elements\ContactElement; $contact = Campaign::$plugin->contacts->getContactByEmail('name@email.com'); if ($contact === null) { $contact = new ContactElement(); $contact->email = 'name@email.com'; $contact->customFieldName = $customFieldValue; } Craft::$app->getElements()->saveElement($contact) ``` -------------------------------- ### Twig Template Example with Campaign Variables Source: https://putyourlightson.com/plugins/campaign/index Demonstrates how to use campaign template variables like `campaign`, `contact`, `browserVersionUrl`, and `unsubscribeUrl` within a Twig template for email content. Includes conditional logic for contact information. ```twig
Hello {{ contact.name }}
{% endif %} {{ campaign.body }} View this email in your browser {% if unsubscribeUrl %} Unsubscribe here {% endif %} ``` -------------------------------- ### Get Contact by Email (PHP) Source: https://putyourlightson.com/plugins/campaign/index Retrieves a single contact by their email address using the Campaign plugin's element query in PHP. Returns the contact element if found, otherwise null. ```php use putyourlightson\campaign\elements\ContactElement; $contact = ContactElement::find()->email('jim@bean.com')->one(); ``` -------------------------------- ### Submit Subscribe Form via AJAX (JavaScript) Source: https://putyourlightson.com/plugins/campaign/index Provides an example using the Fetch API to submit the mailing list subscribe form asynchronously. It sends form data via a POST request to the `/actions/campaign/forms/subscribe` endpoint and handles the JSON response to display success or failure messages. ```javascript const form = $('#subscribe-form'); const formData = $(form).serialize(); fetch('/actions/campaign/forms/subscribe', { method: 'POST', headers: { 'Accept': 'application/json', }, body: formData, }) .then(response => response.json()) .then(result => { if (result.status == 200) { // Success alert('You have successfully subscribed to the mailing list.'); } else { // Failure alert('Error subscribing to the mailing list.'); console.log(result.errors); } }); ``` -------------------------------- ### Get Contact by Email (Twig) Source: https://putyourlightson.com/plugins/campaign/index Retrieves a single contact by their email address using the Campaign plugin's element query in Twig. Returns the contact element if found, otherwise null. ```twig {% set contact = craft.campaign.contacts.email('jim@bean.com').one() %} {% if contact %} {{ contact.name }} {% endif %} ``` -------------------------------- ### Get Sendouts using Twig Source: https://putyourlightson.com/plugins/campaign/index Retrieves sendout elements from the Campaign plugin using Craft CMS's Twig templating. It demonstrates filtering by sendout type and campaign ID, then iterating through results to display sendout titles and dates. ```twig {% set sendouts = craft.campaign.sendouts.sendoutType('sent').campaignId(5).all %} {% for sendout in sendouts %} {{ sendout.title }} sent on {{ sendout.sendDate|date }} {% endfor %} ``` -------------------------------- ### Get Segment by ID in Template (Twig) Source: https://putyourlightson.com/plugins/campaign/index Shows how to retrieve a specific segment from the campaign plugin within Craft CMS templates. It uses the `craft.campaign.segments` element query to find a segment by its ID. ```twig // Gets the first segment with the specified ID {% set segment = craft.campaign.segments.id(3).one %} {% if segment %} Segment {{ segment.title }} {% endif %} ``` -------------------------------- ### Create Mailing List Subscribe Form (Twig) Source: https://putyourlightson.com/plugins/campaign/index Demonstrates how to create a mailing list subscribe form using Twig templating. It includes essential form fields like email, first name, last name, and custom fields, along with CSRF protection, action inputs, and redirect handling. The example also shows how to integrate reCAPTCHA. ```twig {% set mailingList = craft.campaign.mailingLists.id(7).one() %} {# If there were any validation errors, a `contact` variable will be passed to the template, which contains the posted values and validation errors. If that’s not set, we’ll default to a new contact. #} {% set contact = contact ?? create('putyourlightson\\campaign\\elements\\ContactElement') %} ``` -------------------------------- ### Get Mailing List by ID (Craft CMS Twig) Source: https://putyourlightson.com/plugins/campaign/index Retrieves a mailing list element using its ID in Craft CMS Twig. It demonstrates fetching a single mailing list and checking if it exists before displaying its title. ```twig // Gets the first mailing list with the specified ID {% set mailingList = craft.campaign.mailingLists.id(7).one() %} {% if mailingList %} Subscribe to {{ mailingList.title }} {% endif %} ``` -------------------------------- ### Get Segment by ID in Plugin (PHP) Source: https://putyourlightson.com/plugins/campaign/index Demonstrates how to access and retrieve segment elements programmatically within a Craft CMS plugin. It utilizes the `SegmentElement::find()` method to query for segments, similar to other Craft element types. ```php use putyourlightson\campaign\elements\SegmentElement; $segment = SegmentElement::find()->id(3)->one(); ``` -------------------------------- ### Get Mailing List by ID (Craft CMS PHP) Source: https://putyourlightson.com/plugins/campaign/index Retrieves a mailing list element using its ID in Craft CMS PHP. It shows how to use the `MailingListElement::find()` method to query for a specific mailing list by its ID. ```php use putyourlightson\campaign\elements\MailingListElement; $mailingList = MailingListElement::find()->id(7)->one(); ``` -------------------------------- ### Campaign Plugin Email Delivery Services Source: https://putyourlightson.com/plugins/campaign/index Lists recommended email delivery services and plugins compatible with Craft CMS, detailing their features and potential limitations. ```APIDOC Campaign Plugin Email Delivery: Craft CMS Native Support: - Sendmail - SMTP - Gmail Recommended Third-Party Plugins: - Amazon SES (by PutYourLightsOn) - ElasticEmail (by Bert Oost) - MailerSend (by Studio Espresso) - Note: Has daily request quotas and rate limits. - Mailgun (by Pixel & Tonic) - Mandrill (by Pixel & Tonic) - Postmark (by Pixel & Tonic) - Supports broadcast message streams. - SendGrid (by PutYourLightsOn) Preferred Services: - Mailgun (https://www.mailgun.com/) - Postmark (https://postmarkapp.com/) - Both are recommended for ease of use and reliable deliverability. ``` -------------------------------- ### Campaign URI Format Configuration Source: https://putyourlightson.com/plugins/campaign/index Defines the URL structure for campaigns. This setting allows for dynamic URI generation using campaign properties like slugs. ```text campaign/{slug} ``` -------------------------------- ### Run Pending Sendouts (Craft Console) Source: https://putyourlightson.com/plugins/campaign/index Commands to manage the sending of pending sendouts for the campaign plugin using the Craft CMS console runner. These commands can either run the sendouts directly or queue them for later processing. ```shell php craft campaign/sendouts/run ``` ```shell php craft campaign/sendouts/queue ``` -------------------------------- ### Template Caching with Craft CMS Source: https://putyourlightson.com/plugins/campaign/index Demonstrates how to use Craft CMS's {% cache %} tag with 'globally' and 'using key' parameters to reduce rendering time for static parts of email campaign templates. This helps improve performance by caching specific sections of the template based on campaign ID. ```twig Hello {{ contact.firstName }}, {# Cache the last 10 entries. #} {% cache globally using key 'campaign-' ~ campaign.id %} {% for entry in craft.entries.limit(10).all() %} {{ entry.title }} {% endfor %} {% endcache %} ``` -------------------------------- ### Subscribe Contact to Mailing List using PHP Source: https://putyourlightson.com/plugins/campaign/index Creates and subscribes a contact to a specified mailing list using the `FormService::createAndSubscribe()` method. This method respects the mailing list's verification settings and handles retrieving mailing list details and request parameters. ```php use putyourlightson\campaign\Campaign; use putyourlightson\campaign\elements\MailingListElement; use craft\errors\NotFoundHttpException; $mailingList = MailingListElement::find()->slug($mailingListSlug)->one(); if ($mailingList === null) { throw new NotFoundHttpException('Mailing list not found'); } $fieldValues = Craft::$app->getRequest()->getBodyParam('fields'); $source = Craft::$app->getRequest()->getReferrer(); Campaign::$plugin->forms->createAndSubscribeContact($email, $fieldValues, $mailingList, 'web', $source); ``` -------------------------------- ### Campaign HTML Template Configuration Source: https://putyourlightson.com/plugins/campaign/index Specifies the Twig template file used to render the HTML content when a campaign's URL is requested. The path is relative to Craft's main templates folder. ```text _campaign/html ``` -------------------------------- ### Sendout Element Query Parameters Source: https://putyourlightson.com/plugins/campaign/index Defines the available parameters for querying SendoutElement objects in the Campaign plugin. These parameters allow for filtering sendouts based on unique IDs, types, associated campaigns, mailing lists, and segments. ```APIDOC SendoutElementQuery: - sid: (string) Only fetch sendouts with the given SID (unique sendout ID). - sendoutType: (string) Only fetch sendouts with the given sendout type (regular / scheduled / automated). - campaignId: (integer) Only fetch sendouts with the given campaign ID. - mailingListId: (integer) Only fetch sendouts with the given mailing list ID. - segmentId: (integer) Only fetch sendouts with the given segment ID. (Supports all parameters available to standard Craft Element Queries like id, slug, status, orderBy, etc.) ``` -------------------------------- ### Create Contact Update Form Source: https://putyourlightson.com/plugins/campaign/index Enables contacts to update their personal data and mailing list subscriptions. Requires the contact's `cid` and `uid` for authentication and uses hidden inputs for these identifiers. ```twig {% set contact = craft.campaign.contacts.userId(currentUser.id).one() %} ``` -------------------------------- ### Campaign Element Query Parameters Source: https://putyourlightson.com/plugins/campaign/index Details additional parameters supported by the `CampaignElementQuery` class for filtering campaigns, beyond standard Craft element query parameters. Includes `campaignType` and `campaignTypeId`. ```APIDOC CampaignElementQuery: campaignType(string|array|CampaignTypeModel): Only fetch campaigns that belong to a given campaign type(s). Accepted values include a campaign type handle, an array of campaign type handles, or a CampaignTypeModel object. campaignTypeId(int|array): Only fetch campaigns that belong to a given campaign type(s), referenced by its ID. ``` -------------------------------- ### Email Template Tags Usage Source: https://putyourlightson.com/plugins/campaign/index Illustrates the usage of available template tags within email campaigns, such as 'campaign', 'browserVersionUrl', 'contact', and 'unsubscribeUrl'. It shows conditional rendering based on the presence of these tags, ensuring content is only displayed when available. ```twig {% if browserVersionUrl %} View this email in your browser {% endif %} {% if contact.firstName %} Hello {{ contact.firstName }}, {% endif %} Welcome to this month's newsletter in which we have some fascintaing announcements! {{ campaign.announcements }} {% if unsubscribeUrl %} Unsubscribe from this mailing list {% endif %} ``` -------------------------------- ### Contact Element Query Parameters Source: https://putyourlightson.com/plugins/campaign/index Details parameters available for querying Contact elements via the ContactElementQuery class, extending standard Craft element query capabilities. These parameters allow for precise filtering of contacts based on various criteria. ```APIDOC ContactElementQuery: Extends: Craft Element Query Parameters: userId (int): Only fetch a contact that is synced to the given user ID. cid (string): Only fetch a contact with the given CID (unique contact ID). email (string): Only fetch a contact with the given email address. mailingListId (int): Only fetch contacts with the given mailing list ID. segmentId (int): Only fetch contacts with the given segment ID. ``` -------------------------------- ### Register Custom Contact Condition Rule (PHP) Source: https://putyourlightson.com/plugins/campaign/index Demonstrates how to register a custom condition rule type with the `ContactCondition` class using an event listener. This allows developers to create their own specific contact segmentation logic. ```php use Craft; use craft\base\conditions\BaseCondition; use craft\events\RegisterConditionRuleTypesEvent; use putyourlightson\campaign\elements\conditions\contacts\ContactCondition; use yii\base\Event; Event::on( ContactCondition::class, BaseCondition::EVENT_REGISTER_CONDITION_RULE_TYPES, function (RegisterConditionRuleTypesEvent $event) { $event->conditionRuleTypes[] = MyCustomConditionRule::class; } ); ``` -------------------------------- ### Unsubscribe Contact Source: https://putyourlightson.com/plugins/campaign/index Demonstrates how to unsubscribe a contact from a specific mailing list using the `FormService::unsubscribeContact()` method. It requires retrieving the contact and mailing list objects first. ```php use putyourlightson\campaign\Campaign; use putyourlightson\campaign\elements\MailingListElement; // Assuming $email and $mailingListSlug are defined $contact = Campaign::$plugin->contacts->getContactByEmail($email); $mailingList = MailingListElement::find()->slug($mailingListSlug)->one(); Campaign::$plugin->forms->unsubscribeContact($contact, $mailingList); ``` -------------------------------- ### Register Custom Sendout Condition Rule (PHP) Source: https://putyourlightson.com/plugins/campaign/index Demonstrates how to register a custom condition rule type for sendouts within the campaign plugin. This involves listening to the `EVENT_REGISTER_CONDITION_RULE_TYPES` event on `SendoutScheduleCondition` and adding a custom rule class. ```php use Craft; use craft\base\conditions\BaseCondition; use craft\events\RegisterConditionRuleTypesEvent; use putyourlightson\campaign\elements\conditions\sendouts\SendoutScheduleCondition; use yii\base\Event; Event::on( SendoutScheduleCondition::class, BaseCondition::EVENT_REGISTER_CONDITION_RULE_TYPES, function (RegisterConditionRuleTypesEvent $event) { $event->conditionRuleTypes[] = MyCustomConditionRule::class; } ); ``` -------------------------------- ### Configure Custom Queue for Campaign Plugin Source: https://putyourlightson.com/plugins/campaign/index This snippet shows how to configure the Campaign plugin to use a custom queue in Craft CMS. This is recommended for handling long-running sendout jobs independently, improving performance and preventing timeouts. ```php return [ 'bootstrap' => ['customQueue'], 'components' => [ 'plugins' => [ 'pluginConfigs' => [ 'campaign' => [ 'queue' => 'customQueue', ], ], ], 'customQueue' => [ 'class' => \craft\queue\Queue::class, 'channel' => 'custom', ], ], ]; ``` -------------------------------- ### Subscribe Verification Email Template Source: https://putyourlightson.com/plugins/campaign/index Template for the verification email sent to new contacts. Supports tags like `pendingContact.fieldData.firstName` and `url` for personalization. Custom fields are accessed via `fieldData`. ```twig Hello {{ pendingContact.fieldData.firstName }}, Please verify your email address by clicking on the following link: {{ url }} ``` -------------------------------- ### Create User-Specific Mailing List Unsubscribe Form Source: https://putyourlightson.com/plugins/campaign/index Allows logged-in users to unsubscribe from mailing lists they are subscribed to. It retrieves the current user's contact and iterates through their associated mailing lists. ```twig {% set contact = craft.campaign.contacts.userId(currentUser.id).one() %} {% set mailingLists = contact.getMailingLists() %} {% for mailingList in mailingLists %} {% endfor %} ``` -------------------------------- ### Create Mailing List Unsubscribe Form Source: https://putyourlightson.com/plugins/campaign/index Provides a Twig/HTML form for contacts to unsubscribe from a specific mailing list. Requires the 'Unsubscribe Form Allowed' setting to be enabled for the mailing list type. ```twig {% set mailingList = craft.campaign.mailingLists.id(7).one() %} ``` -------------------------------- ### Contact Data Fields Source: https://putyourlightson.com/plugins/campaign/index Details the data fields stored for contacts within the Campaign plugin, including activity-related information and GeoIP data. These fields are crucial for understanding contact behavior and complying with privacy policies. ```APIDOC Contact: email: The contact's email address. country: The country the contact was last active from (if GeoIP is enabled). geoIp: GeoIP location data, including continent, country, region, city, postcode, and timezone (if GeoIP is enabled). continentCode: Continent code. continentName: Continent name. countryCode: Country code. countryName: Country name. regionCode: Region code. regionName: Region name. city: City name. postCode: Postal code. timeZone: Time zone. device: The device the contact was last active from. os: The operating system (OS) the contact was last active from. client: The web browser client the contact was last active from. lastActivity: Timestamp of the contact's last activity. complained: Timestamp of the contact's spam complaint. bounced: Timestamp of the contact's bounced email. verified: Timestamp of the contact's verified email (through double opt-in). ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.