Exemple #1
0
        public void CampaignCreate_Successful()
        {
            // Arrange
            MailChimpManager       mc             = new MailChimpManager(TestGlobal.Test_APIKey);
            ListResult             lists          = mc.GetLists();
            string                 listID         = lists.Data[1].Id;
            CampaignSegmentOptions segmentOptions = new CampaignSegmentOptions();

            segmentOptions.Match = "All";
            string dateListCreated = lists.Data[1].DateCreated;

            segmentOptions.Conditions = new List <CampaignSegmentCriteria>();
            segmentOptions.Conditions.Add(new CampaignSegmentCriteria {
                Field = "date", Operator = "eq", Value = dateListCreated
            });
            CampaignCreateContent content = new CampaignCreateContent();

            content.HTML = "<p>Testing</p>";
            CampaignCreateOptions options = new CampaignCreateOptions();

            options.Title     = "Testing";
            options.ListId    = listID;
            options.ToName    = "Test Name";
            options.FromEmail = "*****@*****.**";
            options.FromName  = "Testing Company Name";
            options.Subject   = "Test Subject";

            //Act
            Campaign result = mc.CreateCampaign("regular", options, content, segmentOptions, null);

            // Assert
            Assert.IsNotNull(result);
        }
Exemple #2
0
        public string SendTransactionalEmail(string recipientEmail, string content, string title, string fromName, string fromEmail, string subject
                                             , string emailName, string listName = null)
        {
            var emailContent = new CampaignCreateContent {
                HTML = content
            };
            var emailOptions = 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();
            }

            emailOptions.ListId     = list.Id;
            emailOptions.AutoFooter = false;
            var batchEmailParameters = new List <BatchEmailParameter>();
            var emailParameters      = new List <EmailParameter>();

            batchEmailParameters.Add(new BatchEmailParameter
            {
                Email = new EmailParameter {
                    Email = recipientEmail
                },
            });
            emailParameters.Add(new EmailParameter {
                Email = recipientEmail
            });
            mailChimpManager.BatchSubscribe(list.Id, batchEmailParameters, doubleOptIn: false, updateExisting: true, replaceInterests: false);
            var segmentResult = mailChimpManager.AddStaticSegment(list.Id, emailName);

            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()
            });

            Campaign result = mailChimpManager.CreateCampaign("regular", emailOptions, emailContent, segmentOptions, null);
            string   cId    = result.Id;

            mailChimpManager.SendCampaign(cId);
            return(cId);
        }
Exemple #3
0
        public void SendCampaign()
        {
            if ((cmbTemplate.SelectedIndex > 0 && cmbTemplate.SelectedItem != null) && (cmbList.SelectedIndex > 0 && cmbList.SelectedItem != null))
            {
                Int32 selectedTemplateId = Convert.ToInt32(cmbTemplate.SelectedValue);

                string selectedListId = cmbList.SelectedValue.ToString();
                //IEnumerable<string> listEnum = new string[] { selectedListId.ToString() };

                //--Create Campaign
                CampaignSegmentOptions segmentOptions = new CampaignSegmentOptions();
                segmentOptions.Match = "All";
                CampaignCreateOptions options = new CampaignCreateOptions();
                options.Title      = mailchimptitle;
                options.ListId     = selectedListId;
                options.ToName     = mailchimpToName;
                options.FromEmail  = mailchimpFromEmail;
                options.FromName   = mailchimpFromName;
                options.Subject    = mailchimpSubject;
                options.TemplateID = selectedTemplateId;
                CampaignCreateContent content = new CampaignCreateContent();
                Campaign result = mc.CreateCampaign("regular", options, content, null, null);


                //GetMemberInfo
                if (chkEnableSMS.Checked)
                {
                    List <EmailParameter> emails  = new List <EmailParameter>();
                    MembersResult         results = mc.GetAllMembersForList(selectedListId, "subscribed", 0, 100);
                    foreach (var i in results.Data)
                    {
                        foreach (var j in i.MemberMergeInfo)
                        {
                            if (j.Key.Trim() == "PHONE")
                            {
                                if (!string.IsNullOrEmpty(j.Value.ToString()))
                                {
                                    //  SmsSender.SendSMS(j.Value.ToString(), "919460264151", "5b2a23d7", "59d9fa03", Uri.EscapeUriString("Test Message"));
                                }
                            }
                        }
                    }
                }
            }
            MessageBox.Show("Success");
            this.Close();
        }
Exemple #4
0
        /// <summary>
        ///Create a new draft campaign to send. You can not have more than 32,000 campaigns in your account.
        ///See http://apidocs.mailchimp.com/api/2.0/campaigns/create.php for explanation of full options.
        /// </summary>
        /// <param name="type">The Campaign Type to create - one of "regular", "plaintext", "absplit", "rss", "auto"</param>
        /// <param name="options">A struct of the standard options for this campaign.</param>
        /// <param name="content">The content for this campaign </param>
        /// <param name="segmentOptions">optional - if you wish to do Segmentation with this campaign this array should contain: see CampaignSegmentTest(). It's suggested that you test your options against campaignSegmentTest().</param>
        /// <param name="typeOptions">optional - various extra options based on the campaign type</param>
        /// <returns></returns>
        public Campaign CreateCampaign(string type, CampaignCreateOptions options, CampaignCreateContent content, CampaignSegmentOptions segmentOptions = null, CampaignTypeOptions typeOptions = null)
        {
            //  Our api action:
            string apiAction = "campaigns/create";

            //  Create our arguments object:
            object args = new
            {
                apikey       = this.APIKey,
                type         = type,
                options      = options,
                content      = content,
                segment_opts = segmentOptions,
                type_opts    = typeOptions
            };

            //  Make the call:
            return(MakeAPICall <Campaign>(apiAction, args));
        }
Exemple #5
0
        public string SendCampaign(MailChimpCampaignRequest request)
        {
            string Id = string.Empty;

            try
            {
                CampaignCreateOptions cco = new CampaignCreateOptions()
                {
                    ListId    = request.ListId,
                    Title     = request.Name,
                    Subject   = request.Subject,
                    FromEmail = request.FromEmail,
                    FromName  = request.FromName
                };

                Dictionary <string, string> sections = new Dictionary <string, string>();


                CampaignCreateContent ccc = new CampaignCreateContent()
                {
                    Sections = sections,
                    HTML     = request.Text
                };

                IMailChimpManager mailChimpManager = new MailChimp.MailChimpManager(request.ApiKey);
                var campaignMailChimp = mailChimpManager.CreateCampaign("regular", cco, ccc, null, null);

                if (!String.IsNullOrEmpty(campaignMailChimp.Id))
                {
                    var resultSendCampaign = mailChimpManager.SendCampaign(campaignMailChimp.Id);
                    Id = resultSendCampaign.Complete ? campaignMailChimp.Id : string.Empty;
                }
            }
            catch (Exception e)
            {
                var messageException = telemetria.MakeMessageException(e, System.Reflection.MethodBase.GetCurrentMethod().Name);
                telemetria.Critical(messageException);
            }
            return(Id);
        }
Exemple #6
0
        public string CreateCampaignMailChimp(string idCampaign)
        {
            string idList     = null;
            string apiKey     = null;
            int    idTemplate = 0;
            string subject    = "";
            string fromEmail  = "";
            string fromName   = "";

            var campaign = GetCampaignById(idCampaign);
            var product  = GetProductByIdProduct(campaign.PRODUCT.IdProduct);

            try
            {
                if (product.PRODUCT_SETTINGS != null && product.PRODUCT_SETTINGS.Any())
                {
                    foreach (var item in product.PRODUCT_SETTINGS)
                    {
                        apiKey     = item.SettingName.Equals("mailChimpApiToken") ? item.SettingValue : apiKey;
                        idList     = item.SettingName.Equals("mailChimpList") ? item.SettingValue : idList;
                        idTemplate = item.SettingName.Equals("mailChimpTemplate") ? Convert.ToInt32(item.SettingValue) : idTemplate;
                    }
                }

                if (campaign.CAMPAIGN_SETTINGS != null && campaign.CAMPAIGN_SETTINGS.Any())
                {
                    foreach (var setting in campaign.CAMPAIGN_SETTINGS)
                    {
                        subject   = setting.SettingName.Equals("mailChimpSubject") ? setting.SettingValue : subject;
                        fromName  = setting.SettingName.Equals("mailChimpFromName") ? setting.SettingValue : fromName;
                        fromEmail = setting.SettingName.Equals("mailChimpFromEmail") ? setting.SettingValue : fromEmail;
                    }
                }

                CampaignCreateOptions cco = new CampaignCreateOptions()
                {
                    ListId    = idList,
                    Title     = campaign.Name,
                    Subject   = subject,
                    FromEmail = fromEmail,
                    FromName  = fromName
                };

                Dictionary <string, string> sections = new Dictionary <string, string>();
                if (product.SITE.CATEGORY != null && product.SITE.CATEGORY.Any())
                {
                    foreach (var item in product.SITE.CATEGORY)
                    {
                        sections.Add(item.IdCategory.ToString(), item.Description);
                    }
                }

                CampaignCreateContent ccc = new CampaignCreateContent()
                {
                    Sections = sections,
                    HTML     = campaign.AdText
                };

                IMailChimpManager mailChimpManager = new MailChimpManager(apiKey);
                var campaignMailChimp = mailChimpManager.CreateCampaign("regular", cco, ccc, null, null);

                if (campaign != null && !String.IsNullOrEmpty(campaignMailChimp.Id))
                {
                    var resultSendCampaign = mailChimpManager.SendCampaign(campaignMailChimp.Id);
                    return(resultSendCampaign.Complete ? campaignMailChimp.Id : null);
                }
            }
            catch (Exception e)
            {
                var messageException = telemetria.MakeMessageException(e, System.Reflection.MethodBase.GetCurrentMethod().Name);
                telemetria.Critical(messageException);
            }
            return(null);
        }
Exemple #7
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);
        }