/// <summary> /// Send email notification by message template /// </summary> /// <param name="messageTemplate">Message template</param> /// <param name="emailAccount">Email account</param> /// <param name="tokens">Tokens</param> /// <param name="toEmailAddress">Recipient email address</param> /// <param name="toName">Recipient name</param> /// <param name="attachmentFilePath">Attachment file path</param> /// <param name="attachmentFileName">Attachment file name</param> /// <param name="replyToEmailAddress">"Reply to" email</param> /// <param name="replyToName">"Reply to" name</param> /// <param name="fromEmail">Sender email. If specified, then it overrides passed "emailAccount" details</param> /// <param name="fromName">Sender name. If specified, then it overrides passed "emailAccount" details</param> /// <param name="subject">Subject. If specified, then it overrides subject of a message template</param> /// <returns>Queued email identifier</returns> private async Task <int?> SendEmailNotificationAsync(MessageTemplate messageTemplate, EmailAccount emailAccount, IEnumerable <Token> tokens, string toEmailAddress, string toName, string attachmentFilePath = null, string attachmentFileName = null, string replyToEmailAddress = null, string replyToName = null, string fromEmail = null, string fromName = null, string subject = null) { //get plugin settings var storeId = (int?)tokens.FirstOrDefault(token => token.Key == "Store.Id")?.Value; var sendinBlueSettings = await _settingService.LoadSettingAsync <SendinBlueSettings>(storeId ?? 0); //ensure email notifications enabled if (!sendinBlueSettings.UseSmtp) { return(null); } //whether to send email by the passed message template var templateId = await _genericAttributeService.GetAttributeAsync <int?>(messageTemplate, SendinBlueDefaults.TemplateIdAttribute); var sendEmailForThisMessageTemplate = templateId.HasValue; if (!sendEmailForThisMessageTemplate) { return(null); } //get the specified email account from settings emailAccount = await _emailAccountService.GetEmailAccountByIdAsync(sendinBlueSettings.EmailAccountId) ?? emailAccount; //get an email from the template var email = await _sendinBlueEmailManager.GetQueuedEmailFromTemplateAsync(templateId.Value) ?? throw new NopException($"There is no template with id {templateId}"); //replace body and subject tokens if (string.IsNullOrEmpty(subject)) { subject = email.Subject; } if (!string.IsNullOrEmpty(subject)) { email.Subject = _tokenizer.Replace(subject, tokens, false); } if (!string.IsNullOrEmpty(email.Body)) { email.Body = _tokenizer.Replace(email.Body, tokens, true); } //set email parameters email.Priority = QueuedEmailPriority.High; email.From = !string.IsNullOrEmpty(fromEmail) ? fromEmail : email.From; email.FromName = !string.IsNullOrEmpty(fromName) ? fromName : email.FromName; email.To = toEmailAddress; email.ToName = CommonHelper.EnsureMaximumLength(toName, 300); email.ReplyTo = replyToEmailAddress; email.ReplyToName = replyToName; email.CC = string.Empty; email.Bcc = messageTemplate.BccEmailAddresses; email.AttachmentFilePath = attachmentFilePath; email.AttachmentFileName = attachmentFileName; email.AttachedDownloadId = messageTemplate.AttachedDownloadId; email.CreatedOnUtc = DateTime.UtcNow; email.EmailAccountId = emailAccount.Id; email.DontSendBeforeDateUtc = messageTemplate.DelayBeforeSend.HasValue ? (DateTime?)(DateTime.UtcNow + TimeSpan.FromHours(messageTemplate.DelayPeriod.ToHours(messageTemplate.DelayBeforeSend.Value))) : null; //queue email await _queuedEmailService.InsertQueuedEmailAsync(email); return(email.Id); }