/// <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); }
/// <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("campaign"); } if (emailAccount == null) { throw new ArgumentNullException("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 liquidObject = new LiquidObject(); var store = await _storeService.GetStoreById(campaign.StoreId); if (store == null) { store = (await _storeService.GetAllStores()).FirstOrDefault(); } await _messageTokenProvider.AddStoreTokens(liquidObject, store, language, emailAccount); await _messageTokenProvider.AddNewsLetterSubscriptionTokens(liquidObject, subscription, store); if (customer != null) { await _messageTokenProvider.AddCustomerTokens(liquidObject, customer, store, language); await _messageTokenProvider.AddShoppingCartTokens(liquidObject, customer, store, language); } var body = LiquidExtensions.Render(liquidObject, campaign.Body); var subject = LiquidExtensions.Render(liquidObject, campaign.Subject); 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 }; 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, _localizationService.GetResource("ActivityLog.SendCampaign"), customer, campaign.Name); } else { await _customerActivityService.InsertActivity("CustomerReminder.SendCampaign", campaign.Id, _localizationService.GetResource("ActivityLog.SendCampaign"), campaign.Name + " - " + subscription.Email); } totalEmailsSent++; } return(totalEmailsSent); }