logo.png
Synopse mORMot Framework API Reference

dddInfraEmailer.pas unit

Purpose: Shared DDD Infrastructure: generic emailing service
- this unit is a part of the freeware Synopse mORMot framework, licensed under a MPL/GPL/LGPL tri-license; version 1.18

1.1. Units used in the dddInfraEmailer unit

Unit NameDescription
dddDomUserInterfacesShared DDD Domains: User interfaces definition
dddDomUserTypesShared DDD Domains: User objects definition
dddInfraEmailShared DDD Infrastructure: implement an email validation service
mORMotCommon ORM and SOA classes for mORMot
mORMotDDDDomain-Driven-Design toolbox for mORMot
SynCommonsCommon functions used by most Synopse projects
SynCrtSockClasses implementing TCP/UDP/HTTP client and server protocol
SynLogLogging functions used by Synopse projects
SynMustacheLogic-less mustache template rendering
SynTableFilter/database/cache/buffer/security/search/multithread/OS features
SynTestsUnit test functions used by Synopse projects

1.2. dddInfraEmailer class hierarchy

TSynMonitorWithSizeTDDDEmailerDaemonStatsTSQLRecordTimedTSQLRecordEmailerTInterfaceResolverForSingleInterfaceTSMTPServerTSMTPServerSocketConnectionAbstractTSMTPServerSocketConnectionTInterfacedObjectTDDDMonitoredDaemonProcessRestTDDDEmailerDaemonProcessTDDDMonitoredDaemonTDDDEmailerDaemonTDDDTemplateAbstractTDDDTemplateFromFolderTCQRSServiceIInvokableISMTPServerConnection
dddInfraEmailer class hierarchy

1.3. Objects implemented in the dddInfraEmailer unit

ObjectsDescription
ISMTPServerConnectionUsed to inject the exact SMTP process to TDDDEmailerDaemon
TDDDEmailerDaemonDaemon used to send emails via SMTP
TDDDEmailerDaemonProcessThread processing a SMTP connection
TDDDEmailerDaemonStatsStatistics about a TDDDEmailerDaemon instance
TDDDTemplateAbstractAbstract Mustache-Based templating
TDDDTemplateFromFolderMustache-Based templating from a local folder
TSMTPServerAbstract class used to resolve ISMTPServerConnection
TSMTPServerSocketConnectionImplements ISMTPServerConnection using SynCrtSock's low-level SMTP access
TSMTPServerSocketConnectionAbstractImplements an abstract ISMTPServerConnection class
TSQLRecordEmailerORM class for email validation process

1.3.1. ISMTPServerConnection

ISMTPServerConnection = interface(IInvokable)

Used to inject the exact SMTP process to TDDDEmailerDaemon


function SendEmail(const aRecipient: TRawUTF8DynArray; const aSender,aSubject,aHeader,aBody: RawUTF8): RawUTF8;

This method should send the email, returning an error message on issue
- if no header is supplied, it will expect one UTF-8 encoded text message


1.3.2. TSMTPServer

TSMTPServer = class(TInterfaceResolverForSingleInterface)

Abstract class used to resolve ISMTPServerConnection
- see TSMTPServerSocket for actual implementation


constructor Create(aImplementation: TInterfacedObjectClass; aParameters: TSMTPServer); overload;

Initialize the class with the parameters of another TSMTPServer instance
- in fact, TSMTPServer could be used as parameter storage of its needed published properties, e.g. in a TApplicationSettingsAbstract sub-class


constructor Create(aImplementation: TInterfacedObjectClass; const aAddress: RawUTF8; aPort: cardinal; const aLogin,aPassword: RawUTF8); overload;

Initialize the class with the supplied parameters


procedure SetDefaultValuesIfVoid;

Will fill some default values in the properties, if none is set
- i.e. 'dummy:dummy@localhost:25'


1.3.3. TSMTPServerSocketConnectionAbstract

TSMTPServerSocketConnectionAbstract = class(TInterfacedObject)

Implements an abstract ISMTPServerConnection class


1.3.4. TSMTPServerSocketConnection

TSMTPServerSocketConnection = class(TSMTPServerSocketConnectionAbstract)

Implements ISMTPServerConnection using SynCrtSock's low-level SMTP access


1.3.5. TDDDEmailerDaemonStats

TDDDEmailerDaemonStats = class(TSynMonitorWithSize)

Statistics about a TDDDEmailerDaemon instance
- in addition to a standard TSynMonitor, will maintain the connection count


procedure NewConnection;

Will increase the connection count


property Connection: cardinal read fConnection;

The connection count


1.3.6. TDDDEmailerDaemonProcess

TDDDEmailerDaemonProcess = class(TDDDMonitoredDaemonProcessRest)

Thread processing a SMTP connection


1.3.7. TDDDEmailerDaemon

TDDDEmailerDaemon = class(TDDDMonitoredDaemon)

Daemon used to send emails via SMTP
- it will maintain a list of action in a TSQLRecordEmailer ORM storage


function SendEmail(const aRecipients: TRawUTF8DynArray; const aSender,aSubject,aHeaders,aBody: RawUTF8): TCQRSResult;

This is the main entry point of this service
- here the supplied message body is already fully encoded, as expected by SMTP (i.e. as one text message, or multi-part encoded)
- if no header is supplied, it will expect one UTF-8 encoded text message


property RestClass: TSQLRecordEmailerClass read fRestClass;

The associated class TSQLRecordEmailer used for status persistence
- any class inheriting from TSQLRecordEmailer in the Rest.Model will be recognized by TDDDEmailerDaemon to store its information


property SMTPServer: TSMTPServer read fSMTPServer write fSMTPServer;

The associated class used as actual SMTP client


1.3.8. TSQLRecordEmailer

TSQLRecordEmailer = class(TSQLRecordTimed)

ORM class for email validation process
- we do not create a whole domain here, just an ORM persistence layer


class procedure InitializeTable(Server: TSQLRestServer; const FieldName: RawUTF8; Options: TSQLInitializeTableOptions); override;

Will create an index on State+ID


1.3.9. TDDDTemplateAbstract

TDDDTemplateAbstract = class(TCQRSService)

Abstract Mustache-Based templating


1.3.10. TDDDTemplateFromFolder

TDDDTemplateFromFolder = class(TDDDTemplateAbstract)

Mustache-Based templating from a local folder


1.4. Types implemented in the dddInfraEmailer unit

1.4.1. TSQLRecordEmailerState

TSQLRecordEmailerState = ( esPending, esSending, esSent, esFailed );

State machine used during email validation process


1.5. Functions or procedures implemented in the dddInfraEmailer unit

Functions or proceduresDescription
TestDddInfraEmailerYou can call this function within a TSynTestCase class to validate the email validation via a full regression set

1.5.1. TestDddInfraEmailer

procedure TestDddInfraEmailer(serverClass: TSQLRestServerClass; test: TSynTestCase);

You can call this function within a TSynTestCase class to validate the email validation via a full regression set
- could be used as such:

procedure TTestCrossCuttingFeatures.Emailer;
begin // TSQLRestServerDB is injected to avoid any dependency to mORMotSQLite3
  TestDddInfraEmailer(TSQLRestServerDB,self);
end;