예제 #1
0
        public int Create(string externalKey, string layoutHtml, Priority priority = Priority.Medium)
        {
            if (externalKey.Length > Guid.Empty.ToString().Length)
            {
                throw new ArgumentException(
                          "externalKey too long, should be max length of " + Guid.Empty.ToString().Length, "externalKey");
            }

            if (_triggeredSendDefinitionClient.DoesTriggeredSendDefinitionExist(externalKey))
            {
                throw new Exception(string.Format("A TriggeredSendDefinition with external key {0} already exsits",
                                                  externalKey));
            }

            var dataExtensionExternalKey = ExternalKeyGenerator.GenerateExternalKey("data-extension-" + externalKey);

            if (!_dataExtensionClient.DoesDataExtensionExist(dataExtensionExternalKey))
            {
                var dataExtensionTemplateObjectId = _dataExtensionClient.RetrieveTriggeredSendDataExtensionTemplateObjectId();
                var dataExtensionFieldNames       = new HashSet <string> {
                    "Subject"
                };

                var replacementFieldNames = LayoutHtmlReplacementFieldNameParser.Parse(layoutHtml);
                foreach (var replacementFieldName in replacementFieldNames)
                {
                    dataExtensionFieldNames.Add(replacementFieldName);
                }

                _dataExtensionClient.CreateDataExtension(dataExtensionTemplateObjectId,
                                                         dataExtensionExternalKey,
                                                         "triggeredsend-" + externalKey,
                                                         dataExtensionFieldNames);
            }

            var emailName        = "email-" + externalKey;
            var emailExternalKey = ExternalKeyGenerator.GenerateExternalKey("email-" + externalKey);

            layoutHtml += EmailContentHelper.GetOpenTrackingTag() +
                          EmailContentHelper.GetCompanyPhysicalMailingAddressTags();

            var emailId = _emailRequestClient.CreateEmail(emailExternalKey, emailName, "%%Subject%%",
                                                          layoutHtml);

            var deliveryProfileExternalKey = ExternalKeyGenerator.GenerateExternalKey("blank-delivery-profile");

            _deliveryProfileClient.TryCreateBlankDeliveryProfile(deliveryProfileExternalKey);

            return(_triggeredSendDefinitionClient.CreateTriggeredSendDefinition(externalKey,
                                                                                emailId,
                                                                                dataExtensionExternalKey,
                                                                                deliveryProfileExternalKey,
                                                                                externalKey,
                                                                                externalKey,
                                                                                priority.ToString()));
        }
예제 #2
0
        private int Create(string externalKey, string layoutHtml)
        {
            var isTemplated = !string.IsNullOrWhiteSpace(layoutHtml);

            if (externalKey.Length > Guid.Empty.ToString().Length)
            {
                throw new ArgumentException("externalKey too long, should be max length of " + Guid.Empty.ToString().Length, "externalKey");
            }

            if (_triggeredSendDefinitionClient.DoesTriggeredSendDefinitionExist(externalKey))
            {
                throw new Exception(string.Format("A TriggeredSendDefinition with external key {0} already exsits", externalKey));
            }

            var dataExtensionExternalKey = ExternalKeyGenerator.GenerateExternalKey("data-extension-" + externalKey);

            if (!_dataExtensionClient.DoesDataExtensionExist(dataExtensionExternalKey))
            {
                var dataExtensionTemplateObjectId = _dataExtensionClient.RetrieveTriggeredSendDataExtensionTemplateObjectId();
                var dataExtensionFieldNames       = new HashSet <string> {
                    "Subject", "Body"
                };
                if (!isTemplated)
                {
                    dataExtensionFieldNames.Add("Head");
                }

                _dataExtensionClient.CreateDataExtension(dataExtensionTemplateObjectId,
                                                         dataExtensionExternalKey,
                                                         "triggeredsend-" + externalKey,
                                                         dataExtensionFieldNames);
            }


            int emailId;
            var emailName        = "email-" + externalKey;
            var emailExternalKey = ExternalKeyGenerator.GenerateExternalKey("email-" + externalKey);

            if (isTemplated)
            {
                var emailTempalteExternalKey = ExternalKeyGenerator.GenerateExternalKey("email-template" + externalKey);
                var emailTemplateId          = _emailTemplateClient.RetrieveEmailTemplateId(emailTempalteExternalKey);
                if (emailTemplateId == 0)
                {
                    emailTemplateId = _emailTemplateClient.CreateEmailTemplate(emailTempalteExternalKey,
                                                                               "template-" + externalKey,
                                                                               layoutHtml);
                }

                emailId = _emailRequestClient.CreateEmailFromTemplate(emailTemplateId,
                                                                      emailName,
                                                                      "%%Subject%%",
                                                                      new KeyValuePair <string, string>("dynamicArea", "%%Body%%"));
            }
            else
            {
                emailId = _emailRequestClient.CreateEmail(emailExternalKey, emailName, "%%Subject%%",
                                                          "<html>" +
                                                          "<head>%%Head%%</head>" +
                                                          "%%Body%%" +
                                                          EmailContentHelper.GetOpenTrackingTag() +
                                                          EmailContentHelper.GetCompanyPhysicalMailingAddressTags() +
                                                          "</html>");
            }


            var deliveryProfileExternalKey = ExternalKeyGenerator.GenerateExternalKey("blank-delivery-profile");

            _deliveryProfileClient.TryCreateBlankDeliveryProfile(deliveryProfileExternalKey);

            return(_triggeredSendDefinitionClient.CreateTriggeredSendDefinition(externalKey,
                                                                                emailId,
                                                                                dataExtensionExternalKey,
                                                                                deliveryProfileExternalKey,
                                                                                externalKey,
                                                                                externalKey));
        }