Esempio 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, int languageId, List <int> Categories, DateTime startDate, DateTime?endDate, int itemCount)
        {
            if (campaign == null)
            {
                throw new ArgumentNullException("campaign");
            }

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

            int  totalEmailsSent    = 0;
            bool hasProductsToken   = campaign.Body.IndexOf("%Store.RecentProducts%") > 0;
            bool hasBuyingRequests  = campaign.Body.IndexOf("%Store.RecentProductBuyingRequests%") > 0;
            var  productDates       = new NewsletterDates();
            var  buyingRequestDates = new NewsletterDates();
            var  tokens             = new List <Token>();

            if (hasProductsToken)
            {
                productDates = _newsletterDatesService.GetAllNewsletterDates().Where(x => x.LanguageId == languageId && x.IsProduct).FirstOrDefault();
                if (endDate == null)
                {
                    startDate = productDates.LastSubmit;
                }
                _messageTokenProvider.AddRecentProductsToken(tokens, languageId, Categories, startDate, endDate, itemCount);
            }
            if (hasBuyingRequests)
            {
                buyingRequestDates = _newsletterDatesService.GetAllNewsletterDates().Where(x => x.LanguageId == languageId && !x.IsProduct).FirstOrDefault();
                if (endDate == null)
                {
                    startDate = buyingRequestDates.LastSubmit;
                }
                _messageTokenProvider.AddRecentBuyingRequestsToken(tokens, languageId, Categories, startDate, endDate, itemCount);
            }

            foreach (var subscription in subscriptions.Where(x => x.LanguageId == languageId))
            {
                var tokensTemp = new List <Token>();
                tokensTemp.AddRange(tokens);
                _messageTokenProvider.AddStoreTokens(tokensTemp);
                _messageTokenProvider.AddNewsLetterSubscriptionTokens(tokensTemp, subscription);

                var customer = _customerService.GetCustomerByEmail(subscription.Email);
                if (customer != null)
                {
                    _messageTokenProvider.AddCustomerTokens(tokensTemp, customer);
                }

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

                var email = new QueuedEmail()
                {
                    Priority       = 3,
                    From           = emailAccount.Email,
                    FromName       = emailAccount.DisplayName,
                    To             = subscription.Email,
                    Subject        = subject,
                    Body           = body,
                    CreatedOnUtc   = DateTime.UtcNow,
                    EmailAccountId = emailAccount.Id
                };
                _queuedEmailService.InsertQueuedEmail(email);
                totalEmailsSent++;
            }
            if (hasProductsToken)
            {
                productDates.LastSubmit = DateTime.UtcNow;
                _newsletterDatesService.UpdateNewsletterDates(productDates);
            }
            if (hasBuyingRequests)
            {
                buyingRequestDates.LastSubmit = DateTime.UtcNow;
                _newsletterDatesService.UpdateNewsletterDates(buyingRequestDates);
            }

            return(totalEmailsSent);
        }