コード例 #1
0
        public void GetMergeVars_Successful()
        {
            //  Arrange
            MailChimpManager mc    = new MailChimpManager(TestGlobal.Test_APIKey);
            ListResult       lists = mc.GetLists();

            // we don't want to have an existing merge var interfere
            try { mc.DeleteMergeVar(lists.Data.First().Id, "TESTGETVAR"); }
            catch { }
            MergeVarOptions options = new MergeVarOptions()
            {
                FieldType = "url",
                HelpText  = "A url, such as https://github.com/danesparza/MailChimp.NET"
            };

            mc.AddMergeVar(lists.Data.First().Id, "TESTGETVAR", "Test Value", options);

            // Act
            MergeVarResult result = mc.GetMergeVars(lists.Data.Select(l => l.Id));

            // Assert
            Assert.IsNotNull(result.Data);
            Assert.IsTrue(result.Data.Any(d => d.MergeVars.Any(m => m.Tag == "TESTGETVAR")));

            // Clean up
            mc.DeleteMergeVar(lists.Data.First().Id, "TESTGETVAR");
        }
コード例 #2
0
        /// <summary>
        /// Validate nexmo fields
        /// </summary>
        /// <returns></returns>
        public bool ValidateFields()
        {
            if (cmbCampaign.SelectedIndex <= 0 || cmbCampaign.SelectedItem == null)
            {
                MessageBox.Show("Please select Campaign.", "Campaign", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                cmbCampaign.Focus();
                return(false);
            }

            if (chkEnableSMS.Checked && pnlNexmoMessage.Visible)
            {
                if (cmbFieldPhone.SelectedIndex <= 0 || cmbFieldPhone.SelectedItem == null)
                {
                    MessageBox.Show("Please select recipient field.", "Recipient field", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    cmbFieldPhone.Focus();
                    return(false);
                }
                if (string.IsNullOrEmpty(txtMessage.Text.Trim()))
                {
                    MessageBox.Show("Please enter Message.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    txtMessage.Focus();
                    return(false);
                }
            }
            else
            {
                if (cmbCampaign.SelectedIndex > 0)
                {
                    bool           isFoundPhoneField = false;
                    string         listId            = string.Empty;
                    string         campaignId        = cmbCampaign.SelectedValue.ToString();
                    CampaignFilter options           = new CampaignFilter();
                    options.CampaignId = campaignId;
                    CampaignListResult campaign = Manager.GetCampaigns(options);
                    foreach (var data in campaign.Data)
                    {
                        listId = data.ListId;
                        break;
                    }
                    IEnumerable <string> listEnum = new string[] { listId.ToString() };
                    MergeVarResult       results  = Manager.GetMergeVars(listEnum);
                    foreach (var list in results.Data)
                    {
                        foreach (var mergeVars in list.MergeVars)
                        {
                            if (mergeVars.FieldType == "phone")
                            {
                                isFoundPhoneField = true;
                            }
                        }
                        if (!isFoundPhoneField)
                        {
                            MessageBox.Show("No Phone type field available in " + list.Name + "list.", "Phone field", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            return(false);
                        }
                    }
                }
            }
            return(true);
        }
コード例 #3
0
    public static IEnumerable ListsMergeVars(SqlString apikey, SqlString list_id)
    {
        List <MergeVarItemResult> vars = new List <MergeVarItemResult>();

        MailChimpManager mc     = new MailChimpManager(apikey.ToString());
        MergeVarResult   result = mc.GetMergeVars(new List <String> {
            list_id.ToString()
        });

        if (result.Data.Count > 0)
        {
            vars.AddRange(result.Data[0].MergeVars);
        }

        return(vars);
    }
コード例 #4
0
        /// <summary>
        /// Bind phone type field
        /// </summary>
        public void BindFields()
        {
            List <MailChimpList> mailChimpField      = new List <MailChimpList>();
            List <MailChimpList> mailChimpPhoneField = new List <MailChimpList>();

            mailChimpPhoneField.Add(new MailChimpList("0", "Select recipient field"));
            string         listId     = string.Empty;
            string         campaignId = cmbCampaign.SelectedValue.ToString();
            CampaignFilter options    = new CampaignFilter();

            options.CampaignId = campaignId;
            CampaignListResult lists = Manager.GetCampaigns(options);

            foreach (var data in lists.Data)
            {
                listId = data.ListId;
                break;
            }
            //--member of list
            List <EmailParameter> emails   = new List <EmailParameter>();
            IEnumerable <string>  listEnum = new string[] { listId.ToString() };
            MergeVarResult        results  = Manager.GetMergeVars(listEnum);

            foreach (var list in results.Data)
            {
                foreach (var mergeVars in list.MergeVars)
                {
                    mailChimpField.Add(new MailChimpList(mergeVars.Id.ToString(), mergeVars.Tag));
                    // Get only Phone type field value
                    if (mergeVars.FieldType == "phone")
                    {
                        mailChimpPhoneField.Add(new MailChimpList(mergeVars.Id.ToString(), mergeVars.Tag));
                    }
                }
            }

            if (mailChimpField != null && mailChimpField.Count > 0)
            {
                lstboxFields.Items.Clear();
                lstboxFields.Items.AddRange(mailChimpField.ToArray());
                lstboxFields.DisplayMember = "Name";

                cmbFieldPhone.DisplayMember = "Name";
                cmbFieldPhone.ValueMember   = "Id";
                cmbFieldPhone.DataSource    = mailChimpPhoneField;
            }
        }
コード例 #5
0
        public string SendCampaign(int campaignId, string campaignName, IEnumerable <EmailRecipient> emails, IEnumerable <Company> Companies, string title, string subject,
                                   string content, string fromEmail, string fromName, string campaignKey, int accountId, byte?campaignType, string listName = null)
        {
            content      = content.Replace("*|CAMPID|*", campaignId.ToString()).Replace("<o:p>", "").Replace("</o:p>", "");//.FormatHTML();
            campaignName = Guid.NewGuid().ToString();
            var campaignCreateContent = campaignType == 132 ? new CampaignCreateContent {
                Text = content
            } : new CampaignCreateContent {
                HTML = content
            };
            var campaignCreateOptions = new CampaignCreateOptions
            {
                Title     = title,
                FromEmail = fromEmail,
                FromName  = fromName,
                Subject   = subject,
            };

            var list = default(ListInfo);

            if (string.IsNullOrWhiteSpace(listName))
            {
                list = mailChimpManager.GetLists().Data[0];
            }
            else
            {
                list = mailChimpManager.GetLists().Data.Where(li => string.Equals(li.Name, listName, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
            }

            campaignCreateOptions.ListId     = list.Id;
            campaignCreateOptions.AutoFooter = false;
            //subscribe every id to the list
            var batchEmailParameters           = new List <BatchEmailParameter>();
            var emailParameters                = new List <EmailParameter>();
            IEnumerable <string> mergervarlist = new List <string> {
                list.Id
            };
            MergeVarResult resultdata = mailChimpManager.GetMergeVars(mergervarlist);
            IEnumerable <MergeVarItemResult> mergevardata = resultdata.Data.Select(x => x.MergeVars).FirstOrDefault();

            // delete all mergervars
            foreach (var mergevar in mergevardata)
            {
                try
                {
                    mailChimpManager.DeleteMergeVar(list.Id, mergevar.Tag);
                }
                catch (Exception ex)
                {
                    Logger.Current.Error("Exception" + ex);
                }
            }

            //add mergevars
            var pc           = emails.FirstOrDefault();
            var fields       = (pc.ContactFields != null) ? pc.ContactFields.Keys : new List <string>();
            var defaultValue = string.Empty;
            var displayName  = string.Empty;

            foreach (var field in fields)
            {
                if (field.Contains("CF") || field.Contains("DF"))
                {
                    defaultValue = "XXXXX";
                    displayName  = field;
                }
                else
                {
                    var propInfo = typeof(RecipientMergeVar).GetProperties().Where(p => p.Name == field).FirstOrDefault();
                    if (propInfo != null)
                    {
                        displayName = propInfo.GetCustomAttributes(typeof(DisplayNameAttribute), true).Cast <DisplayNameAttribute>().FirstOrDefault().DisplayName;
                    }
                }
                mailChimpManager.AddMergeVar(list.Id, field, displayName, new MergeVarOptions()
                {
                    FieldType = "text", DefaultValue = defaultValue
                });
            }
            //set merge vars
            string replaceValue = string.Empty;

            foreach (var contact in emails)
            {
                var rmv = new RecipientMergeVar();

                if (contact.ContactFields != null && contact.ContactFields.Keys != null)
                {
                    var keys       = contact.ContactFields.Keys;
                    var properties = rmv.GetType().GetProperties();
                    foreach (var key in keys)
                    {
                        replaceValue = contact.ContactFields[key].ToString();
                        if (key.Contains("CF") || key.Contains("DF"))
                        {
                            //TODO

                            /*
                             * Write code to handle muliti select custom field.
                             */
                            try
                            {
                                mailChimpManager.SetMergeVar(list.Id, key, replaceValue);
                            }
                            catch (MailChimp.Errors.MailChimpAPIException e)
                            {
                                ExceptionHandler.Current.HandleException(e, DefaultExceptionPolicies.LOG_ONLY_POLICY);
                            }
                            catch (Exception e)
                            {
                                ExceptionHandler.Current.HandleException(e, DefaultExceptionPolicies.LOG_ONLY_POLICY);
                            }
                        }
                        else
                        {
                            var pi = properties.Where(p => p.Name == key).FirstOrDefault();
                            if (pi != null)
                            {
                                pi.SetValue(rmv, replaceValue);
                            }
                        }
                    }
                }
                batchEmailParameters.Add(new BatchEmailParameter
                {
                    Email = new EmailParameter {
                        Email = contact.EmailId
                    },
                    MergeVars = rmv
                });

                emailParameters.Add(new EmailParameter {
                    Email = contact.EmailId
                });
            }

            mailChimpManager.BatchSubscribe(list.Id, batchEmailParameters, doubleOptIn: false, updateExisting: true, replaceInterests: false);
            var segmentResult = mailChimpManager.AddStaticSegment(list.Id, campaignName);

            mailChimpManager.AddStaticSegmentMembers(list.Id, segmentResult.NewStaticSegmentID, emailParameters);
            var segmentOptions = new CampaignSegmentOptions();

            segmentOptions.Match      = "All";
            segmentOptions.Conditions = new List <CampaignSegmentCriteria>();
            segmentOptions.Conditions.Add(new CampaignSegmentCriteria {
                Field = "static_segment", Operator = "eq", Value = segmentResult.NewStaticSegmentID.ToString()
            });

            string cId = "";

            try
            {
                Campaign result = mailChimpManager.CreateCampaign("regular", campaignCreateOptions, campaignCreateContent, segmentOptions, null);
                cId = result.Id;
                mailChimpManager.SendCampaign(cId);
            }
            catch (MailChimp.Errors.MailChimpAPIException e)
            {
                ExceptionHandler.Current.HandleException(e, DefaultExceptionPolicies.LOG_AND_RETHROW_POLICY);
            }

            return(cId);
        }