added ability to disable SMTP and set IsBodyHtml by default
This commit is contained in:
parent
6183d6a22e
commit
261adefbc7
|
@ -145,9 +145,9 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="row mb-1 align-items-center">
|
<div class="row mb-1 align-items-center">
|
||||||
<Label Class="col-sm-3" For="enabledSSl" HelpText="Specify if SSL is required for your SMTP server" ResourceKey="UseSsl">SSL Enabled: </Label>
|
<Label Class="col-sm-3" For="smtpssl" HelpText="Specify if SSL is required for your SMTP server" ResourceKey="UseSsl">SSL Enabled: </Label>
|
||||||
<div class="col-sm-9">
|
<div class="col-sm-9">
|
||||||
<select id="enabledSSl" class="form-select" @bind="@_smtpssl" >
|
<select id="smtpssl" class="form-select" @bind="@_smtpssl" >
|
||||||
<option value="True">@SharedLocalizer["Yes"]</option>
|
<option value="True">@SharedLocalizer["Yes"]</option>
|
||||||
<option value="False">@SharedLocalizer["No"]</option>
|
<option value="False">@SharedLocalizer["No"]</option>
|
||||||
</select>
|
</select>
|
||||||
|
@ -183,7 +183,16 @@
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="row mb-1 align-items-center">
|
<div class="row mb-1 align-items-center">
|
||||||
|
<Label Class="col-sm-3" For="smtpenabled" HelpText="Specify if SMTP is enabled for this site" ResourceKey="SMTPEnabled">Enabled? </Label>
|
||||||
|
<div class="col-sm-9">
|
||||||
|
<select id="smtpenabled" class="form-select" @bind="@_smtpenabled">
|
||||||
|
<option value="True">@SharedLocalizer["Yes"]</option>
|
||||||
|
<option value="False">@SharedLocalizer["No"]</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="row mb-1 align-items-center">
|
||||||
<Label Class="col-sm-3" For="retention" HelpText="Number of days of notifications to retain" ResourceKey="Retention">Retention (Days): </Label>
|
<Label Class="col-sm-3" For="retention" HelpText="Number of days of notifications to retain" ResourceKey="Retention">Retention (Days): </Label>
|
||||||
<div class="col-sm-9">
|
<div class="col-sm-9">
|
||||||
<input id="retention" class="form-control" @bind="@_retention" />
|
<input id="retention" class="form-control" @bind="@_retention" />
|
||||||
|
@ -354,6 +363,7 @@
|
||||||
private string _togglesmtppassword = string.Empty;
|
private string _togglesmtppassword = string.Empty;
|
||||||
private string _smtpsender = string.Empty;
|
private string _smtpsender = string.Empty;
|
||||||
private string _smtprelay = "False";
|
private string _smtprelay = "False";
|
||||||
|
private string _smtpenabled = "True";
|
||||||
private string _retention = string.Empty;
|
private string _retention = string.Empty;
|
||||||
private string _pwaisenabled;
|
private string _pwaisenabled;
|
||||||
private int _pwaappiconfileid = -1;
|
private int _pwaappiconfileid = -1;
|
||||||
|
@ -434,6 +444,7 @@
|
||||||
_togglesmtppassword = SharedLocalizer["ShowPassword"];
|
_togglesmtppassword = SharedLocalizer["ShowPassword"];
|
||||||
_smtpsender = SettingService.GetSetting(settings, "SMTPSender", string.Empty);
|
_smtpsender = SettingService.GetSetting(settings, "SMTPSender", string.Empty);
|
||||||
_smtprelay = SettingService.GetSetting(settings, "SMTPRelay", "False");
|
_smtprelay = SettingService.GetSetting(settings, "SMTPRelay", "False");
|
||||||
|
_smtpenabled = SettingService.GetSetting(settings, "SMTPEnabled", "True");
|
||||||
_retention = SettingService.GetSetting(settings, "NotificationRetention", "30");
|
_retention = SettingService.GetSetting(settings, "NotificationRetention", "30");
|
||||||
|
|
||||||
// aliases
|
// aliases
|
||||||
|
@ -600,7 +611,8 @@
|
||||||
settings = SettingService.SetSetting(settings, "SMTPPassword", _smtppassword, true);
|
settings = SettingService.SetSetting(settings, "SMTPPassword", _smtppassword, true);
|
||||||
settings = SettingService.SetSetting(settings, "SMTPSender", _smtpsender, true);
|
settings = SettingService.SetSetting(settings, "SMTPSender", _smtpsender, true);
|
||||||
settings = SettingService.SetSetting(settings, "SMTPRelay", _smtprelay, true);
|
settings = SettingService.SetSetting(settings, "SMTPRelay", _smtprelay, true);
|
||||||
settings = SettingService.SetSetting(settings, "NotificationRetention", _retention, true);
|
settings = SettingService.SetSetting(settings, "SMTPEnabled", _smtpenabled, true);
|
||||||
|
settings = SettingService.SetSetting(settings, "NotificationRetention", _retention, true);
|
||||||
await SettingService.UpdateSiteSettingsAsync(settings, site.SiteId);
|
await SettingService.UpdateSiteSettingsAsync(settings, site.SiteId);
|
||||||
|
|
||||||
await logger.LogInformation("Site Settings Saved {Site}", site);
|
await logger.LogInformation("Site Settings Saved {Site}", site);
|
||||||
|
|
|
@ -369,4 +369,10 @@
|
||||||
<data name="PageContent.Heading" xml:space="preserve">
|
<data name="PageContent.Heading" xml:space="preserve">
|
||||||
<value>Page Content</value>
|
<value>Page Content</value>
|
||||||
</data>
|
</data>
|
||||||
|
<data name="SMTPEnabled.HelpText" xml:space="preserve">
|
||||||
|
<value>Specify if SMTP is enabled for this site</value>
|
||||||
|
</data>
|
||||||
|
<data name="SMTPEnabled.Text" xml:space="preserve">
|
||||||
|
<value>Enabled?</value>
|
||||||
|
</data>
|
||||||
</root>
|
</root>
|
|
@ -42,119 +42,127 @@ namespace Oqtane.Infrastructure
|
||||||
// get site settings
|
// get site settings
|
||||||
List<Setting> sitesettings = settingRepository.GetSettings(EntityNames.Site, site.SiteId).ToList();
|
List<Setting> sitesettings = settingRepository.GetSettings(EntityNames.Site, site.SiteId).ToList();
|
||||||
Dictionary<string, string> settings = GetSettings(sitesettings);
|
Dictionary<string, string> settings = GetSettings(sitesettings);
|
||||||
if (settings.ContainsKey("SMTPHost") && settings["SMTPHost"] != "" &&
|
if (!settings.ContainsKey("SMTPEnabled") || settings["SMTPEnabled"] == "True")
|
||||||
settings.ContainsKey("SMTPPort") && settings["SMTPPort"] != "" &&
|
|
||||||
settings.ContainsKey("SMTPSSL") && settings["SMTPSSL"] != "" &&
|
|
||||||
settings.ContainsKey("SMTPSender") && settings["SMTPSender"] != "")
|
|
||||||
{
|
{
|
||||||
// construct SMTP Client
|
if (settings.ContainsKey("SMTPHost") && settings["SMTPHost"] != "" &&
|
||||||
var client = new SmtpClient()
|
settings.ContainsKey("SMTPPort") && settings["SMTPPort"] != "" &&
|
||||||
|
settings.ContainsKey("SMTPSSL") && settings["SMTPSSL"] != "" &&
|
||||||
|
settings.ContainsKey("SMTPSender") && settings["SMTPSender"] != "")
|
||||||
{
|
{
|
||||||
DeliveryMethod = SmtpDeliveryMethod.Network,
|
// construct SMTP Client
|
||||||
UseDefaultCredentials = false,
|
var client = new SmtpClient()
|
||||||
Host = settings["SMTPHost"],
|
|
||||||
Port = int.Parse(settings["SMTPPort"]),
|
|
||||||
EnableSsl = bool.Parse(settings["SMTPSSL"])
|
|
||||||
};
|
|
||||||
if (settings["SMTPUsername"] != "" && settings["SMTPPassword"] != "")
|
|
||||||
{
|
|
||||||
client.Credentials = new NetworkCredential(settings["SMTPUsername"], settings["SMTPPassword"]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// iterate through undelivered notifications
|
|
||||||
int sent = 0;
|
|
||||||
List<Notification> notifications = notificationRepository.GetNotifications(site.SiteId, -1, -1).ToList();
|
|
||||||
foreach (Notification notification in notifications)
|
|
||||||
{
|
|
||||||
// get sender and receiver information from user object if not provided
|
|
||||||
if ((string.IsNullOrEmpty(notification.FromEmail) || string.IsNullOrEmpty(notification.FromDisplayName)) && notification.FromUserId != null)
|
|
||||||
{
|
{
|
||||||
var user = userRepository.GetUser(notification.FromUserId.Value);
|
DeliveryMethod = SmtpDeliveryMethod.Network,
|
||||||
if (user != null)
|
UseDefaultCredentials = false,
|
||||||
{
|
Host = settings["SMTPHost"],
|
||||||
notification.FromEmail = (string.IsNullOrEmpty(notification.FromEmail)) ? user.Email : notification.FromEmail;
|
Port = int.Parse(settings["SMTPPort"]),
|
||||||
notification.FromDisplayName = (string.IsNullOrEmpty(notification.FromDisplayName)) ? user.DisplayName : notification.FromDisplayName;
|
EnableSsl = bool.Parse(settings["SMTPSSL"])
|
||||||
}
|
};
|
||||||
}
|
if (settings["SMTPUsername"] != "" && settings["SMTPPassword"] != "")
|
||||||
if ((string.IsNullOrEmpty(notification.ToEmail) || string.IsNullOrEmpty(notification.ToDisplayName)) && notification.ToUserId != null)
|
|
||||||
{
|
{
|
||||||
var user = userRepository.GetUser(notification.ToUserId.Value);
|
client.Credentials = new NetworkCredential(settings["SMTPUsername"], settings["SMTPPassword"]);
|
||||||
if (user != null)
|
|
||||||
{
|
|
||||||
notification.ToEmail = (string.IsNullOrEmpty(notification.ToEmail)) ? user.Email : notification.ToEmail;
|
|
||||||
notification.ToDisplayName = (string.IsNullOrEmpty(notification.ToDisplayName)) ? user.DisplayName : notification.ToDisplayName;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// validate recipient
|
// iterate through undelivered notifications
|
||||||
if (string.IsNullOrEmpty(notification.ToEmail))
|
int sent = 0;
|
||||||
|
List<Notification> notifications = notificationRepository.GetNotifications(site.SiteId, -1, -1).ToList();
|
||||||
|
foreach (Notification notification in notifications)
|
||||||
{
|
{
|
||||||
log += "Recipient Missing For NotificationId: " + notification.NotificationId + "<br />";
|
// get sender and receiver information from user object if not provided
|
||||||
notification.IsDeleted = true;
|
if ((string.IsNullOrEmpty(notification.FromEmail) || string.IsNullOrEmpty(notification.FromDisplayName)) && notification.FromUserId != null)
|
||||||
notificationRepository.UpdateNotification(notification);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
MailMessage mailMessage = new MailMessage();
|
|
||||||
|
|
||||||
// sender
|
|
||||||
if (settings.ContainsKey("SMTPRelay") && settings["SMTPRelay"] == "True" && !string.IsNullOrEmpty(notification.FromEmail))
|
|
||||||
{
|
{
|
||||||
if (!string.IsNullOrEmpty(notification.FromDisplayName))
|
var user = userRepository.GetUser(notification.FromUserId.Value);
|
||||||
|
if (user != null)
|
||||||
{
|
{
|
||||||
mailMessage.From = new MailAddress(notification.FromEmail, notification.FromDisplayName);
|
notification.FromEmail = (string.IsNullOrEmpty(notification.FromEmail)) ? user.Email : notification.FromEmail;
|
||||||
|
notification.FromDisplayName = (string.IsNullOrEmpty(notification.FromDisplayName)) ? user.DisplayName : notification.FromDisplayName;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ((string.IsNullOrEmpty(notification.ToEmail) || string.IsNullOrEmpty(notification.ToDisplayName)) && notification.ToUserId != null)
|
||||||
|
{
|
||||||
|
var user = userRepository.GetUser(notification.ToUserId.Value);
|
||||||
|
if (user != null)
|
||||||
|
{
|
||||||
|
notification.ToEmail = (string.IsNullOrEmpty(notification.ToEmail)) ? user.Email : notification.ToEmail;
|
||||||
|
notification.ToDisplayName = (string.IsNullOrEmpty(notification.ToDisplayName)) ? user.DisplayName : notification.ToDisplayName;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// validate recipient
|
||||||
|
if (string.IsNullOrEmpty(notification.ToEmail))
|
||||||
|
{
|
||||||
|
log += "Recipient Missing For NotificationId: " + notification.NotificationId + "<br />";
|
||||||
|
notification.IsDeleted = true;
|
||||||
|
notificationRepository.UpdateNotification(notification);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
MailMessage mailMessage = new MailMessage();
|
||||||
|
|
||||||
|
// sender
|
||||||
|
if (settings.ContainsKey("SMTPRelay") && settings["SMTPRelay"] == "True" && !string.IsNullOrEmpty(notification.FromEmail))
|
||||||
|
{
|
||||||
|
if (!string.IsNullOrEmpty(notification.FromDisplayName))
|
||||||
|
{
|
||||||
|
mailMessage.From = new MailAddress(notification.FromEmail, notification.FromDisplayName);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
mailMessage.From = new MailAddress(notification.FromEmail);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
mailMessage.From = new MailAddress(notification.FromEmail);
|
mailMessage.From = new MailAddress(settings["SMTPSender"], (!string.IsNullOrEmpty(notification.FromDisplayName)) ? notification.FromDisplayName : site.Name);
|
||||||
|
}
|
||||||
|
|
||||||
|
// recipient
|
||||||
|
if (!string.IsNullOrEmpty(notification.ToDisplayName))
|
||||||
|
{
|
||||||
|
mailMessage.To.Add(new MailAddress(notification.ToEmail, notification.ToDisplayName));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
mailMessage.To.Add(new MailAddress(notification.ToEmail));
|
||||||
|
}
|
||||||
|
|
||||||
|
// subject
|
||||||
|
mailMessage.Subject = notification.Subject;
|
||||||
|
|
||||||
|
//body
|
||||||
|
mailMessage.Body = notification.Body;
|
||||||
|
|
||||||
|
// encoding
|
||||||
|
mailMessage.SubjectEncoding = System.Text.Encoding.UTF8;
|
||||||
|
mailMessage.BodyEncoding = System.Text.Encoding.UTF8;
|
||||||
|
mailMessage.IsBodyHtml = true;
|
||||||
|
|
||||||
|
// send mail
|
||||||
|
try
|
||||||
|
{
|
||||||
|
client.Send(mailMessage);
|
||||||
|
sent++;
|
||||||
|
notification.IsDelivered = true;
|
||||||
|
notification.DeliveredOn = DateTime.UtcNow;
|
||||||
|
notificationRepository.UpdateNotification(notification);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
// error
|
||||||
|
log += ex.Message + "<br />";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
mailMessage.From = new MailAddress(settings["SMTPSender"], (!string.IsNullOrEmpty(notification.FromDisplayName)) ? notification.FromDisplayName : site.Name);
|
|
||||||
}
|
|
||||||
|
|
||||||
// recipient
|
|
||||||
if (!string.IsNullOrEmpty(notification.ToDisplayName))
|
|
||||||
{
|
|
||||||
mailMessage.To.Add(new MailAddress(notification.ToEmail, notification.ToDisplayName));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
mailMessage.To.Add(new MailAddress(notification.ToEmail));
|
|
||||||
}
|
|
||||||
|
|
||||||
// subject
|
|
||||||
mailMessage.Subject = notification.Subject;
|
|
||||||
|
|
||||||
//body
|
|
||||||
mailMessage.Body = notification.Body;
|
|
||||||
|
|
||||||
// encoding
|
|
||||||
mailMessage.SubjectEncoding = System.Text.Encoding.UTF8;
|
|
||||||
mailMessage.BodyEncoding = System.Text.Encoding.UTF8;
|
|
||||||
|
|
||||||
// send mail
|
|
||||||
try
|
|
||||||
{
|
|
||||||
client.Send(mailMessage);
|
|
||||||
sent++;
|
|
||||||
notification.IsDelivered = true;
|
|
||||||
notification.DeliveredOn = DateTime.UtcNow;
|
|
||||||
notificationRepository.UpdateNotification(notification);
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
// error
|
|
||||||
log += ex.Message + "<br />";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
log += "Notifications Delivered: " + sent + "<br />";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
log += "SMTP Not Configured Properly In Site Settings - Host, Port, SSL, And Sender Are All Required" + "<br />";
|
||||||
}
|
}
|
||||||
log += "Notifications Delivered: " + sent + "<br />";
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
log += "SMTP Not Configured Properly In Site Settings - Host, Port, SSL, And Sender Are All Required" + "<br />";
|
log += "SMTP Disabled In Site Settings" + "<br />";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user