2019-05-15 14:56:09 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* (c) Copyright Ascensio System Limited 2010-2018
|
|
|
|
*
|
|
|
|
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
|
|
|
|
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
|
|
|
|
* In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that
|
|
|
|
* Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights.
|
|
|
|
*
|
|
|
|
* THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
|
|
|
|
*
|
|
|
|
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
|
|
|
|
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
|
|
|
|
*
|
|
|
|
* Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
|
|
|
|
* relevant author attributions when distributing the software. If the display of the logo in its graphic
|
|
|
|
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
|
|
|
|
* in every copy of the program you distribute.
|
|
|
|
* Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2019-11-06 15:03:09 +00:00
|
|
|
using System.Linq;
|
2020-08-27 14:01:37 +00:00
|
|
|
|
2020-08-31 09:01:23 +00:00
|
|
|
using ASC.Common;
|
2019-05-15 14:56:09 +00:00
|
|
|
using ASC.Common.Logging;
|
|
|
|
using ASC.Common.Utils;
|
|
|
|
using ASC.Core.Notify.Senders;
|
|
|
|
using ASC.Core.Tenants;
|
|
|
|
using ASC.Notify.Messages;
|
2019-11-06 15:03:09 +00:00
|
|
|
using ASC.Notify.Sinks;
|
2020-08-27 14:01:37 +00:00
|
|
|
|
2019-11-06 15:03:09 +00:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
2019-05-15 14:56:09 +00:00
|
|
|
namespace ASC.Core.Notify
|
|
|
|
{
|
2020-11-10 08:42:50 +00:00
|
|
|
public class EmailSenderSink : Sink
|
2019-05-15 14:56:09 +00:00
|
|
|
{
|
|
|
|
private static readonly string senderName = ASC.Core.Configuration.Constants.NotifyEMailSenderSysName;
|
|
|
|
private readonly INotifySender sender;
|
|
|
|
|
|
|
|
|
2020-09-15 13:11:05 +00:00
|
|
|
public EmailSenderSink(INotifySender sender, IServiceProvider serviceProvider, IOptionsMonitor<ILog> options)
|
2019-11-06 15:03:09 +00:00
|
|
|
{
|
|
|
|
this.sender = sender ?? throw new ArgumentNullException("sender");
|
2020-09-15 13:11:05 +00:00
|
|
|
ServiceProvider = serviceProvider;
|
|
|
|
Log = options.Get("ASC.Notify");
|
2019-11-06 15:03:09 +00:00
|
|
|
}
|
|
|
|
|
2020-09-15 13:11:05 +00:00
|
|
|
private IServiceProvider ServiceProvider { get; }
|
|
|
|
private ILog Log { get; }
|
|
|
|
|
2019-05-15 14:56:09 +00:00
|
|
|
public override SendResponse ProcessMessage(INoticeMessage message)
|
|
|
|
{
|
|
|
|
if (message.Recipient.Addresses == null || message.Recipient.Addresses.Length == 0)
|
|
|
|
{
|
|
|
|
return new SendResponse(message, senderName, SendResult.IncorrectRecipient);
|
|
|
|
}
|
|
|
|
|
|
|
|
var responce = new SendResponse(message, senderName, default(SendResult));
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var m = CreateNotifyMessage(message);
|
|
|
|
var result = sender.Send(m);
|
|
|
|
|
2019-11-06 15:03:09 +00:00
|
|
|
responce.Result = result switch
|
|
|
|
{
|
2019-08-15 15:13:25 +00:00
|
|
|
NoticeSendResult.TryOnceAgain => SendResult.Inprogress,
|
|
|
|
NoticeSendResult.MessageIncorrect => SendResult.IncorrectRecipient,
|
|
|
|
NoticeSendResult.SendingImpossible => SendResult.Impossible,
|
2019-11-06 15:03:09 +00:00
|
|
|
_ => SendResult.OK,
|
|
|
|
};
|
2019-05-15 14:56:09 +00:00
|
|
|
return responce;
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
return new SendResponse(message, senderName, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private NotifyMessage CreateNotifyMessage(INoticeMessage message)
|
|
|
|
{
|
|
|
|
var m = new NotifyMessage
|
|
|
|
{
|
|
|
|
Subject = message.Subject.Trim(' ', '\t', '\n', '\r'),
|
|
|
|
ContentType = message.ContentType,
|
|
|
|
Content = message.Body,
|
|
|
|
Sender = senderName,
|
2019-08-01 08:47:15 +00:00
|
|
|
CreationDate = DateTime.UtcNow.Ticks,
|
2019-11-06 15:03:09 +00:00
|
|
|
};
|
|
|
|
|
2020-09-04 09:06:30 +00:00
|
|
|
using var scope = ServiceProvider.CreateScope();
|
|
|
|
|
2020-08-31 09:01:23 +00:00
|
|
|
var scopeClass = scope.ServiceProvider.GetService<EmailSenderSinkScope>();
|
2020-09-04 09:06:30 +00:00
|
|
|
var (tenantManager, configuration, options) = scopeClass;
|
2019-05-15 14:56:09 +00:00
|
|
|
|
2019-10-10 13:18:12 +00:00
|
|
|
var tenant = tenantManager.GetCurrentTenant(false);
|
2019-05-15 14:56:09 +00:00
|
|
|
m.Tenant = tenant == null ? Tenant.DEFAULT_TENANT : tenant.TenantId;
|
|
|
|
|
2020-09-04 09:06:30 +00:00
|
|
|
var from = MailAddressUtils.Create(configuration.SmtpSettings.SenderAddress, configuration.SmtpSettings.SenderDisplayName);
|
2019-05-15 14:56:09 +00:00
|
|
|
var fromTag = message.Arguments.FirstOrDefault(x => x.Tag.Equals("MessageFrom"));
|
2020-09-04 09:06:30 +00:00
|
|
|
if ((configuration.SmtpSettings.IsDefaultSettings || string.IsNullOrEmpty(configuration.SmtpSettings.SenderDisplayName)) &&
|
2019-05-15 14:56:09 +00:00
|
|
|
fromTag != null && fromTag.Value != null)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
from = MailAddressUtils.Create(from.Address, fromTag.Value.ToString());
|
|
|
|
}
|
|
|
|
catch { }
|
|
|
|
}
|
|
|
|
m.From = from.ToString();
|
|
|
|
|
|
|
|
var to = new List<string>();
|
|
|
|
foreach (var address in message.Recipient.Addresses)
|
|
|
|
{
|
|
|
|
to.Add(MailAddressUtils.Create(address, message.Recipient.Name).ToString());
|
|
|
|
}
|
|
|
|
m.To = string.Join("|", to.ToArray());
|
|
|
|
|
|
|
|
var replyTag = message.Arguments.FirstOrDefault(x => x.Tag == "replyto");
|
2020-09-28 15:21:29 +00:00
|
|
|
if (replyTag != null && replyTag.Value is string value)
|
2019-05-15 14:56:09 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2020-09-28 15:21:29 +00:00
|
|
|
m.ReplyTo = MailAddressUtils.Create(value).ToString();
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2019-11-06 15:03:09 +00:00
|
|
|
ServiceProvider.GetService<IOptionsMonitor<ILog>>().Get("ASC.Notify").Error("Error creating reply to tag for: " + replyTag.Value, e);
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var priority = message.Arguments.FirstOrDefault(a => a.Tag == "Priority");
|
|
|
|
if (priority != null)
|
|
|
|
{
|
|
|
|
m.Priority = Convert.ToInt32(priority.Value);
|
|
|
|
}
|
|
|
|
|
|
|
|
var attachmentTag = message.Arguments.FirstOrDefault(x => x.Tag == "EmbeddedAttachments");
|
|
|
|
if (attachmentTag != null && attachmentTag.Value != null)
|
|
|
|
{
|
2019-08-01 08:47:15 +00:00
|
|
|
m.EmbeddedAttachments.AddRange(attachmentTag.Value as NotifyMessageAttachment[]);
|
2020-09-15 13:11:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var autoSubmittedTag = message.Arguments.FirstOrDefault(x => x.Tag == "AutoSubmitted");
|
|
|
|
if (autoSubmittedTag != null && autoSubmittedTag.Value is string)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
m.AutoSubmitted = autoSubmittedTag.Value.ToString();
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Log.Error("Error creating AutoSubmitted tag for: " + autoSubmittedTag.Value, e);
|
|
|
|
}
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
}
|
2020-10-28 20:02:03 +00:00
|
|
|
|
|
|
|
[Scope]
|
2020-08-31 09:01:23 +00:00
|
|
|
public class EmailSenderSinkScope
|
|
|
|
{
|
|
|
|
private TenantManager TenantManager { get; }
|
|
|
|
private CoreConfiguration CoreConfiguration { get; }
|
|
|
|
private IOptionsMonitor<ILog> Options { get; }
|
|
|
|
|
|
|
|
public EmailSenderSinkScope(TenantManager tenantManager, CoreConfiguration coreConfiguration, IOptionsMonitor<ILog> options)
|
|
|
|
{
|
|
|
|
TenantManager = tenantManager;
|
|
|
|
CoreConfiguration = coreConfiguration;
|
|
|
|
Options = options;
|
|
|
|
}
|
|
|
|
|
2020-09-04 09:06:30 +00:00
|
|
|
public void Deconstruct(out TenantManager tenantManager, out CoreConfiguration coreConfiguration, out IOptionsMonitor<ILog> optionsMonitor)
|
2020-09-30 14:47:42 +00:00
|
|
|
{
|
|
|
|
(tenantManager, coreConfiguration, optionsMonitor) = (TenantManager, CoreConfiguration, Options);
|
|
|
|
}
|
2020-08-31 09:01:23 +00:00
|
|
|
}
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|