If you wish to integrate an email provider on your CoreConnect application, you can do so by enabling one of these plugins in your Functions service.
Each email type should be linked to a specific Queue on your queuing system (e.g. reset-password-email-default
)
SendGrid
With this plugin you can integrate your SendGrid account with your CoreConnect application. This plugin should only be installed in the Functions service, it automatically registers queues for signUp and resetPassword emails. Additional queues can be added via custom plugins.
Name | CoreConnect.Email.SendGrid |
Choice | true |
Configuration name | EmailSettings |
Configuration | { "APIKey": "", "SenderName": "noreply", "SenderEmail": "[email protected]", "EmailTemplates": [], "Actions": { "Registration": "Registration", "ResetPassword": "ResetPassword", "Wishlist": "Wishlist" }, "TemplateVariables": [ { "Key": "Registration", "TokenName": "accountCreationToken" }, { "Key": "PasswordReset", "TokenName": "resetPasswordToken" }, { "Key": "Wishlist", "TokenName": "" } ] } |
Consumers Plugin
This plugin should be installed in the Functions service along with one email provider plugin (e.g. SendGrid), it automatically registers queues for signUp and resetPassword emails.
Name | CoreConnect.Email.ConsumersPlugin |
Choice | true |
Configuration | There is no specific configuration for this plugin |
Technical details
Once you have activated an email provider plugin in your CoreConnect application, you will likely want to add new email types to be sent for specific events. This can be done directly on your service or via a custom plugin. These are the necessary steps to add a new email type:
- Create a new custom plugin (see Custom Plugins) and override the
RegisterQueues
method to add a new queue and a new consumer for your new email type. In this example it will be an email to be sent when a user’s wishlist is updated (e.g. one of the items’ price has been reduced):
using CoreConnect.Plugins;
namespace MyEmailTypes
public class MyCustomPlugin : CoreConnectPlugin{ public override string Name => "MyCustomPlugin"; public override string ConfigurationName => "MyCustomPluginSettings"; public override string Description => "Custom plugin for CoreConnect";
...
public override void RegisterQueues(ILogger logger, IQueueRegistration registration) { registration.RegisterQueue<WishlistEmailMessage, WishlistEmailConsumer>("wishlist-changes-email-default"); }}
- Create the classes for the email message and consumer:
using CoreConnect.Email.Models;using CoreConnect.Email.Services;using MassTransit;using Microsoft.Extensions.Logging;
namespace MyEmailTypes
public class WishlistEmailMessage{ public string? Email { get; set; } public string? Locale { get; set; } public string? CountryCode { get; set; }}
public class WishlistEmailConsumer(IEmailProvider emailService, ILogger<WishlistEmailConsumer> logger) : IConsumer<WishlistEmailMessage>{ private readonly IEmailProvider _emailService = emailService; private readonly ILogger<WishlistEmailConsumer> _logger = logger; private readonly string _actionType = "Wishlist"
public async Task Consume(ConsumeContext<WishlistEmailMessage> context) { var actionType = _emailService.GetEmailActionTypes().FirstOrDefault( kvp => kvp.Key == _actionType).Value ?? _actionType;
if (!string.IsNullOrEmpty(context.Message.Email)) { try {
await _emailService.SendEmailAsync(context.Message.Email, $"{_actionType}_{context.Message.Locale}-{context.Message.CountryCode}", string.Empty);
_logger.LogInformation(LogMessages.EmailSentSuccessfully, context.Message.Email); } catch (Exception e) { _logger.LogError(LogMessages.EmailFailed, context.Message.Email, e.Message); } } }}
- Add the configuration for the new email type on your email plugin’s configuration (e.g.
EmailSettings
for the SendGrid plugin):
{ "EmailSettings": { ... "Actions": { ... "Wishlist": "Wishlist" }, "TemplateVariables": [ ... { "Key": "Wishlist", "TokenName": "" } ], "EmailTemplates": [ ... { "TemplateId": "<template-id-from-sendgrid>", "TemplateName": "Wishlist", "Subject": "Changes on your wishlist", }, { "TemplateId": "<template-id-from-sendgrid>", "TemplateName": "Wishlist_en-NL", "Subject": "Changes on your wishlist", }, { "TemplateId": "<template-id-from-sendgrid>", "TemplateName": "Wishlist_nl-NL", "Subject": "Wijzigingen op uw verlanglijstje", }, ] }}
- At this point your plugin is ready to send emails when a new message of type
WishlistEmailMessage
is published to thewishlist-changes-email-default
queue. You can now create a new service for the Gateway on your plugin to handle the logic and send a message to the queue when the event occurs.
using CoreConnect.Plugins;
namespace MyEmailTypes
public class MyCustomPlugin : CoreConnectPlugin{ ... public virtual void RegisterGatewayServices(ILogger logger, IConfiguration configuration, IServiceCollection builder) { builder.AddScoped<IWishlistService, WishlistService>(); }
...
public class WishlistService : IWishlistService { ...
public async Task WishlistUpdatedAsync(WishlistUpdateMessage message) { ...
WishlistEmailMessage message = new() { CountryCode = requestContext.CountryCode, Locale = requestContext.Locale, Email = response.Customer.Email, };
await bus.Publish(message, cancellation); } }}