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}
37
222
229namespace SmtpClient {
232
235
237public hashdecl SmtpResponseInfo {
239 int code;
240
242 string desc;
243};
244
247
248public:
250
261 constructor(string sender, string subject) ;
262
263};
264
267
268public:
269};
270
272
276
277public:
279protected:
280 Socket sock();
281
282 // connect string
283 string connect;
284
285 // ensures exclusive access to the object
286 Mutex mutex();
287
288 bool nosend = False;
289
290 // optional info log closure
291 *code log_info;
292
293 // optional debug log closure
294 *code log_debug;
295
296 // tls flag (ie \c "STARTTLS" flag; ie application layer security)
297 bool tls = False;
298
299 // ssl flag (for TLS/SSL connections - ie transport layer instead of application layer security)
300 bool ssl = False;
301
302 // esmtp flag
303 bool esmtp;
304
305 // authentication credentials
306 *string user;
307 *string pass;
308
309 // logged in flag
310 bool logged_in = False;
311
312 // read timeout in milliseconds
313 timeout readTimeout = DefaultReadTimeout;
314
315 // connect timeout in milliseconds
316 timeout connectTimeout = DefaultConnectTimeout;
317
318 // HELO/EHLO reply
319 hash<auto> hello_reply;
320
321 const MaxDebugLine = 2048;
322
323public:
325
327 const SmtpPort = 25;
328
330
335 const SmtpsPort = 465;
336
338 const EsmtpPort = 587;
339
341 const Protocols = {
342 "smtp": (
343 "port": SmtpPort,
344 "ssl": False,
345 "tls": False,
346 ),
347 "smtps": (
348 "port": SmtpsPort,
349 "ssl": True,
350 "tls": False,
351 ),
352 "smtptls": (
353 "port": SmtpsPort,
354 "ssl": False,
355 "tls": True,
356 ),
357 "esmtp": (
358 "port": EsmtpPort,
359 "ssl": False,
360 "tls": False,
361 ),
362 "esmtps": (
363 "port": SmtpsPort,
364 "ssl": True,
365 "tls": False,
366 ),
367 "esmtptls": (
368 "port": EsmtpPort,
369 "ssl": False,
370 "tls": True,
371 ),
372 };
373
375
382 constructor(string host, softint port, *code log, *code dbglog);
383
384
386
406 constructor(string url, *code log, *code dbglog);
407
408
410
413
414
416
423 hash<auto> getInfo();
424
425
428
429
432
433
435
437 string getTarget();
438
439
441
445 tls(bool n_tls);
446
447
449 bool tls();
450
451
453
457 ssl(bool n_ssl);
458
459
461 bool ssl();
462
463
465
472 setUserPass(string n_user, string n_pass);
473
474
476 test(bool ns);
477
478
480 bool test();
481
482
484
489
490
493
494
496
499
500
502 setReadTimeout(timeout to);
503
504
507
508
511
512
514 setConnectTimeout(timeout to);
515
516
519
520
523
524
526
540
541
543
546
547
549
559
560
562
598 nothing setWarningQueue(int warning_ms, int warning_bs, Queue queue, auto arg, timeout min_ms = 1s);
599
600
602
623 hash<auto> getUsageInfo();
624
625
627
637
638
640
656 setEventQueue(Qore::Thread::Queue queue, auto arg, *bool with_data);
657
658
660
673
674
676 // don't reimplement this method; fix/enhance it in the module
677protected:
678 final disconnectIntern();
679public:
680
681
682protected:
683 log(string msg);
684public:
685
686
687protected:
688 logDbg(string msg);
689public:
690
691
692protected:
693 connectIntern();
694public:
695
696
697protected:
698 loginIntern();
699public:
700
701
702 // send data over the socket
703protected:
704 sendDataIntern(data str);
705public:
706
707
708 // send data and log in the debug log if set
709protected:
710 sendData(string str);
711public:
712
713
714 // send data and log in the debug log if set
715protected:
716 sendData(binary b);
717public:
718
719
720 // send a command over the socket and return the response as a hash
721 // don't reimplement this method; fix/enhance it in the module
722protected:
723 final hash<SmtpResponseInfo> sendCommand(string str);
724public:
725
726
727 // read a line from the socket (terminated with \n)
728protected:
729 string readLine(timeout to);
730public:
731
732
733 // sends the message header (without body & attachments) to the SMTP server
734 // don't reimplement this method; fix/enhance it in the module
735protected:
736 final hash<auto> sendMessageInfoIntern(MailMessage::Message message);
737public:
738
739
740protected:
741 forceDisconnectIntern();
742public:
743
745};
746
749
750public:
752 const SPS_CONNECT = "connect";
753
755 const SPS_READ_HELLO = "read-hello";
756
758 const SPS_SEND_HELLO = "send-hello";
759
761 const SPS_READ_HELLO_REPLY = "read-hello-reply";
762
764 const SPS_STARTTLS = "starttls";
765
767 const SPS_READ_STARTTLS_REPLY = "read-starttls-reply";
768
770 const SPS_UPGRADE_TO_TLS = "upgrade-tls";
771
773 const SPS_LOGIN = "login";
774
776 const SPS_READ_LOGIN_REPLY = "read-login-reply";
777
779 const SPS_COMPLETE = "complete";
780
781protected:
783 hash<auto> info;
784
786 Socket sock();
787
789 AbstractPollOperation poller;
790
792 Mutex m();
793
795 string state;
796
798 bool goal_reached = False;
799
801 bool esmtp = False;
802
805
808
811
814
817
820
821public:
822
825
826
828
830 string getGoal();
831
832
834
836 string getState();
837
838
840
843
844
846 *hash<SocketPollInfo> continuePoll();
847
848
850protected:
852public:
853
854
856
858protected:
859 bool getReply(reference<int> rcode, reference<string> rmsg);
860public:
861
862};
863
865
872
873public:
875 const ConnectionScheme = <ConnectionSchemeInfo>{
876 "cls": Class::forName("SmtpConnection"),
877 };
878
880
890 constructor(string name, string description, string url, hash<auto> attributes = {}, hash<auto> options = {})
891 ;
892
893
895 string getType();
896
897
899
903 *hash<auto> getRuntimeOptions();
904
905
907
911 DataProvider::AbstractDataProvider getDataProvider();
912
913
915
920
921
923
928
929
931
939
940
942protected:
944public:
945
946
948
955protected:
956 SmtpClient getImpl(bool connect = True, *hash<auto> rtopts);
957public:
958
959
961protected:
962 hash<ConnectionSchemeInfo> getConnectionSchemeInfoImpl();
963public:
964
965};
966
968
970class SmtpClientDataProvider : public DataProvider::AbstractDataProvider {
971
972public:
974 const ProviderInfo = <DataProviderInfo>{
975 "type": "SmtpClientDataProvider",
976 "supports_children": True,
977 "constructor_options": ConstructorOptions,
978 "children_can_support_apis": True,
979 "children_can_support_records": False,
980 "children_can_support_observers": False,
981 };
982
985 "url": <DataProviderOptionInfo>{
986 "type": AbstractDataProviderType::get(StringType),
987 "desc": "A URL for an SMTP server",
988 "required": True,
989 },
990 };
991
992protected:
995
996 const ChildMap = {
997 "send-email": Class::forName("SmtpClient::SmtpClientSendEmailDataProvider"),
998 };
999
1000public:
1001
1003 constructor(*hash<auto> options);
1004
1005
1008
1009
1010 string getName();
1011
1012
1014 *string getDesc();
1015
1016
1018 *list<hash<DataProviderSummaryInfo>> getChildProviderSummaryInfo();
1019
1020
1022
1024protected:
1026public:
1027
1028
1030
1034protected:
1035 *AbstractDataProvider getChildProviderImpl(string name);
1036public:
1037
1038
1040protected:
1041 hash<DataProviderInfo> getStaticInfoImpl();
1042public:
1043
1044};
1045
1047class SmtpClientSendEmailDataProvider : public AbstractDataProvider {
1048
1049public:
1052
1054 const ProviderInfo = <DataProviderInfo>{
1055 "name": "send-email",
1056 "desc": "SMTP send email data provider",
1057 "type": "SmtpClientSendEmailDataProvider",
1058 "constructor_options": SmtpClientDataProvider::ConstructorOptions,
1059 "supports_request": True,
1060 };
1061
1063 const ProviderSummaryInfo = cast<hash<DataProviderSummaryInfo>>(ProviderInfo{
1064 AbstractDataProvider::DataProviderSummaryInfoKeys
1065 });
1066
1069
1072
1074 constructor(*hash<auto> options);
1075
1076
1079
1080
1082 string getName();
1083
1084
1086
1091protected:
1092 auto doRequestImpl(auto req, *hash<auto> request_options);
1093public:
1094
1095
1097
1099protected:
1100 *AbstractDataProviderType getRequestTypeImpl();
1101public:
1102
1103
1105
1107protected:
1108 *AbstractDataProviderType getResponseTypeImpl();
1109public:
1110
1111
1113 hash<DataProviderInfo> getStaticInfoImpl();
1114
1115};
1116
1118class SmtpSendEmailAttachmentDataType : public HashDataType {
1119
1120public:
1121protected:
1123 const Fields = {
1124 "data": {
1125 "type": DataType,
1126 "desc": "The data for the attachment",
1127 "required": True,
1128 },
1129 "encoding": {
1130 "type": StringType,
1131 "desc": "The encoding to use for the attachment:\n"
1132 "- `default`: `quoted-printable` for string data, `base64` for binary data\n"
1133 "- `none`: no content encoding (not recommended)\n"
1134 "- `quoted-printable`: quoted printable encoding \n"
1135 "- `base64`: base64 encoding",
1136 "default_value": "default",
1137 },
1138 "hdr": {
1139 "type": HashOrNothingType,
1140 "desc": "Optional headers to send with the attachment",
1141 },
1142 "mime-type": {
1143 "type": StringType,
1144 "desc": "The MIME type of the attachment",
1145 "required": True,
1146 },
1147 "name": {
1148 "type": StringType,
1149 "desc": "The name or filename of the attachment",
1150 "required": True,
1151 },
1152 };
1153
1154public:
1155
1158
1159};
1160
1162class SmtpSendEmailRequestDataType : public HashDataType {
1163
1164public:
1165protected:
1167 const Fields = {
1168 "attachments": {
1169 "type": new SoftListDataType(new SmtpSendEmailAttachmentDataType(), True),
1170 "desc": "Any attachments for the message",
1171 },
1172 "bcc": {
1173 "type": new SoftListDataType(StringType, True),
1174 "desc": "One or more `Bcc:` addresses for the email",
1175 },
1176 "body": {
1177 "type": DataType,
1178 "desc": "The message body",
1179 },
1180 "body-encoding": {
1181 "type": StringType,
1182 "desc": "The encoding to use for the attachment:\n"
1183 "- `default`: `quoted-printable` for string data, `base64` for binary data\n"
1184 "- `none`: no content encoding (not recommended)\n"
1185 "- `quoted-printable`: quoted printable encoding \n"
1186 "- `base64`: base64 encoding",
1187 "default_value": "default",
1188 },
1189 "body-mime-type": {
1190 "type": StringOrNothingType,
1191 "desc": "The MIME type of the message body",
1192 },
1193 "cc": {
1194 "type": new SoftListDataType(StringType, True),
1195 "desc": "One or more `Cc:` addresses for the email",
1196 },
1197 "sender": {
1198 "type": StringType,
1199 "desc": "The sender address for the email",
1200 "required": True,
1201 },
1202 "subject": {
1203 "type": StringType,
1204 "desc": "The subject for the email",
1205 },
1206 "to": {
1207 "type": new SoftListDataType(StringType, True),
1208 "desc": "One or more `To:` addresses for the email",
1209 },
1210 };
1211
1212public:
1213
1216
1217};
1218
1220class SmtpSendEmailResponseInfoDataType : public HashDataType {
1221
1222public:
1223protected:
1225 const Fields = {
1226 "code": {
1227 "type": IntType,
1228 "desc": "The return code provided by the SMTP server for the command",
1229 },
1230 "desc": {
1231 "type": StringType,
1232 "desc": "The string description returned by the server",
1233 },
1234 };
1235
1236public:
1237
1240
1241};
1242
1244class SmtpSendEmailResponseDataType : public HashDataType {
1245
1246public:
1249
1250};
1251
1253class SmtpClientDataProviderFactory : public AbstractDataProviderFactory {
1254
1255public:
1256protected:
1258 static Class cls = new Class("SmtpClientDataProvider");
1259
1261 const FactoryInfo = <DataProviderFactoryInfo>{
1262 "name": "smtpclient",
1263 "desc": "SMTP client data provider factory",
1264 "children_can_support_apis": True,
1265 };
1266
1267public:
1268
1270
1272protected:
1273 hash<DataProviderFactoryInfo> getInfoImpl();
1274public:
1275
1276
1278
1280protected:
1281 hash<DataProviderInfo> getProviderInfoImpl();
1282public:
1283
1284
1286protected:
1288public:
1289
1290};
1291};
for backwards-compatibility only
Definition: SmtpClient.qm.dox.h:266
for backwards-compatibility and convenience
Definition: SmtpClient.qm.dox.h:246
constructor(string sender, string subject)
creates a Message object from the arguments given
The SMTP data provider factory.
Definition: SmtpClient.qm.dox.h:1253
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:1258
Class getClassImpl()
Returns the class for the data provider object.
const FactoryInfo
Factory info.
Definition: SmtpClient.qm.dox.h:1261
Data provider implementing an API for sending email messages.
Definition: SmtpClient.qm.dox.h:970
*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:974
constructor(SmtpClient smtp)
Creates the object from an SmtpClient object.
SmtpClient smtp
The SmtpClient object.
Definition: SmtpClient.qm.dox.h:994
constructor(*hash< auto > options)
Creates the object from constructor options.
const ConstructorOptions
Constructor options.
Definition: SmtpClient.qm.dox.h:984
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:1047
const RequestType
Request type.
Definition: SmtpClient.qm.dox.h:1068
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:1054
constructor(SmtpClient smtp)
Creates the object from an SMTP connection.
const ProviderSummaryInfo
Provider summary info.
Definition: SmtpClient.qm.dox.h:1063
SmtpClient smtp
SMTP connection.
Definition: SmtpClient.qm.dox.h:1051
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:1071
class for SMTP connections; returns an object of class SmtpClient for sending emails
Definition: SmtpClient.qm.dox.h:871
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:875
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:748
hash< auto > info
Info about the SMTP client.
Definition: SmtpClient.qm.dox.h:783
Socket sock()
The Socket for communication with the server.
bool esmtp
ESMTP flag.
Definition: SmtpClient.qm.dox.h:801
startLogin()
Starts the polling login state.
bool goal_reached
Goal reached flag.
Definition: SmtpClient.qm.dox.h:798
const SPS_LOGIN
Login state.
Definition: SmtpClient.qm.dox.h:773
const SPS_SEND_HELLO
Send hello state.
Definition: SmtpClient.qm.dox.h:758
string state
Current state.
Definition: SmtpClient.qm.dox.h:795
const SPS_CONNECT
Connect state.
Definition: SmtpClient.qm.dox.h:752
const SPS_READ_LOGIN_REPLY
Read login reply.
Definition: SmtpClient.qm.dox.h:776
const SPS_STARTTLS
Send STARTTLS state.
Definition: SmtpClient.qm.dox.h:764
AbstractPollOperation poller
The current internal poll operation.
Definition: SmtpClient.qm.dox.h:789
int starttls_code
Server's STARTTLS code.
Definition: SmtpClient.qm.dox.h:810
const SPS_UPGRADE_TO_TLS
Upgrade socket connection to TLS.
Definition: SmtpClient.qm.dox.h:770
string getState()
Returns the current state.
int hello_reply_code
Server's HELO / EHLO reply code.
Definition: SmtpClient.qm.dox.h:804
int login_code
Server's login code.
Definition: SmtpClient.qm.dox.h:816
const SPS_COMPLETE
Complete state.
Definition: SmtpClient.qm.dox.h:779
Mutex m()
Lock for atomicity.
string hello_reply
Server's HELO / EHLO reply.
Definition: SmtpClient.qm.dox.h:807
*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:761
string getGoal()
Returns the goal.
const SPS_READ_STARTTLS_REPLY
Read STARTTLS reply.
Definition: SmtpClient.qm.dox.h:767
const SPS_READ_HELLO
Read hello state.
Definition: SmtpClient.qm.dox.h:755
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:813
string login_reply
Server's login reply.
Definition: SmtpClient.qm.dox.h:819
bool goalReached()
Returns True when the goal as been reached.
SMTP attachment type.
Definition: SmtpClient.qm.dox.h:1118
const Fields
Field descriptions.
Definition: SmtpClient.qm.dox.h:1123
SMTP send email request type.
Definition: SmtpClient.qm.dox.h:1162
const Fields
Field descriptions.
Definition: SmtpClient.qm.dox.h:1167
SMTP send email response data type.
Definition: SmtpClient.qm.dox.h:1244
SMTP send email response info type.
Definition: SmtpClient.qm.dox.h:1220
const Fields
Field descriptions.
Definition: SmtpClient.qm.dox.h:1225
the SmtpClient namespace holds all public definitions in the SmtpClient module
Definition: SmtpClient.qm.dox.h:229
const DefaultConnectTimeout
30 second connect timeout
Definition: SmtpClient.qm.dox.h:234
const DefaultReadTimeout
15 second read timeout
Definition: SmtpClient.qm.dox.h:231
internal response hash
Definition: SmtpClient.qm.dox.h:237
int code
The response code.
Definition: SmtpClient.qm.dox.h:239
string desc
The response string.
Definition: SmtpClient.qm.dox.h:242