Skip to content

Commit

Permalink
#2005 Moved "contact us" emails to message templates. Now store owner…
Browse files Browse the repository at this point in the history
…s can choose a default email account (used to send these emails) and configure BCC.
  • Loading branch information
AndreiMaz committed Feb 9, 2017
1 parent 363a74b commit 47dab56
Show file tree
Hide file tree
Showing 10 changed files with 267 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,15 @@ public static partial class MessageTemplateSystemNames
/// </summary>
public const string BackInStockNotification = "Customer.BackInStock";

/// <summary>
/// Represents system name of 'Contact us' message
/// </summary>
public const string ContactUsMessage = "Service.ContactUs";
/// <summary>
/// Represents system name of 'Contact vendor' message
/// </summary>
public const string ContactVendorMessage = "Service.ContactVendor";

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5649,6 +5649,22 @@ protected virtual void InstallMessageTemplates()
Body = string.Format("<p>{0}<a href=\"%Store.URL%\">%Store.Name%</a>{0}<br />{0}<br />{0}Vendor %Vendor.Name% (%Vendor.Email%) has just changed information about itself.{0}</p>{0}", Environment.NewLine),
IsActive = true,
EmailAccountId = eaGeneral.Id
},
new MessageTemplate
{
Name = MessageTemplateSystemNames.ContactUsMessage,
Subject = "%Store.Name%. Contact us",
Body = string.Format("<p>{0}%ContactUs.Body%{0}</p>{0}", Environment.NewLine),
IsActive = true,
EmailAccountId = eaGeneral.Id,
},
new MessageTemplate
{
Name = MessageTemplateSystemNames.ContactVendorMessage,
Subject = "%Store.Name%. Contact us",
Body = string.Format("<p>{0}%ContactUs.Body%{0}</p>{0}", Environment.NewLine),
IsActive = true,
EmailAccountId = eaGeneral.Id,
}
};
_messageTemplateRepository.Insert(messageTemplates);
Expand Down
33 changes: 30 additions & 3 deletions src/Libraries/Nop.Services/Messages/IWorkflowMessageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,30 @@ public partial interface IWorkflowMessageService
/// <returns>Queued email identifier</returns>
int SendBackInStockNotification(BackInStockSubscription subscription, int languageId);

/// <summary>
/// Sends "contact us" message
/// </summary>
/// <param name="languageId">Message language identifier</param>
/// <param name="senderEmail">Sender email</param>
/// <param name="senderName">Sender name</param>
/// <param name="subject">Email subject. Pass null if you want a message template subject to be used.</param>
/// <param name="body">Email body</param>
/// <returns>Queued email identifier</returns>
int SendContactUsMessage(int languageId, string senderEmail, string senderName, string subject, string body);

/// <summary>
/// Sends "contact vendor" message
/// </summary>
/// <param name="vendor">Vendor</param>
/// <param name="languageId">Message language identifier</param>
/// <param name="senderEmail">Sender email</param>
/// <param name="senderName">Sender name</param>
/// <param name="subject">Email subject. Pass null if you want a message template subject to be used.</param>
/// <param name="body">Email body</param>
/// <returns>Queued email identifier</returns>
int SendContactVendorMessage(Vendor vendor, int languageId, string senderEmail,
string senderName, string subject, string body);

/// <summary>
/// Sends a test email
/// </summary>
Expand All @@ -418,8 +442,7 @@ public partial interface IWorkflowMessageService
/// <param name="tokens">Tokens</param>
/// <param name="languageId">Message language identifier</param>
/// <returns>Queued email identifier</returns>
int SendTestEmail(int messageTemplateId, string sendToEmail,
List<Token> tokens, int languageId);
int SendTestEmail(int messageTemplateId, string sendToEmail, List<Token> tokens, int languageId);

/// <summary>
/// Send notification
Expand All @@ -434,12 +457,16 @@ public partial interface IWorkflowMessageService
/// <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>
int SendNotification(MessageTemplate messageTemplate,
EmailAccount emailAccount, int languageId, IEnumerable<Token> tokens,
string toEmailAddress, string toName,
string attachmentFilePath = null, string attachmentFileName = null,
string replyToEmailAddress = null, string replyToName = null);
string replyToEmailAddress = null, string replyToName = null,
string fromEmail = null, string fromName = null, string subject = null);

#endregion
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ public static IEnumerable<string> GetTokenGroups(this MessageTemplate messageTem
case MessageTemplateSystemNames.BackInStockNotification:
return new[] { TokenGroupNames.StoreTokens, TokenGroupNames.CustomerTokens, TokenGroupNames.ProductBackInStockTokens };

case MessageTemplateSystemNames.ContactUsMessage:
return new[] { TokenGroupNames.StoreTokens, TokenGroupNames.ContactUs };

case MessageTemplateSystemNames.ContactVendorMessage:
return new[] { TokenGroupNames.StoreTokens, TokenGroupNames.ContactVendor };

default:
return new string[] { };
}
Expand Down
16 changes: 16 additions & 0 deletions src/Libraries/Nop.Services/Messages/MessageTokenProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,22 @@ public partial class MessageTokenProvider : IMessageTokenProvider
"%VatValidationResult.Address%"
});

//contact us tokens
_allowedTokens.Add(TokenGroupNames.ContactUsValidation, new[]
{
"%ContactUs.SenderEmail%",
"%ContactUs.SenderName%",
"%ContactUs.Body%"
});

//contact vendor tokens
_allowedTokens.Add(TokenGroupNames.ContactVendorValidation, new[]
{
"%ContactUs.SenderEmail%",
"%ContactUs.SenderName%",
"%ContactUs.Body%"
});

return _allowedTokens;
}
}
Expand Down
12 changes: 11 additions & 1 deletion src/Libraries/Nop.Services/Messages/TokenGroupNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,16 @@ public static partial class TokenGroupNames
/// <summary>
/// Represents tokens with information about validation of the VAT request
/// </summary>
public const string VatValidation = "Vat validation tokens";
public const string VatValidation = "VAT validation tokens";

/// <summary>
/// Represents tokens with information about contact us
/// </summary>
public const string ContactUs = "Contact us tokens";

/// <summary>
/// Represents tokens with information about contact vendor
/// </summary>
public const string ContactVendor = "Contact vendor tokens";
}
}
146 changes: 141 additions & 5 deletions src/Libraries/Nop.Services/Messages/WorkflowMessageService.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Nop.Core;
using Nop.Core.Domain.Blogs;
using Nop.Core.Domain.Catalog;
using Nop.Core.Domain.Common;
using Nop.Core.Domain.Customers;
using Nop.Core.Domain.Forums;
using Nop.Core.Domain.Messages;
Expand Down Expand Up @@ -31,8 +33,10 @@ public partial class WorkflowMessageService : IWorkflowMessageService
private readonly IMessageTokenProvider _messageTokenProvider;
private readonly IStoreService _storeService;
private readonly IStoreContext _storeContext;
private readonly CommonSettings _commonSettings;
private readonly EmailAccountSettings _emailAccountSettings;
private readonly IEventPublisher _eventPublisher;
private readonly HttpContextBase _httpContext;

#endregion

Expand All @@ -46,8 +50,10 @@ public partial class WorkflowMessageService : IWorkflowMessageService
IMessageTokenProvider messageTokenProvider,
IStoreService storeService,
IStoreContext storeContext,
CommonSettings commonSettings,
EmailAccountSettings emailAccountSettings,
IEventPublisher eventPublisher)
IEventPublisher eventPublisher,
HttpContextBase httpContext)
{
this._messageTemplateService = messageTemplateService;
this._queuedEmailService = queuedEmailService;
Expand All @@ -57,8 +63,10 @@ public partial class WorkflowMessageService : IWorkflowMessageService
this._messageTokenProvider = messageTokenProvider;
this._storeService = storeService;
this._storeContext = storeContext;
this._commonSettings = commonSettings;
this._emailAccountSettings = emailAccountSettings;
this._eventPublisher = eventPublisher;
this._httpContext = httpContext;
}

#endregion
Expand Down Expand Up @@ -1692,6 +1700,128 @@ public virtual int SendBackInStockNotification(BackInStockSubscription subscript
return SendNotification(messageTemplate, emailAccount, languageId, tokens, toEmail, toName);
}

/// <summary>
/// Sends "contact us" message
/// </summary>
/// <param name="languageId">Message language identifier</param>
/// <param name="senderEmail">Sender email</param>
/// <param name="senderName">Sender name</param>
/// <param name="subject">Email subject. Pass null if you want a message template subject to be used.</param>
/// <param name="body">Email body</param>
/// <returns>Queued email identifier</returns>
public virtual int SendContactUsMessage(int languageId, string senderEmail,
string senderName, string subject, string body)
{
var store = _storeContext.CurrentStore;
languageId = EnsureLanguageIsActive(languageId, store.Id);

var messageTemplate = GetActiveMessageTemplate(MessageTemplateSystemNames.ContactUsMessage, store.Id);
if (messageTemplate == null)
return 0;

//email account
var emailAccount = GetEmailAccountOfMessageTemplate(messageTemplate, languageId);

string fromEmail;
string fromName;
//required for some SMTP servers
if (_commonSettings.UseSystemEmailForContactUsForm)
{
fromEmail = emailAccount.Email;
fromName = emailAccount.DisplayName;
body = string.Format("<strong>From</strong>: {0} - {1}<br /><br />{2}",
_httpContext.Server.HtmlEncode(senderName), _httpContext.Server.HtmlEncode(senderEmail), body);
}
else
{
fromEmail = senderEmail;
fromName = senderName;
}

//tokens
var tokens = new List<Token>();
_messageTokenProvider.AddStoreTokens(tokens, store, emailAccount);
tokens.Add(new Token("ContactUs.SenderEmail", senderEmail));
tokens.Add(new Token("ContactUs.SenderName", senderName));
tokens.Add(new Token("ContactUs.Body", body, true));

//event notification
_eventPublisher.MessageTokensAdded(messageTemplate, tokens);

var toEmail = emailAccount.Email;
var toName = emailAccount.DisplayName;

return SendNotification(messageTemplate, emailAccount, languageId, tokens, toEmail, toName,
fromEmail: fromEmail,
fromName: fromName,
subject: subject,
replyToEmailAddress: senderEmail,
replyToName: senderName);
}

/// <summary>
/// Sends "contact vendor" message
/// </summary>
/// <param name="vendor">Vendor</param>
/// <param name="languageId">Message language identifier</param>
/// <param name="senderEmail">Sender email</param>
/// <param name="senderName">Sender name</param>
/// <param name="subject">Email subject. Pass null if you want a message template subject to be used.</param>
/// <param name="body">Email body</param>
/// <returns>Queued email identifier</returns>
public virtual int SendContactVendorMessage(Vendor vendor, int languageId, string senderEmail,
string senderName, string subject, string body)
{
if (vendor == null)
throw new ArgumentNullException("vendor");

var store = _storeContext.CurrentStore;
languageId = EnsureLanguageIsActive(languageId, store.Id);

var messageTemplate = GetActiveMessageTemplate(MessageTemplateSystemNames.ContactVendorMessage, store.Id);
if (messageTemplate == null)
return 0;

//email account
var emailAccount = GetEmailAccountOfMessageTemplate(messageTemplate, languageId);

string fromEmail;
string fromName;
//required for some SMTP servers
if (_commonSettings.UseSystemEmailForContactUsForm)
{
fromEmail = emailAccount.Email;
fromName = emailAccount.DisplayName;
body = string.Format("<strong>From</strong>: {0} - {1}<br /><br />{2}",
_httpContext.Server.HtmlEncode(senderName), _httpContext.Server.HtmlEncode(senderEmail), body);
}
else
{
fromEmail = senderEmail;
fromName = senderName;
}

//tokens
var tokens = new List<Token>();
_messageTokenProvider.AddStoreTokens(tokens, store, emailAccount);
tokens.Add(new Token("ContactUs.SenderEmail", senderEmail));
tokens.Add(new Token("ContactUs.SenderName", senderName));
tokens.Add(new Token("ContactUs.Body", body, true));

//event notification
_eventPublisher.MessageTokensAdded(messageTemplate, tokens);

var toEmail = vendor.Email;
var toName = vendor.Name;

return SendNotification(messageTemplate, emailAccount, languageId, tokens, toEmail, toName,
fromEmail: fromEmail,
fromName: fromName,
subject: subject,
replyToEmailAddress: senderEmail,
replyToName: senderName);
}

/// <summary>
/// Sends a test email
/// </summary>
Expand Down Expand Up @@ -1728,21 +1858,27 @@ public virtual int SendTestEmail(int messageTemplateId, string sendToEmail, List
/// <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>
public virtual int SendNotification(MessageTemplate messageTemplate,
EmailAccount emailAccount, int languageId, IEnumerable<Token> tokens,
string toEmailAddress, string toName,
string attachmentFilePath = null, string attachmentFileName = null,
string replyToEmailAddress = null, string replyToName = null)
string replyToEmailAddress = null, string replyToName = null,
string fromEmail = null, string fromName = null, string subject = null)
{
if (messageTemplate == null)
throw new ArgumentNullException("messageTemplate");

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

//retrieve localized message template data
var bcc = messageTemplate.GetLocalized(mt => mt.BccEmailAddresses, languageId);
var subject = messageTemplate.GetLocalized(mt => mt.Subject, languageId);
if (String.IsNullOrEmpty(subject))
subject = messageTemplate.GetLocalized(mt => mt.Subject, languageId);
var body = messageTemplate.GetLocalized(mt => mt.Body, languageId);

//Replace subject and body tokens
Expand All @@ -1755,8 +1891,8 @@ public virtual int SendTestEmail(int messageTemplateId, string sendToEmail, List
var email = new QueuedEmail
{
Priority = QueuedEmailPriority.High,
From = emailAccount.Email,
FromName = emailAccount.DisplayName,
From = !string.IsNullOrEmpty(fromEmail) ? fromEmail : emailAccount.Email,
FromName = !string.IsNullOrEmpty(fromName) ? fromName : emailAccount.DisplayName,
To = toEmailAddress,
ToName = toName,
ReplyTo = replyToEmailAddress,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13212,9 +13212,6 @@
<LocaleResource Name="ContactUs.Email.Required">
<Value>Enter email</Value>
</LocaleResource>
<LocaleResource Name="ContactUs.EmailSubject">
<Value>{0}. Contact us</Value>
</LocaleResource>
<LocaleResource Name="ContactUs.Enquiry">
<Value>Enquiry</Value>
</LocaleResource>
Expand Down Expand Up @@ -13260,9 +13257,6 @@
<LocaleResource Name="ContactVendor.Email.Required">
<Value>Enter email</Value>
</LocaleResource>
<LocaleResource Name="ContactVendor.EmailSubject">
<Value>{0}. Contact us</Value>
</LocaleResource>
<LocaleResource Name="ContactVendor.Enquiry">
<Value>Enquiry</Value>
</LocaleResource>
Expand Down

0 comments on commit 47dab56

Please sign in to comment.