Ejemplo n.º 1
0
        /// <summary>
        /// Sends a campaign to specified email
        /// </summary>
        /// <param name="campaign">Campaign</param>
        /// <param name="emailAccount">Email account</param>
        /// <param name="email">Email</param>
        public virtual async Task SendCampaign(Campaign campaign, EmailAccount emailAccount, string email)
        {
            if (campaign == null)
            {
                throw new ArgumentNullException(nameof(campaign));
            }

            if (emailAccount == null)
            {
                throw new ArgumentNullException(nameof(emailAccount));
            }

            var language = await _languageService.GetLanguageById(campaign.LanguageId);

            if (language == null)
            {
                language = (await _languageService.GetAllLanguages()).FirstOrDefault();
            }

            var store = await _storeService.GetStoreById(campaign.StoreId);

            if (store == null)
            {
                store = (await _storeService.GetAllStores()).FirstOrDefault();
            }

            var builder = new LiquidObjectBuilder(_mediator);

            builder.AddStoreTokens(store, language, emailAccount);
            var customer = await _customerService.GetCustomerByEmail(email);

            if (customer != null)
            {
                builder.AddCustomerTokens(customer, store, language)
                .AddShoppingCartTokens(customer, store, language);
            }

            var liquidObject = await builder.BuildAsync();

            var body    = LiquidExtensions.Render(liquidObject, campaign.Body);
            var subject = LiquidExtensions.Render(liquidObject, campaign.Subject);

            await _emailSender.SendEmail(emailAccount, subject, body, emailAccount.Email, emailAccount.DisplayName, email, null);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Sends a campaign to specified emails
        /// </summary>
        /// <param name="campaign">Campaign</param>
        /// <param name="emailAccount">Email account</param>
        /// <param name="subscriptions">Subscriptions</param>
        /// <returns>Total emails sent</returns>
        public virtual async Task <int> SendCampaign(Campaign campaign, EmailAccount emailAccount,
                                                     IEnumerable <NewsLetterSubscription> subscriptions)
        {
            if (campaign == null)
            {
                throw new ArgumentNullException(nameof(campaign));
            }

            if (emailAccount == null)
            {
                throw new ArgumentNullException(nameof(emailAccount));
            }

            int totalEmailsSent = 0;
            var language        = await _languageService.GetLanguageById(campaign.LanguageId);

            if (language == null)
            {
                language = (await _languageService.GetAllLanguages()).FirstOrDefault();
            }

            foreach (var subscription in subscriptions)
            {
                Customer customer = null;

                if (!String.IsNullOrEmpty(subscription.CustomerId))
                {
                    customer = await _customerService.GetCustomerById(subscription.CustomerId);
                }

                if (customer == null)
                {
                    customer = await _customerService.GetCustomerByEmail(subscription.Email);
                }

                //ignore deleted or inactive customers when sending newsletter campaigns
                if (customer != null && (!customer.Active || customer.Deleted))
                {
                    continue;
                }

                var builder = new LiquidObjectBuilder(_mediator);
                var store   = await _storeService.GetStoreById(campaign.StoreId);

                if (store == null)
                {
                    store = (await _storeService.GetAllStores()).FirstOrDefault();
                }

                builder.AddStoreTokens(store, language, emailAccount)
                .AddNewsLetterSubscriptionTokens(subscription, store);
                if (customer != null)
                {
                    builder.AddCustomerTokens(customer, store, language)
                    .AddShoppingCartTokens(customer, store, language);
                }

                var liquidObject = await builder.BuildAsync();

                var body    = LiquidExtensions.Render(liquidObject, campaign.Body);
                var subject = LiquidExtensions.Render(liquidObject, campaign.Subject);

                var email = new QueuedEmail
                {
                    PriorityId     = QueuedEmailPriority.Low,
                    From           = emailAccount.Email,
                    FromName       = emailAccount.DisplayName,
                    To             = subscription.Email,
                    Subject        = subject,
                    Body           = body,
                    CreatedOnUtc   = DateTime.UtcNow,
                    EmailAccountId = emailAccount.Id
                };
                await _queuedEmailService.InsertQueuedEmail(email);
                await InsertCampaignHistory(new CampaignHistory()
                {
                    CampaignId = campaign.Id, CustomerId = subscription.CustomerId, Email = subscription.Email, CreatedDateUtc = DateTime.UtcNow, StoreId = campaign.StoreId
                });

                //activity log
                if (customer != null)
                {
                    await _customerActivityService.InsertActivity("CustomerReminder.SendCampaign", campaign.Id, _translationService.GetResource("ActivityLog.SendCampaign"), customer, campaign.Name);
                }
                else
                {
                    await _customerActivityService.InsertActivity("CustomerReminder.SendCampaign", campaign.Id, _translationService.GetResource("ActivityLog.SendCampaign"), campaign.Name + " - " + subscription.Email);
                }

                totalEmailsSent++;
            }
            return(totalEmailsSent);
        }