Ejemplo n.º 1
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 int SendCampaign(Campaign campaign, EmailAccount emailAccount,
                                        IEnumerable <NewsLetterSubscription> subscriptions)
        {
            if (campaign == null)
            {
                throw new ArgumentNullException("campaign");
            }

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

            int totalEmailsSent = 0;

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

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

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

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

                var tokens = new List <Token>();
                _messageTokenProvider.AddStoreTokens(tokens, _storeContext.CurrentStore, emailAccount);
                _messageTokenProvider.AddNewsLetterSubscriptionTokens(tokens, subscription);
                if (customer != null)
                {
                    _messageTokenProvider.AddCustomerTokens(tokens, customer);
                    _messageTokenProvider.AddShoppingCartTokens(tokens, customer);
                    _messageTokenProvider.AddRecommendedProductsTokens(tokens, customer);
                    _messageTokenProvider.AddRecentlyViewedProductsTokens(tokens, customer);
                }

                string subject = _tokenizer.Replace(campaign.Subject, tokens, false);
                string body    = _tokenizer.Replace(campaign.Body, tokens, true);

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

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

                totalEmailsSent++;
            }
            return(totalEmailsSent);
        }