Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Azure Email Communication Services (Lombiq Technologies: OCORE-129) #14749

Closed
wants to merge 147 commits into from
Closed
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
147 commits
Select commit Hold shift + click to select a range
22f80fd
Support email through Azure Communicaton Services
hishamco Nov 17, 2023
3db73e8
Options -> Settings
hishamco Nov 23, 2023
99b596c
Add NullEmailService for testing purpose
hishamco Nov 23, 2023
ac8fbc3
Fix broken unit test
hishamco Nov 23, 2023
31a41fc
Add OC.Email.Azure module
hishamco Nov 23, 2023
f15812a
Split the email settings from SMTP email settings
hishamco Nov 23, 2023
05180af
Fix ConnectionString configuration message
hishamco Nov 23, 2023
40462d4
Enable OC.Email.Smtp feature on ComingSoon recipe
hishamco Nov 23, 2023
71d9f94
Get the default sender from EmailSettings
hishamco Nov 23, 2023
ace3bc0
Add ConfigureAzureEmailSettings() extension method
hishamco Nov 23, 2023
1acd3f3
OC.Email.Azure should depends on OC.Email
hishamco Nov 24, 2023
73d4230
Add docs
hishamco Nov 24, 2023
f625248
Merge branch 'main' into hishamco/email-azure
hishamco Nov 24, 2023
8fd62a7
Should inherits from EmailServiceBase
hishamco Nov 24, 2023
29c6c61
Add unit test
hishamco Nov 24, 2023
673acf1
Fix TEmailService for NullEmailService
hishamco Nov 24, 2023
da600dd
SmtpSettings -> SmtpEmailSettings
hishamco Nov 24, 2023
8cdc83d
Don't check the DefaultSender from within SmtpEmailSettings
hishamco Nov 24, 2023
c5b5fb2
Should get SmtpEmailService instance while testing SMTP settings
hishamco Nov 24, 2023
22c3990
mkdocs-material 9.4.12
agriffard Nov 24, 2023
7efda9b
Fix merge conflict
hishamco Nov 24, 2023
cc511c3
Merge branch 'main' into hishamco/email-azure
hishamco Nov 27, 2023
385666b
Use keyed service
hishamco Nov 27, 2023
c19911f
Add other email properties
hishamco Nov 27, 2023
80d8304
Refactoring
hishamco Dec 1, 2023
b536600
Register NullEmailService
hishamco Dec 1, 2023
83487c2
Split OC.Email.Smtp into another module
hishamco Dec 1, 2023
040e7df
Merge remote-tracking branch 'origin/main' into hishamco/email-azure
hishamco Dec 1, 2023
e34e8d0
Fix the build
hishamco Dec 1, 2023
253cf27
Attach supported mime types
hishamco Dec 1, 2023
aafb266
Use primary constrcutor
hishamco Dec 1, 2023
e0fb3f3
MailRecipients -> MailMessageRecipients
hishamco Dec 1, 2023
7142247
Merge remote-tracking branch 'origin/main' into hishamco/email-azure
hishamco Dec 1, 2023
458072e
Revert changes from ServiceDescriptorExtensions
hishamco Dec 1, 2023
ef4a8bc
Merge remote-tracking branch 'origin/main' into hishamco/email-azure
hishamco Dec 11, 2023
93ed350
Remove keyed service
hishamco Dec 16, 2023
1ebde44
Merge remote-tracking branch 'origin/main' into hishamco/email-azure
hishamco Dec 20, 2023
36f8977
Use application/xml instead of text/xml
hishamco Dec 20, 2023
b81fc21
ccRecipients -> bccRecipients
hishamco Dec 20, 2023
2e40baa
Reorder the modifiers
hishamco Dec 20, 2023
7d378ae
Merge branch 'main' into hishamco/email-azure
hishamco Jan 3, 2024
53ceb20
Update src/OrchardCore.Modules/OrchardCore.Email.Smtp/Drivers/SmtpSet…
hishamco Jan 11, 2024
2716e83
Update src/OrchardCore.Modules/OrchardCore.Email.Smtp/Drivers/SmtpSet…
hishamco Jan 11, 2024
3037a91
Update src/OrchardCore/OrchardCore.Email.Core/Services/SmtpEmailServi…
hishamco Jan 11, 2024
cb8debb
Update src/OrchardCore.Modules/OrchardCore.Email.Azure/Services/Azure…
hishamco Jan 11, 2024
a607841
Update src/docs/reference/README.md
hishamco Jan 11, 2024
3aa1348
Update src/OrchardCore.Modules/OrchardCore.Email/Views/EmailSettings.…
hishamco Jan 11, 2024
e530d3d
Update src/OrchardCore/OrchardCore.Email.Core/Services/NullEmailServi…
hishamco Jan 11, 2024
0c93928
Update src/OrchardCore/OrchardCore.Email.Core/Services/SmtpEmailServi…
hishamco Jan 11, 2024
b58a9ff
Merge branch 'main' into hishamco/email-azure
hishamco Jan 11, 2024
957faa1
Move Microsoft.Extensions.Diagnostics.Testing to Dependencies
hishamco Jan 11, 2024
2ae0940
Address feedback
hishamco Jan 11, 2024
5e0d6e4
Fix doc
hishamco Jan 11, 2024
45e595e
Merge branch 'main' into hishamco/email-azure
hishamco Jan 11, 2024
eeacb04
Address feedback
hishamco Jan 15, 2024
2a7c24f
Fix the build
hishamco Jan 15, 2024
906baa0
Introduce EmailDeliveryService(s)
hishamco Jan 18, 2024
da7e1aa
Remove email services
hishamco Jan 18, 2024
3296011
Use file-scope namespaces
hishamco Jan 18, 2024
70c4017
Fix the broken test
hishamco Jan 18, 2024
6114f50
Modify the docs
hishamco Jan 18, 2024
a604ee6
Add release note
hishamco Jan 18, 2024
9bc4808
Add hint in SmtpEmailSettings.Edit.cshtml
hishamco Jan 18, 2024
1573d7f
Merge branch 'main' into hishamco/email-azure
hishamco Jan 18, 2024
9dd9c7e
Merge branch 'main' into hishamco/email-azure
hishamco Jan 23, 2024
0fb87c6
Split SMTP settings from email settings
hishamco Jan 18, 2024
96ec593
Split azure email mime types into another file for clarity
hishamco Jan 18, 2024
8fa8fba
AddEmailServices()
hishamco Jan 20, 2024
5aac300
Use keyed services
hishamco Jan 21, 2024
7971126
Add EmailDeliveryServiceResolver
hishamco Jan 23, 2024
67dd7e9
Merge branch 'main' into hishamco/email-azure
hishamco Jan 23, 2024
e2b25d2
Fix the build
hishamco Jan 23, 2024
dbb99b7
Formatting
hishamco Jan 24, 2024
1d40a82
Enable OC.Email by OC.Email.Smtp or OC.Email.Azure
hishamco Jan 24, 2024
6932ab4
Update src/docs/reference/modules/Email.Azure/README.md
hishamco Jan 28, 2024
bf482eb
Update src/docs/reference/modules/Email.Smtp/README.md
hishamco Jan 28, 2024
2a6878e
Update src/OrchardCore.Modules/OrchardCore.Email.Azure/Services/Azure…
hishamco Jan 28, 2024
03d9aca
Update src/OrchardCore/OrchardCore.Email.Abstractions/Services/IEmail…
hishamco Jan 28, 2024
a0a5940
Merge branch 'main' into hishamco/email-azure
hishamco Jan 28, 2024
81efd07
deliveryServiceName -> deliveryMethodName
hishamco Jan 28, 2024
40c7be3
Fix the removed key
hishamco Jan 28, 2024
85c65af
Refer to the docs
hishamco Jan 28, 2024
4770bf4
Remove IEmailMessageValidator from AdminController
hishamco Jan 28, 2024
e318063
Update src/docs/reference/modules/Email.Smtp/README.md
hishamco Jan 28, 2024
db131a0
Update src/docs/reference/modules/Email/README.md
hishamco Jan 28, 2024
a2e09e2
Log warning in NullEmailDeliveryService
hishamco Jan 29, 2024
3f7a69f
Add additional notes to the release notes
hishamco Jan 29, 2024
727e0ad
Move _allowedMimeTypes back to the base class
hishamco Jan 29, 2024
a11d223
Fix log error message
hishamco Jan 29, 2024
112bad5
Use more descriptive name
hishamco Jan 29, 2024
5fbb570
Fix localized message
hishamco Jan 29, 2024
049926e
Fix broken test
hishamco Jan 29, 2024
bbbb145
Validate() -> IsValidate()
hishamco Jan 29, 2024
40d7fdc
Update src/docs/releases/1.9.0.md
hishamco Jan 29, 2024
b8c0f81
Update src/OrchardCore/OrchardCore.Email.Core/Services/EmailMessageVa…
hishamco Jan 29, 2024
573f8fe
Remove EmailDeliveryServiceName from abstraction project
hishamco Jan 29, 2024
c2efdfe
submitterAddress -> senderAddress
hishamco Jan 29, 2024
9fbbfac
Move IEmailDeliveryServiceResolver to OC.Email.Services namespace
hishamco Jan 29, 2024
5931737
Add IEmailServiceEvents
hishamco Jan 29, 2024
627490d
Fix test
hishamco Jan 29, 2024
7a7d9c1
Split tests
hishamco Jan 29, 2024
46c5fda
Add Azure Email module docs in release notes
hishamco Jan 29, 2024
a14ade8
Simplify resolving keyed services
hishamco Jan 29, 2024
57d787a
IsValidate() -> IsValid()
hishamco Jan 29, 2024
f08c246
Fix the build
hishamco Jan 29, 2024
e52e895
Fix EmailDeliveryServiceResolver
hishamco Jan 29, 2024
09105ce
Update test/OrchardCore.Tests/Modules/OrchardCore.Email/EmailValidato…
hishamco Jan 30, 2024
361e326
Update src/OrchardCore/OrchardCore.Email.Core/Services/NullEmailDeliv…
hishamco Jan 30, 2024
c7fc3c9
Update src/docs/releases/1.9.0.md
hishamco Jan 30, 2024
2acfe6e
Update src/OrchardCore.Modules/OrchardCore.Email.Azure/Manifest.cs
hishamco Jan 30, 2024
778f560
deliveryMethodName -> deliveryServiceName
hishamco Jan 30, 2024
592f436
sender -> recipient
hishamco Jan 30, 2024
3950960
Fix SmtpEmailSettingsConfiguration
hishamco Jan 30, 2024
505813f
SMTP configuration should be backward compatible
hishamco Feb 1, 2024
e0fcd4b
NullEmailDeliveryService should fail when deliver an email
hishamco Feb 1, 2024
ab5183b
Add note for backward compatibility
hishamco Feb 1, 2024
760d520
Merge branch 'main' into hishamco/email-azure
hishamco Feb 1, 2024
d6f3d39
Fix the build
hishamco Feb 1, 2024
7cc40d0
Simplify resolving EmailDeliveryServiceResolver
hishamco Feb 1, 2024
69566e5
Add migration process to the release notes
hishamco Feb 1, 2024
64a070a
Fix the email menu
hishamco Feb 1, 2024
fb11fae
Introduce IEmailResult
hishamco Feb 1, 2024
42ccee8
Merge branch 'main' into hishamco/email-azure
hishamco Feb 1, 2024
58ec136
Update src/docs/releases/1.9.0.md
hishamco Feb 1, 2024
2f39c0e
Update src/OrchardCore.Modules/OrchardCore.Email.Azure/Views/Admin/Op…
hishamco Feb 1, 2024
2c22f1f
Update src/OrchardCore.Modules/OrchardCore.Email.Smtp/Views/SmtpEmail…
hishamco Feb 1, 2024
8283a70
Comment the settings
hishamco Feb 1, 2024
7791c2b
Remove unnecessary lines
hishamco Feb 1, 2024
75365fd
Remove SMTP keys from email section
hishamco Feb 1, 2024
636a1f4
Add new modules to mkdocs.yml
hishamco Feb 1, 2024
2df5c43
Merge branch 'main' into hishamco/email-azure
hishamco Feb 1, 2024
6309a81
Add video to the docs
hishamco Feb 2, 2024
98c53a9
Merge branch 'main' into hishamco/email-azure
hishamco Feb 2, 2024
ac8d13c
Merge branch 'main' into hishamco/email-azure
hishamco Feb 2, 2024
4e40444
Merge branch 'main' into hishamco/email-azure
hishamco Feb 3, 2024
828757d
Apply suggestions from code review
hishamco Feb 3, 2024
c7f838e
Format admin menu & permissions
hishamco Feb 3, 2024
95131c4
Use constants
hishamco Feb 3, 2024
47e52db
Use protected
hishamco Feb 3, 2024
62b2155
Fix docs
hishamco Feb 3, 2024
ca86ef2
Use NavigationHelper
hishamco Feb 3, 2024
dbe443c
Save indentation
hishamco Feb 3, 2024
1153d45
Don't use nameof
hishamco Feb 3, 2024
62a46ca
Fix the log message
hishamco Feb 3, 2024
b5d9cd2
Use file-scope
hishamco Feb 3, 2024
d65d580
Merge branch 'main' into hishamco/email-azure
hishamco Feb 3, 2024
83aae0a
Remove unnecessary Order
hishamco Feb 3, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions OrchardCore.sln
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OrchardCore.Sms", "src\Orch
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OrchardCore.Sms.Core", "src\OrchardCore\OrchardCore.Sms.Core\OrchardCore.Sms.Core.csproj", "{20356393-B16D-466C-8203-877A534E287D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OrchardCore.Email.Azure", "src\OrchardCore.Modules\OrchardCore.Email.Azure\OrchardCore.Email.Azure.csproj", "{91DD6469-20AE-4F94-AF49-C13E8FCA4343}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -1335,6 +1337,10 @@ Global
{20356393-B16D-466C-8203-877A534E287D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{20356393-B16D-466C-8203-877A534E287D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{20356393-B16D-466C-8203-877A534E287D}.Release|Any CPU.Build.0 = Release|Any CPU
{91DD6469-20AE-4F94-AF49-C13E8FCA4343}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{91DD6469-20AE-4F94-AF49-C13E8FCA4343}.Debug|Any CPU.Build.0 = Debug|Any CPU
{91DD6469-20AE-4F94-AF49-C13E8FCA4343}.Release|Any CPU.ActiveCfg = Release|Any CPU
{91DD6469-20AE-4F94-AF49-C13E8FCA4343}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -1566,6 +1572,7 @@ Global
{2D93F509-1FB3-4E22-92F0-588D0EFBA921} = {F23AC6C2-DE44-4699-999D-3C478EF3D691}
{CBF6DB53-FD0C-47F8-9E60-A1D247ACFD05} = {A066395F-6F73-45DC-B5A6-B4E306110DCE}
{20356393-B16D-466C-8203-877A534E287D} = {F23AC6C2-DE44-4699-999D-3C478EF3D691}
{91DD6469-20AE-4F94-AF49-C13E8FCA4343} = {A066395F-6F73-45DC-B5A6-B4E306110DCE}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {46A1D25A-78D1-4476-9CBF-25B75E296341}
Expand Down
1 change: 1 addition & 0 deletions src/OrchardCore.Build/Dependencies.AspNetCore.props
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
<!-- 'Microsoft.Extensions' packages that are not included in the ASP.NET Core shared framework -->
<ItemGroup>
<PackageManagement Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="$(MicrosoftExtensionsPackagesVersion)" />
<PackageManagement Include="Microsoft.Extensions.Diagnostics.Testing" Version="$(MicrosoftExtensionsPackagesVersion)" />
<PackageManagement Include="Microsoft.Extensions.Http.Polly" Version="$(MicrosoftExtensionsPackagesVersion)" />
</ItemGroup>

Expand Down
1 change: 1 addition & 0 deletions src/OrchardCore.Build/Dependencies.props
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<PackageManagement Include="AWSSDK.S3" Version="3.7.104.11" />
<PackageManagement Include="AWSSDK.Extensions.NETCore.Setup" Version="3.7.7" />
<PackageManagement Include="AWSSDK.SecurityToken" Version="3.7.101.60" />
<PackageManagement Include="Azure.Communication.Email" Version="1.0.1" />
<PackageManagement Include="Azure.Extensions.AspNetCore.Configuration.Secrets" Version="1.3.0" />
<PackageManagement Include="Azure.Extensions.AspNetCore.DataProtection.Blobs" Version="1.3.2" />
<PackageManagement Include="Azure.Identity" Version="1.10.4" />
Expand Down
4 changes: 4 additions & 0 deletions src/OrchardCore.Cms.Web/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
// "BlobName": "", // Optional, defaults to Sites/tenant_name/DataProtectionKeys.xml. Templatable, refer docs.
// "CreateContainer": true // Creates the container during app startup if it does not already exist.
//},
//"OrchardCore_Email_Azure": {
// "DefaultSender": "",
// "ConnectionString": "" // Set to your Azure Communication Service email connection string.
//},
// See https://docs.orchardcore.net/en/latest/docs/reference/modules/Markdown/#markdown-configuration
//"OrchardCore_Markdown": {
// "Extensions": "nohtml+advanced"
Expand Down
36 changes: 36 additions & 0 deletions src/OrchardCore.Modules/OrchardCore.Email.Azure/AdminMenu.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Localization;
using OrchardCore.Navigation;

namespace OrchardCore.Email.Azure
{
public class AdminMenu : INavigationProvider
{
protected readonly IStringLocalizer S;

public AdminMenu(IStringLocalizer<AdminMenu> localizer)
{
S = localizer;
}

public Task BuildNavigationAsync(string name, NavigationBuilder builder)
{
if (!string.Equals(name, "admin", StringComparison.OrdinalIgnoreCase))
return Task.CompletedTask;

builder
.Add(S["Configuration"], configuration => configuration
.Add(S["Settings"], settings => settings
.Add(S["Email"], S["Email"].PrefixPosition(), email => email
.AddClass("email").Id("email")
.Add(S["Azure Email Settings"], S["Azure Email Settings"].PrefixPosition(), options => options
.Action("Options", "Admin", new { area = "OrchardCore.Email.Azure" })
.Permission(Permissions.ViewAzureEmailOptions)
.LocalNav())
)));

return Task.CompletedTask;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace OrchardCore.Email.Azure
{
/// <summary>
/// Represents a settings for Azure email.
/// </summary>
public class AzureEmailSettings : EmailSettings
{
/// <summary>
/// Gets or sets the connection string.
/// </summary>
public string ConnectionString { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using OrchardCore.Email.Azure.ViewModels;

namespace OrchardCore.Email.Azure.Controllers
{
public class AdminController : Controller
{
private readonly IAuthorizationService _authorizationService;
private readonly AzureEmailSettings _options;

public AdminController(IAuthorizationService authorizationService, IOptions<AzureEmailSettings> options)
{
_authorizationService = authorizationService;
_options = options.Value;
}

public async Task<IActionResult> Options()
BenedekFarkas marked this conversation as resolved.
Show resolved Hide resolved
{
if (!await _authorizationService.AuthorizeAsync(User, Permissions.ViewAzureEmailOptions))
{
return Forbid();
}

var model = new OptionsViewModel
{
DefaultSender = _options.DefaultSender,
ConnectionString = _options.ConnectionString
};

return View(model);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Microsoft.Extensions.Configuration;
using OrchardCore.Email.Azure;
using OrchardCore.Environment.Shell.Configuration;

namespace Microsoft.Extensions.DependencyInjection
{
public static class OrchardCoreBuilderExtensions
{
public static OrchardCoreBuilder ConfigureAzureEmailSettings(this OrchardCoreBuilder builder)
{
builder.ConfigureServices((tenantServices, serviceProvider) =>
{
var configurationSection = serviceProvider.GetRequiredService<IShellConfiguration>().GetSection("OrchardCore_Email_Azure");

tenantServices.PostConfigure<AzureEmailSettings>(settings => configurationSection.Bind(settings));
});

return builder;
}
}
}
11 changes: 11 additions & 0 deletions src/OrchardCore.Modules/OrchardCore.Email.Azure/Manifest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using OrchardCore.Modules.Manifest;

[assembly: Module(
Name = "Azure Email Communication Services",
hishamco marked this conversation as resolved.
Show resolved Hide resolved
Author = ManifestConstants.OrchardCoreTeam,
Website = ManifestConstants.OrchardCoreWebsite,
Version = ManifestConstants.OrchardCoreVersion,
Description = "Provides email settings configuration and a default email service based on Azure Communication Services (ACS).",
hishamco marked this conversation as resolved.
Show resolved Hide resolved
Dependencies = ["OrchardCore.Email"],
Category = "Messaging"
)]
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
<!-- NuGet properties-->
<Title>OrchardCore Email</Title>
<Description>$(OCFrameworkDescription)

Provides email settings configuration and a default email service based on SMTP.</Description>
<PackageTags>$(PackageTags) OrchardCoreFramework</PackageTags>
</PropertyGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\OrchardCore\OrchardCore.Admin.Abstractions\OrchardCore.Admin.Abstractions.csproj" />
<ProjectReference Include="..\..\OrchardCore\OrchardCore.DisplayManagement\OrchardCore.DisplayManagement.csproj" />
<ProjectReference Include="..\..\OrchardCore\OrchardCore.Email.Core\OrchardCore.Email.Core.csproj" />
<ProjectReference Include="..\..\OrchardCore\OrchardCore.Navigation.Core\OrchardCore.Navigation.Core.csproj" />
<ProjectReference Include="..\..\OrchardCore\OrchardCore.Module.Targets\OrchardCore.Module.Targets.csproj" />
<ProjectReference Include="..\..\OrchardCore\OrchardCore.ResourceManagement\OrchardCore.ResourceManagement.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Azure.Communication.Email" />
</ItemGroup>

</Project>
33 changes: 33 additions & 0 deletions src/OrchardCore.Modules/OrchardCore.Email.Azure/Permissions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using OrchardCore.Security.Permissions;

namespace OrchardCore.Email.Azure
{
public class Permissions : IPermissionProvider
{
public static readonly Permission ViewAzureEmailOptions = new(nameof(ViewAzureEmailOptions), "View Azure Email Options");

public Task<IEnumerable<Permission>> GetPermissionsAsync()
{
return Task.FromResult(new[]
{
ViewAzureEmailOptions,
}
.AsEnumerable());
}

public IEnumerable<PermissionStereotype> GetDefaultStereotypes()
{
return new[]
{
new PermissionStereotype
{
Name = "Administrator",
Permissions = new[] { ViewAzureEmailOptions },
},
};
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
using System.Threading.Tasks;
using Azure;
using Azure.Communication.Email;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using OrchardCore.Email.Services;

namespace OrchardCore.Email.Azure.Services;

public class AzureEmailService : EmailServiceBase<AzureEmailSettings>
{
/// <summary>
/// Initializes a new instance of a <see cref="AzureEmailService"/>.
/// </summary>
/// <param name="options">The <see cref="IOptions{AzureEmailSettings}"/>.</param>
/// <param name="logger">The <see cref="ILogger{AzureEmailService}"/>.</param>
/// <param name="stringLocalizer">The <see cref="IStringLocalizer{AzureEmailService}"/>.</param>
public AzureEmailService(
IOptions<AzureEmailSettings> options,
ILogger<AzureEmailService> logger,
IStringLocalizer<AzureEmailService> stringLocalizer) : base(options, logger, stringLocalizer)
{
}

public async override Task<EmailResult> SendAsync(MailMessage message)
{
if (message == null)
{
throw new ArgumentNullException(nameof(message));
}

if (Settings == null)
{
return EmailResult.Failed(S["Azure Email settings must be configured before an email can be sent."]);
}

EmailResult result;
var client = new EmailClient(Settings.ConnectionString);

try
{
var senderAddress = string.IsNullOrWhiteSpace(message.From)
? Settings.DefaultSender
: message.From;

if (!string.IsNullOrWhiteSpace(senderAddress))
{
message.From = senderAddress;
}

var htmlContent = message.IsHtmlBody
? message.Body
: null;
var plainTextContent = message.IsHtmlBody
? null
: message.Body;

await client.SendAsync(WaitUntil.Completed, senderAddress, message.To, message.Subject, htmlContent, plainTextContent: plainTextContent);

result = EmailResult.Success;
}
catch (Exception ex)
{
result = EmailResult.Failed(S["An error occurred while sending an email: '{0}'", ex.Message]);
Piedone marked this conversation as resolved.
Show resolved Hide resolved
}

return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using OrchardCore.Email.Azure;
using OrchardCore.Environment.Shell.Configuration;
using OrchardCore.Entities;
using OrchardCore.Settings;

namespace OrchardCore.Email.Services
{
public class AzureEmailSettingsConfiguration : IConfigureOptions<AzureEmailSettings>
{
private readonly IShellConfiguration _shellConfiguration;
private readonly ISiteService _site;

public AzureEmailSettingsConfiguration(IShellConfiguration shellConfiguration, ISiteService site)
{
_shellConfiguration = shellConfiguration;
_site = site;
}

public void Configure(AzureEmailSettings options)
{
var section = _shellConfiguration.GetSection("OrchardCore_Email_Azure");

var emailSettings = _site.GetSiteSettingsAsync()
.GetAwaiter().GetResult()
.As<EmailSettings>();

options.DefaultSender = section.GetValue(nameof(options.DefaultSender), emailSettings.DefaultSender);
options.ConnectionString = section.GetValue(nameof(options.ConnectionString), string.Empty);
}
}
}
Loading