Qore SmtpClient Module Reference 2.0
Loading...
Searching...
No Matches
SmtpClient.qm.dox.h
1// -*- mode: c++; indent-tabs-mode: nil -*-
3
4/* SmtpClient.qm Copyright 2012 - 2023 Qore Technologies, s.r.o.
5
6 Original Authors: Wolfgang Ritzinger, Marian Bonda, Pavol Potoncok
7
8 Permission is hereby granted, free of charge, to any person obtaining a
9 copy of this software and associated documentation files (the "Software"),
10 to deal in the Software without restriction, including without limitation
11 the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 and/or sell copies of the Software, and to permit persons to whom the
13 Software is furnished to do so, subject to the following conditions:
14
15 The above copyright notice and this permission notice shall be included in
16 all copies or substantial portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 DEALINGS IN THE SOFTWARE.
25*/
26
27// minimum qore version
28
29// need mime definitions
30
31// need MailMessage classes
32
33
34// assume local var scope, do not use "$" for vars, members, and method calls
35
36
221
228namespace SmtpClient {
231
234
236public hashdecl SmtpResponseInfo {
238 int code;
239
241 string desc;
242};
243
246
247public:
249
260 constructor(string sender, string subject) ;
261
262};
263
266
267public:
268};
269
271
275
276public:
278protected:
279 Socket sock();
280
281 // connect string
282 string connect;
283
284 // ensures exclusive access to the object
285 Mutex mutex();
286
287 bool nosend = False;
288
289 // optional info log closure
290 *code log_info;
291
292 // optional debug log closure
293 *code log_debug;
294
295 // tls flag (ie \c "STARTTLS" flag; ie application layer security)
296 bool tls = False;
297
298 // ssl flag (for TLS/SSL connections - ie transport layer instead of application layer security)
299 bool ssl = False;
300
301 // esmtp flag
302 bool esmtp;
303
304 // authentication credentials
305 *string user;
306 *string pass;
307
308 // logged in flag
309 bool logged_in = False;
310
311 // read timeout in milliseconds
312 timeout readTimeout = DefaultReadTimeout;
313
314 // connect timeout in milliseconds
315 timeout connectTimeout = DefaultConnectTimeout;
316
317 // HELO/EHLO reply
318 hash<auto> hello_reply;
319
320 const MaxDebugLine = 2048;
321
322public:
324
326 const SmtpPort = 25;
327
329
334 const SmtpsPort = 465;
335
337 const EsmtpPort = 587;
338
340 const Protocols = {
341 "smtp": (
342 "port": SmtpPort,
343 "ssl": False,
344 "tls": False,
345 ),
346 "smtps": (
347 "port": SmtpsPort,
348 "ssl": True,
349 "tls": False,
350 ),
351 "smtptls": (
352 "port": SmtpsPort,
353 "ssl": False,
354 "tls": True,
355 ),
356 "esmtp": (
357 "port": EsmtpPort,
358 "ssl": False,
359 "tls": False,
360 ),
361 "esmtps": (
362 "port": SmtpsPort,
363 "ssl": True,
364 "tls": False,
365 ),
366 "esmtptls": (
367 "port": EsmtpPort,
368 "ssl": False,
369 "tls": True,
370 ),
371 };
372
374
381 constructor(string host, softint port, *code log, *code dbglog);
382
383
385
405 constructor(string url, *code log, *code dbglog);
406
407
409
412
413
415
422 hash<auto> getInfo();
423
424
427
428
431
432
434
436 string getTarget();
437
438
440
444 tls(bool n_tls);
445
446
448 bool tls();
449
450
452
456 ssl(bool n_ssl);
457
458
460 bool ssl();
461
462
464
471 setUserPass(string n_user, string n_pass);
472
473
475 test(bool ns);
476
477
479 bool test();
480
481
483
488
489
492
493
495
498
499
501 setReadTimeout(timeout to);
502
503
506
507
510
511
513 setConnectTimeout(timeout to);
514
515
518
519
522
523
525
539
540
542
545
546
548
558
559
561
597 nothing setWarningQueue(int warning_ms, int warning_bs, Queue queue, auto arg, timeout min_ms = 1s);
598
599
601
622 hash<auto> getUsageInfo();
623
624
626
636
637
639
655 setEventQueue(Qore::Thread::Queue queue, auto arg, *bool with_data);
656
657
659
672
673
675 // don't reimplement this method; fix/enhance it in the module
676protected:
677 final disconnectIntern();
678public:
679
680
681protected:
682 log(string msg);
683public:
684
685
686protected:
687 logDbg(string msg);
688public:
689
690
691protected:
692 connectIntern();
693public:
694
695
696protected:
697 loginIntern();
698public:
699
700
701 // send data over the socket
702protected:
703 sendDataIntern(data str);
704public:
705
706
707 // send data and log in the debug log if set
708protected:
709 sendData(string str);
710public:
711
712
713 // send data and log in the debug log if set
714protected:
715 sendData(binary b);
716public:
717
718
719 // send a command over the socket and return the response as a hash
720 // don't reimplement this method; fix/enhance it in the module
721protected:
722 final hash<SmtpResponseInfo> sendCommand(string str);
723public:
724
725
726 // read a line from the socket (terminated with \n)
727protected:
728 string readLine(timeout to);
729public:
730
731
732 // sends the message header (without body & attachments) to the SMTP server
733 // don't reimplement this method; fix/enhance it in the module
734protected:
735 final hash<auto> sendMessageInfoIntern(MailMessage::Message message);
736public:
737
738
739protected:
740 forceDisconnectIntern();
741public:
742
744};
745
748
749public:
751 const SPS_CONNECT = "connect";
752
754 const SPS_READ_HELLO = "read-hello";
755
757 const SPS_SEND_HELLO = "send-hello";
758
760 const SPS_READ_HELLO_REPLY = "read-hello-reply";
761
763 const SPS_STARTTLS = "starttls";
764
766 const SPS_READ_STARTTLS_REPLY = "read-starttls-reply";
767
769 const SPS_UPGRADE_TO_TLS = "upgrade-tls";
770
772 const SPS_LOGIN = "login";
773
775 const SPS_READ_LOGIN_REPLY = "read-login-reply";
776
778 const SPS_COMPLETE = "complete";
779
780protected:
782 hash<auto> info;
783
785 Socket sock();
786
788 AbstractPollOperation poller;
789
791 Mutex m();
792
794 string state;
795
797 bool goal_reached = False;
798
800 bool esmtp = False;
801
804
807
810
813
816
819
820public:
821
824
825
827
829 string getGoal();
830
831
833
835 string getState();
836
837
839
842
843
845 *hash<SocketPollInfo> continuePoll();
846
847
849protected:
851public:
852
853
855
857protected:
858 bool getReply(reference<int> rcode, reference<string> rmsg);
859public:
860
861};
862
864
871
872public:
874 const ConnectionScheme = <ConnectionSchemeInfo>{
875 "cls": Class::forName("SmtpConnection"),
876 };
877
879
889 constructor(string name, string description, string url, hash<auto> attributes = {}, hash<auto> options = {})
890 ;
891
892
894 string getType();
895
896
898
902 *hash<auto> getRuntimeOptions();
903
904
906
910 DataProvider::AbstractDataProvider getDataProvider();
911
912
914
919
920
922
927
928
930
938
939
941protected:
943public:
944
945
947
954protected:
955 SmtpClient getImpl(bool connect = True, *hash<auto> rtopts);
956public:
957
958
960protected:
961 hash<ConnectionSchemeInfo> getConnectionSchemeInfoImpl();
962public:
963
964};
965
967
969class SmtpClientDataProvider : public DataProvider::AbstractDataProvider {
970
971public:
973 const ProviderInfo = <DataProviderInfo>{
974 "type": "SmtpClientDataProvider",
975 "supports_children": True,
976 "constructor_options": ConstructorOptions,
977 "children_can_support_apis": True,
978 "children_can_support_records": False,
979 "children_can_support_observers": False,
980 };
981
984 "url": <DataProviderOptionInfo>{
985 "type": AbstractDataProviderType::get(StringType),
986 "desc": "A URL for an SMTP server",
987 "required": True,
988 },
989 };
990
991protected:
994
995 const ChildMap = {
996 "send-email": Class::forName("SmtpClient::SmtpClientSendEmailDataProvider"),
997 };
998
999public:
1000
1002 constructor(*hash<auto> options);
1003
1004
1007
1008
1009 string getName();
1010
1011
1013 *string getDesc();
1014
1015
1017 *list<hash<DataProviderSummaryInfo>> getChildProviderSummaryInfo();
1018
1019
1021
1023protected:
1025public:
1026
1027
1029
1033protected:
1034 *AbstractDataProvider getChildProviderImpl(string name);
1035public:
1036
1037
1039protected:
1040 hash<DataProviderInfo> getStaticInfoImpl();
1041public:
1042
1043};
1044
1046class SmtpClientSendEmailDataProvider : public AbstractDataProvider {
1047
1048public:
1051
1053 const ProviderInfo = <DataProviderInfo>{
1054 "name": "send-email",
1055 "desc": "SMTP send email data provider",
1056 "type": "SmtpClientSendEmailDataProvider",
1057 "constructor_options": SmtpClientDataProvider::ConstructorOptions,
1058 "supports_request": True,
1059 };
1060
1062 const ProviderSummaryInfo = cast<hash<DataProviderSummaryInfo>>(ProviderInfo{
1063 AbstractDataProvider::DataProviderSummaryInfoKeys
1064 });
1065
1068
1071
1073 constructor(*hash<auto> options);
1074
1075
1078
1079
1081 string getName();
1082
1083
1085
1090protected:
1091 auto doRequestImpl(auto req, *hash<auto> request_options);
1092public:
1093
1094
1096
1098protected:
1099 *AbstractDataProviderType getRequestTypeImpl();
1100public:
1101
1102
1104
1106protected:
1107 *AbstractDataProviderType getResponseTypeImpl();
1108public:
1109
1110
1112 hash<DataProviderInfo> getStaticInfoImpl();
1113
1114};
1115
1117class SmtpSendEmailAttachmentDataType : public HashDataType {
1118
1119public:
1120protected:
1122 const Fields = {
1123 "data": {
1124 "type": DataType,
1125 "desc": "The data for the attachment",
1126 "required": True,
1127 },
1128 "encoding": {
1129 "type": StringType,
1130 "desc": "The encoding to use for the attachment:\n"
1131 "- `default`: `quoted-printable` for string data, `base64` for binary data\n"
1132 "- `none`: no content encoding (not recommended)\n"
1133 "- `quoted-printable`: quoted printable encoding \n"
1134 "- `base64`: base64 encoding",
1135 "default_value": "default",
1136 },
1137 "hdr": {
1138 "type": HashOrNothingType,
1139 "desc": "Optional headers to send with the attachment",
1140 },
1141 "mime-type": {
1142 "type": StringType,
1143 "desc": "The MIME type of the attachment",
1144 "required": True,
1145 },
1146 "name": {
1147 "type": StringType,
1148 "desc": "The name or filename of the attachment",
1149 "required": True,
1150 },
1151 };
1152
1153public:
1154
1157
1158};
1159
1161class SmtpSendEmailRequestDataType : public HashDataType {
1162
1163public:
1164protected:
1166 const Fields = {
1167 "attachments": {
1168 "type": new SoftListDataType(new SmtpSendEmailAttachmentDataType(), True),
1169 "desc": "Any attachments for the message",
1170 },
1171 "bcc": {
1172 "type": new SoftListDataType(StringType, True),
1173 "desc": "One or more `Bcc:` addresses for the email",
1174 },
1175 "body": {
1176 "type": DataType,
1177 "desc": "The message body",
1178 },
1179 "body-encoding": {
1180 "type": StringType,
1181 "desc": "The encoding to use for the attachment:\n"
1182 "- `default`: `quoted-printable` for string data, `base64` for binary data\n"
1183 "- `none`: no content encoding (not recommended)\n"
1184 "- `quoted-printable`: quoted printable encoding \n"
1185 "- `base64`: base64 encoding",
1186 "default_value": "default",
1187 },
1188 "body-mime-type": {
1189 "type": StringOrNothingType,
1190 "desc": "The MIME type of the message body",
1191 },
1192 "cc": {
1193 "type": new SoftListDataType(StringType, True),
1194 "desc": "One or more `Cc:` addresses for the email",
1195 },
1196 "sender": {
1197 "type": StringType,
1198 "desc": "The sender address for the email",
1199 "required": True,
1200 },
1201 "subject": {
1202 "type": StringType,
1203 "desc": "The subject for the email",
1204 },
1205 "to": {
1206 "type": new SoftListDataType(StringType, True),
1207 "desc": "One or more `To:` addresses for the email",
1208 },
1209 };
1210
1211public:
1212
1215
1216};
1217
1219class SmtpSendEmailResponseInfoDataType : public HashDataType {
1220
1221public:
1222protected:
1224 const Fields = {
1225 "code": {
1226 "type": IntType,
1227 "desc": "The return code provided by the SMTP server for the command",
1228 },
1229 "desc": {
1230 "type": StringType,
1231 "desc": "The string description returned by the server",
1232 },
1233 };
1234
1235public:
1236
1239
1240};
1241
1243class SmtpSendEmailResponseDataType : public HashDataType {
1244
1245public:
1248
1249};
1250
1252class SmtpClientDataProviderFactory : public AbstractDataProviderFactory {
1253
1254public:
1255protected:
1257 static Class cls = new Class("SmtpClientDataProvider");
1258
1260 const FactoryInfo = <DataProviderFactoryInfo>{
1261 "name": "smtpclient",
1262 "desc": "SMTP client data provider factory",
1263 "children_can_support_apis": True,
1264 };
1265
1266public:
1267
1269
1271protected:
1272 hash<DataProviderFactoryInfo> getInfoImpl();
1273public:
1274
1275
1277
1279protected:
1280 hash<DataProviderInfo> getProviderInfoImpl();
1281public:
1282
1283
1285protected:
1287public:
1288
1289};
1290};
for backwards-compatibility only
Definition: SmtpClient.qm.dox.h:265
for backwards-compatibility and convenience
Definition: SmtpClient.qm.dox.h:245
constructor(string sender, string subject)
creates a Message object from the arguments given
The SMTP data provider factory.
Definition: SmtpClient.qm.dox.h:1252
hash< DataProviderFactoryInfo > getInfoImpl()
Returns static factory information without provider_info.
hash< DataProviderInfo > getProviderInfoImpl()
Returns static provider information.
static Class cls
Data provider type info.
Definition: SmtpClient.qm.dox.h:1257
Class getClassImpl()
Returns the class for the data provider object.
const FactoryInfo
Factory info.
Definition: SmtpClient.qm.dox.h:1260
Data provider implementing an API for sending email messages.
Definition: SmtpClient.qm.dox.h:969
*string getDesc()
Returns the data provider description.
*list< string > getChildProviderNamesImpl()
Returns a list of child data provider names, if any.
const ProviderInfo
Provider info.
Definition: SmtpClient.qm.dox.h:973
constructor(SmtpClient smtp)
Creates the object from an SmtpClient object.
SmtpClient smtp
The SmtpClient object.
Definition: SmtpClient.qm.dox.h:993
constructor(*hash< auto > options)
Creates the object from constructor options.
const ConstructorOptions
Constructor options.
Definition: SmtpClient.qm.dox.h:983
hash< DataProviderInfo > getStaticInfoImpl()
Returns data provider static info.
*list< hash< DataProviderSummaryInfo > > getChildProviderSummaryInfo()
Return data provider summary info.
*AbstractDataProvider getChildProviderImpl(string name)
Returns the given child provider or NOTHING if the given child is unknown.
connect()
Connect to the server with the connection parameters set in the constructor()
nothing setWarningQueue(int warning_ms, int warning_bs, Queue queue, auto arg, timeout min_ms=1s)
Sets a Queue object to receive socket warnings.
test(bool ns)
sets or disables test mode; no connections are made in test mode
clearStats()
Clears performance statistics.
hash< auto > getInfo()
Returns info about the connection.
setReadTimeout(timeout to)
sets the read timeout
tls(bool n_tls)
sets the TLS/SSL flag
bool isConnected()
return connection status
date getReadTimeoutDate()
returns the read timeout as a relative time value
setInfoLogClosure(*code log)
Set info log closure.
bool tls()
returns the TLS/SSL flag
constructor(string url, *code log, *code dbglog)
creates the SmtpClient object
setDebugLogClosure(*code log)
Set debug log closure.
int getConnectTimeoutMs()
returns the connect timeout as an integer giving milliseconds
int getReadTimeoutMs()
returns the read timeout as an integer giving milliseconds
bool ssl()
returns the SSL connection flag
forceDisconnect()
force disconnect of socket without error
ssl(bool n_ssl)
sets the SSL connection flag
string getTarget()
Returns the connection target string.
disconnect()
disconnect from the server
nothing clearWarningQueue()
Removes any warning Queue object from the Socket.
constructor(string host, softint port, *code log, *code dbglog)
creates the SmtpClient object
date getConnectTimeoutDate()
returns the connect timeout as a relative time value
setConnectTimeout(timeout to)
sets the connect timeout
setEventQueue(Qore::Thread::Queue queue, auto arg, *bool with_data)
Sets a Queue object to receive socket events.
setEventQueue()
Removes any Queue object.
hash< auto > getUsageInfo()
Returns performance statistics for the socket.
bool test()
returns the test mode flag
hash< auto > sendMessage(MailMessage::Message message)
send a Message to the server
setUserPass(string n_user, string n_pass)
sets the username and password for authenticated connections
destructor()
disconnects if connected and destroys the object
Send email data provider.
Definition: SmtpClient.qm.dox.h:1046
const RequestType
Request type.
Definition: SmtpClient.qm.dox.h:1067
string getName()
Returns the data provider name.
auto doRequestImpl(auto req, *hash< auto > request_options)
Makes a request and returns the response.
*AbstractDataProviderType getResponseTypeImpl()
Returns the description of a response message, if this object represents a response message.
*AbstractDataProviderType getRequestTypeImpl()
Returns the description of a successful request message, if any.
const ProviderInfo
Provider info.
Definition: SmtpClient.qm.dox.h:1053
constructor(SmtpClient smtp)
Creates the object from an SMTP connection.
const ProviderSummaryInfo
Provider summary info.
Definition: SmtpClient.qm.dox.h:1062
SmtpClient smtp
SMTP connection.
Definition: SmtpClient.qm.dox.h:1050
hash< DataProviderInfo > getStaticInfoImpl()
Returns data provider static info.
constructor(*hash< auto > options)
Creates the object from constructor options.
const ResponseType
Response type.
Definition: SmtpClient.qm.dox.h:1070
class for SMTP connections; returns an object of class SmtpClient for sending emails
Definition: SmtpClient.qm.dox.h:870
bool supportsPollingApi()
returns True, since this connection type supports the Qore Socket-based polling API
setChildCapabilities()
Sets child data provider capabilities.
constructor(string name, string description, string url, hash< auto > attributes={}, hash< auto > options={})
creates the SmtpConnection connection object
DataProvider::AbstractDataProvider getDataProvider()
returns an SmtpClientDataProvider object for this connection
Qore::AbstractPollOperation startPollConnect()
Called to start a non-blocking polling ping operation on the remote SMTP server.
hash< ConnectionSchemeInfo > getConnectionSchemeInfoImpl()
Returns the ConnectionSchemeInfo hash for this object.
const ConnectionScheme
Connection entry info.
Definition: SmtpClient.qm.dox.h:874
SmtpClient getImpl(bool connect=True, *hash< auto > rtopts)
returns an SmtpClient object
bool hasDataProvider()
returns True, as the connection returns a data provider with the getDataProvider() method
*hash< auto > getRuntimeOptions()
returns runtime options
string getType()
returns "smtp"
Class for SMTP non-blocking / polling pings.
Definition: SmtpClient.qm.dox.h:747
hash< auto > info
Info about the SMTP client.
Definition: SmtpClient.qm.dox.h:782
Socket sock()
The Socket for communication with the server.
bool esmtp
ESMTP flag.
Definition: SmtpClient.qm.dox.h:800
startLogin()
Starts the polling login state.
bool goal_reached
Goal reached flag.
Definition: SmtpClient.qm.dox.h:797
const SPS_LOGIN
Login state.
Definition: SmtpClient.qm.dox.h:772
const SPS_SEND_HELLO
Send hello state.
Definition: SmtpClient.qm.dox.h:757
string state
Current state.
Definition: SmtpClient.qm.dox.h:794
const SPS_CONNECT
Connect state.
Definition: SmtpClient.qm.dox.h:751
const SPS_READ_LOGIN_REPLY
Read login reply.
Definition: SmtpClient.qm.dox.h:775
const SPS_STARTTLS
Send STARTTLS state.
Definition: SmtpClient.qm.dox.h:763
AbstractPollOperation poller
The current internal poll operation.
Definition: SmtpClient.qm.dox.h:788
int starttls_code
Server's STARTTLS code.
Definition: SmtpClient.qm.dox.h:809
const SPS_UPGRADE_TO_TLS
Upgrade socket connection to TLS.
Definition: SmtpClient.qm.dox.h:769
string getState()
Returns the current state.
int hello_reply_code
Server's HELO / EHLO reply code.
Definition: SmtpClient.qm.dox.h:803
int login_code
Server's login code.
Definition: SmtpClient.qm.dox.h:815
const SPS_COMPLETE
Complete state.
Definition: SmtpClient.qm.dox.h:778
Mutex m()
Lock for atomicity.
string hello_reply
Server's HELO / EHLO reply.
Definition: SmtpClient.qm.dox.h:806
*hash< SocketPollInfo > continuePoll()
Returns a hash to be used for I/O polling or NOTHING in case the poll operation is complete.
constructor(SmtpClient smtp)
Creates the object based on the SmtpClient.
const SPS_READ_HELLO_REPLY
Read hello reply state.
Definition: SmtpClient.qm.dox.h:760
string getGoal()
Returns the goal.
const SPS_READ_STARTTLS_REPLY
Read STARTTLS reply.
Definition: SmtpClient.qm.dox.h:766
const SPS_READ_HELLO
Read hello state.
Definition: SmtpClient.qm.dox.h:754
bool getReply(reference< int > rcode, reference< string > rmsg)
Private reads a reply from the SMTP server.
string starttls_reply
Server's STARTTLS reply.
Definition: SmtpClient.qm.dox.h:812
string login_reply
Server's login reply.
Definition: SmtpClient.qm.dox.h:818
bool goalReached()
Returns True when the goal as been reached.
SMTP attachment type.
Definition: SmtpClient.qm.dox.h:1117
const Fields
Field descriptions.
Definition: SmtpClient.qm.dox.h:1122
SMTP send email request type.
Definition: SmtpClient.qm.dox.h:1161
const Fields
Field descriptions.
Definition: SmtpClient.qm.dox.h:1166
SMTP send email response data type.
Definition: SmtpClient.qm.dox.h:1243
SMTP send email response info type.
Definition: SmtpClient.qm.dox.h:1219
const Fields
Field descriptions.
Definition: SmtpClient.qm.dox.h:1224
the SmtpClient namespace holds all public definitions in the SmtpClient module
Definition: SmtpClient.qm.dox.h:228
const DefaultConnectTimeout
30 second connect timeout
Definition: SmtpClient.qm.dox.h:233
const DefaultReadTimeout
15 second read timeout
Definition: SmtpClient.qm.dox.h:230
internal response hash
Definition: SmtpClient.qm.dox.h:236
int code
The response code.
Definition: SmtpClient.qm.dox.h:238
string desc
The response string.
Definition: SmtpClient.qm.dox.h:241