Search code examples
pythondjangocampaign-monitorcreatesend

How can I easily send a preview email for a Campaign Monitor template?


I already migrated some of our own email templates into Campaign Monitor's system. I'd like to write a routine to send a preview email for a selected template. I'm using Python (https://github.com/campaignmonitor/createsend-python).

Looks like that I have to create a campaign which has a send_preview https://github.com/campaignmonitor/createsend-python/blob/master/createsend/campaign.py#L87 function. However I'd need to create a list also in order to have a campaign object only for this preview purpose. This feel like unnecessary / awkward. I feel like I'm tumbling down a rabbit hole. Is there a simpler way to send a preview email?


Solution

  • I reached out to Campaign Monitor support on the matter, and received the info that you need to create a campaign and a list for sending a test email. What I figured though the list can be empty, since you explicitly specify an email during send_preview. In my case I interpolate a GUID into the name of the list and campaign to guarantee the uniqueness of the name.

    Once the email is sent, you can right away delete these ephemeral campaign and list. Make sure you delete the campaign first, because you cannot delete a list which is tied to a campaign.

    cm_token = {'api_key': settings.CAMPAIGN_MONITOR_ADMIN_API_KEY}
    campaign_monitor = CreateSend(cm_token)
    cm_list = List(cm_token)
    list_id = cm_list.create(email_client_id, "Temporary empty list for preview {}".format(uuid.uuid1()), None, None, None)
    # cm_subscribers = []
    # for recipient in recipient_list:
    #     cm_subscriber = Subscriber(cm_token)
    #     cm_subscriber.add(list_id, recipient[0], "Preview recipient {}".format(uuid.uuid1()), None, None)
    #     cm_subscribers.append(cm_subscriber)
    cm_campaign = Campaign(cm_token)
    cm_campaign.create_from_template(
        customer_account.email_client_id,
        subject,
        "Temporary campaign for preview {}".format(template.id),  # name
        from_email,  # from_name
        from_email,
        from_email,  # reply_to
        [list_id],  # list_ids
        None,  # segment_ids
        template.template_id,
        {}  # template_content
    )
    cm_campaign.send_preview(recipient_list[0])
    # delete temporary list and campaign
    cm_campaign.delete()
    cm_list.delete()