Exemplo n.º 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");
        }
Exemplo n.º 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");
        }
Exemplo n.º 3
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");
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
0
    public static int ListsMergeVarDel(SqlString apikey, SqlString list_id, SqlString tag)
    {
        try
        {
            MailChimpManager mc = new MailChimpManager(apikey.ToString());

            CompleteResult result = mc.DeleteMergeVar(list_id.ToString(), tag.ToString());

            SqlDataRecord record = new SqlDataRecord(CompleteResultsMetaData);
            record.SetBoolean(0, result.Complete);

            SqlContext.Pipe.Send(record);
        }
        catch (Exception ex)
        {
            SqlContext.Pipe.Send(ex.Message);
            return(1);
        }
        return(0);
    }
Exemplo n.º 6
0
        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");
        }
Exemplo n.º 7
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);
        }
Exemplo n.º 8
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");
        }
Exemplo n.º 9
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");
        }
Exemplo n.º 10
0
        public void DeleteMergeVar_Successful()
        {
            //  Arrange
            MailChimpManager mc = new MailChimpManager(TestGlobal.Test_APIKey);
            ListResult lists = mc.GetLists();

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

            // Assert
            Assert.IsNotNull(result);
        }
Exemplo n.º 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);
        }