BrendonKoz Posted 3 hours ago Posted 3 hours ago Constant Contact is not the easiest mail provider to work with as a developer. Although I had hoped to use a different provider prior to our website relaunch that likely won't happen for quite some time. In the process, I created a module to handle the handshake keep-alive that Constant Contact requires so that I could use a very simple, customized single-field form to allow visitors to subscribe to our newsletter. I share this module now. NOTE: Installs SubscribeToConstantContactKeepAlive.module REQUIRES: LazyCron (for the KeepAlive module) SubscribeToConstantContact A lightweight module for the ProcessWire CMS/CMF that lets you subscribe a user to a Constant Contact list. Heavily inspired by SubscribeToMailchimp. The basic idea // Easily subscribe a user with SubscribeToConstantContact $mod = $modules->get("SubscribeToConstantContact"); $mod->subscribe('email@example.com'); How To Install Download the zip file at Github or clone the repo into your site/modules If you downloaded the zip file, extract it in your sites/modules directory. You might have to change the folder's name to 'SubscribeToConstantContact'. Go to the modules admin page, click on refresh and install it. This will also install the associated SubscribeToConstantContactKeepAlive module which is necessary for keeping the API active. Setup at Constant Contact Log into your Constant Contact account and go to the Developer Portal > My Applications (you may have to click a "log in" button again) and edit an existing, or create a new application using Authorization Code Flow and Implicit Flow, and rotating refresh tokens. Retrieve the API Key and Client Secret. NOTE: The client secret may need to be recreated to retrieve it. Module Setup Put the API Key and Client Secret into the module settings (Processwire > Modules > Site > SubscribeToConstantContact), and use the "Redirect URL" as printed on the module config screen for your Constant Contact API application's Redirect URI field value. Submit the form to save the values from this step. Click on the generated URL, "Authorize and connect this module to your Constant Contact Application," on the module configuration screen. Values have been retrieved from the Constant Contact API. Click "Submit" to save them to the module configuration. OPTIONAL: Choose a default contact list for the module to subscribe contacts to. Usage // load module into template $mod = $modules->get("SubscribeToConstantContact"); // subscribe / update a user in your default audience $mod->subscribe('email@example.com'); // add additional fields to fill out user data // subscribe($email, $list_id, $parameters) // $list_id will default to the module's saved configuration value, if set // NOTE: Parameter values are not validated by the module, see the documentation for further info $mod->subscribe('email@example.com', null, ['first_name' => 'John', 'last_name' => 'Doe']); // Subscribe a user to a specific list (other than default) $mod->subscribe('email@example.com', 'adcdef12345', ['first_name' => 'John', 'last_name' => 'Doe']); Additional methods // Unsubscribe a user $mod->unsubscribe('email@example.com'); // Delete a user. Deleted users still exist in Constant Contact, but cannot be seen (in Constant Contact) or retrieved (via API) $mod->delete('email@example.com'); // Unsubscribe a user from a contact list (or array of lists) $mod->removeFromList('email@example.com'); $mod->removeFromList('email@example.com', 'abcdef1356'); $mod->removeFromList('email@example.com', ['abcdef1356']); Example Example usage after a form is submitted on your page: // ... validation of form data $mod = $modules->get("SubscribeToConstantContact"); $user_email = $sanitizer->email($input->post->email); $mod->subscribe($user_email); Troubleshooting In case of trouble check your ProcessWire warning logs. FAQ I can't see the subscriber in the contact list If you have enabled double opt-in in your Constant Contact settings, you will not see the subscriber until the confirmation link in the email sent by Constant Contact has been used; the user may have also been deleted. I get an error in my ProcessWire warning logs Check if you have the proper contact list ID and API Key. Check if you pass a valid email address. Make sure LazyCron is installed Go to the Constant Contact Developer Documentation for more information. *I have only done minimal testing on this module, so use with caution, and please report any bugs related to the stated purpose of the module. Contribution Pull requests welcome. (Especially for the awkward module setup/configuration flow.) 1
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now