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 async Task <int> SendCampaignAsync(Campaign campaign, EmailAccount emailAccount,
                                                          IEnumerable <NewsLetterSubscription> subscriptions)
        {
            if (campaign == null)
            {
                throw new ArgumentNullException(nameof(campaign));
            }

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

            var totalEmailsSent = 0;

            foreach (var subscription in subscriptions)
            {
                var customer = await _customerService.GetCustomerByEmailAsync(subscription.Email);

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

                var tokens = new List <Token>();
                await _messageTokenProvider.AddStoreTokensAsync(tokens, await _storeContext.GetCurrentStoreAsync(), emailAccount);

                await _messageTokenProvider.AddNewsLetterSubscriptionTokensAsync(tokens, subscription);

                if (customer != null)
                {
                    await _messageTokenProvider.AddCustomerTokensAsync(tokens, customer);
                }

                var subject = _tokenizer.Replace(campaign.Subject, tokens, false);
                var 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,
                    DontSendBeforeDateUtc = campaign.DontSendBeforeDateUtc
                };
                await _queuedEmailService.InsertQueuedEmailAsync(email);

                totalEmailsSent++;
            }

            return(totalEmailsSent);
        }