Exemple #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");
        }
Exemple #2
0
        public void UpdateMergeVar_Successful()
        {
            //  Arrange
            MailChimpManager mc      = new MailChimpManager(TestGlobal.Test_APIKey);
            ListResult       lists   = mc.GetLists();
            MergeVarOptions  options = new MergeVarOptions()
            {
                FieldType = "url",
                HelpText  = "A url, such as https://github.com/danesparza/MailChimp.NET"
            };

            // we don't want to have existing merge vars interfere
            try { mc.DeleteMergeVar(lists.Data.First().Id, "TESTUPDVAR"); }
            catch { }
            try { mc.DeleteMergeVar(lists.Data.First().Id, "UPDATEDVAR"); }
            catch { }
            mc.AddMergeVar(lists.Data.First().Id, "TESTUPDVAR", "Test Value", options);

            // Act
            options = new MergeVarOptions()
            {
                Tag      = "UPDATEDVAR",
                HelpText = "Any url you like"
            };
            MergeVarItemResult result = mc.UpdateMergeVar(lists.Data.First().Id, "TESTUPDVAR", options);

            // Assert
            Assert.IsNotNull(result);
            Assert.IsTrue(result.Tag == "UPDATEDVAR");

            // Clean up
            mc.DeleteMergeVar(lists.Data.First().Id, "UPDATEDVAR");
        }
Exemple #3
0
        public void DeleteMergeVar_Successful()
        {
            //  Arrange
            MailChimpManager mc    = new MailChimpManager(TestGlobal.Test_APIKey);
            ListResult       lists = mc.GetLists();

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

            // Act
            CompleteResult result = mc.DeleteMergeVar(lists.Data.First().Id, "TESTDELETE");

            // Assert
            Assert.IsNotNull(result);
            Assert.IsTrue(result.Complete);
        }
Exemple #4
0
    public static int ListsMergeVarAdd(SqlString apikey, SqlString list_id, SqlString tag, SqlString name, SqlString opt_field_type, SqlBoolean opt_req,
                                       SqlBoolean opt_public, SqlBoolean opt_show, SqlInt32 opt_order, SqlString opt_default_value, SqlString opt_helptext, SqlString opt_choices,
                                       SqlString opt_dateformat, SqlString opt_phoneformat, SqlString opt_defaultcountry)
    {
        try
        {
            MailChimpManager mc = new MailChimpManager(apikey.ToString());

            MergeVarItemResult result = mc.AddMergeVar(list_id.ToString(), tag.ToString(), name.ToString(), new MergeVarOptions
            {
                FieldType      = opt_field_type.ToString(),
                Required       = opt_req.IsTrue,
                Public         = opt_public.IsTrue,
                Show           = opt_show.IsTrue,
                Order          = opt_order.Value,
                DefaultValue   = opt_default_value.ToString(),
                HelpText       = opt_helptext.ToString(),
                Choices        = opt_choices.ToString().Split(','),
                DateFormat     = opt_dateformat.ToString(),
                PhoneFormat    = opt_phoneformat.ToString(),
                DefaultCountry = opt_defaultcountry.ToString()
            });

            SqlDataRecord record = new SqlDataRecord(MergeVarResultsMetaData);
            record.SetString(0, result.Name);
            record.SetBoolean(1, result.Required);
            record.SetString(2, result.FieldType);
            record.SetBoolean(3, result.Public);
            record.SetBoolean(4, result.Show);
            record.SetInt32(5, result.Order);
            record.SetString(6, result.DefaultValue);
            record.SetString(7, result.HelpText);
            record.SetString(8, (result.Choices == null) ? string.Empty : string.Join(",", result.Choices));
            record.SetString(9, result.Size);
            record.SetString(10, result.Tag);
            record.SetInt32(11, result.Id);

            SqlContext.Pipe.Send(record);
        }
        catch (Exception ex)
        {
            SqlContext.Pipe.Send(ex.Message);
            SqlContext.Pipe.Send(ex.StackTrace);
            return(1);
        }
        return(0);
    }
Exemple #5
0
        public void ResetMergeVar_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, "TESTRESET"); }
            catch { }
            mc.AddMergeVar(lists.Data.First().Id, "TESTRESET", "Test Value");

            // Act
            CompleteResult result = mc.ResetMergeVar(lists.Data.First().Id, "TESTRESET");

            // Assert
            Assert.IsNotNull(result);
            Assert.IsTrue(result.Complete);

            // Clean up
            mc.DeleteMergeVar(lists.Data.First().Id, "TESTRESET");
        }
        public void SetMergeVar_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, "TESTSET"); }
            catch { }
            mc.AddMergeVar(lists.Data.First().Id, "TESTSET", "Test Value");

            // Act
            CompleteResult result = mc.SetMergeVar(lists.Data.First().Id, "TESTSET", "Set");

            // Assert
            Assert.IsNotNull(result);
            Assert.IsTrue(result.Complete);


            // Clean up
            mc.DeleteMergeVar(lists.Data.First().Id, "TESTSET");
        }
        public void DeleteMergeVar_Successful()
        {
            //  Arrange
            MailChimpManager mc = new MailChimpManager(TestGlobal.Test_APIKey);
            ListResult lists = mc.GetLists();

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

            // Act
            CompleteResult result = mc.DeleteMergeVar(lists.Data.First().Id, "TESTDELETE");

            // Assert
            Assert.IsNotNull(result);
            Assert.IsTrue(result.Complete);
        }
        public void UpdateMergeVar_Successful()
        {
            //  Arrange
            MailChimpManager mc = new MailChimpManager(TestGlobal.Test_APIKey);
            ListResult lists = mc.GetLists();
            MergeVarOptions options = new MergeVarOptions()
            {
                FieldType = "url",
                HelpText = "A url, such as https://github.com/danesparza/MailChimp.NET"
            };
            // we don't want to have existing merge vars interfere
            try { mc.DeleteMergeVar(lists.Data.First().Id, "TESTUPDVAR"); }
            catch { }
            try { mc.DeleteMergeVar(lists.Data.First().Id, "UPDATEDVAR"); }
            catch { }
            mc.AddMergeVar(lists.Data.First().Id, "TESTUPDVAR", "Test Value", options);

            // Act
            options = new MergeVarOptions()
            {
                Tag = "UPDATEDVAR",
                HelpText = "Any url you like"
            };
            MergeVarItemResult result = mc.UpdateMergeVar(lists.Data.First().Id, "TESTUPDVAR", options);

            // Assert
            Assert.IsNotNull(result);
            Assert.IsTrue(result.Tag == "UPDATEDVAR");

            // Clean up
            mc.DeleteMergeVar(lists.Data.First().Id, "UPDATEDVAR");
        }
        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");
        }
        public void AddMergeVar_Successful()
        {
            //  Arrange
            MailChimpManager mc = new MailChimpManager(TestGlobal.Test_APIKey);
            ListResult lists = mc.GetLists();

            MergeVarOptions options = new MergeVarOptions()
            {
                FieldType = "url",
                HelpText = "A url, such as https://github.com/danesparza/MailChimp.NET"
            };

            // Act
            MergeVarItemResult result = mc.AddMergeVar(lists.Data.First().Id, "TESTVALUE", "Test Value", options);

            // Assert
            Assert.IsNotNull(result);
        }
Exemple #11
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);
        }