Qore SmtpClient Module Reference  1.5
 All Classes Namespaces Functions Variables Groups Pages
SmtpClient.qm.dox.h
1 // -*- mode: c++; indent-tabs-mode: nil -*-
2 // @file SmtpClient.qm SmtpClient module definition
3 
4 /* SmtpClient.qm Copyright 2012 - 2015 Qore Technologies, sro
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 // assume local var scope, do not use "$" for vars, members, and method calls
34 
35 
36 /* Version History
37  * 2015-11-11 v1.5: David Nichols <david@qore.org>:
38  + converted to new-style
39 
40  * 2014-03-17 v1.4: David Nichols <david@qore.org>:
41  + fixed missing username and missing password errors
42 
43  * 2014-02-04 v1.3: David Nichols <david@qore.org>:
44  + added socket instrumention support from Qore 0.8.9
45 
46  * 2013-11-25 v1.3: David Nichols <david@qore.org>:
47  + optimized connection and login code; HELO/EHLO and authorization are performed when connecting only; not before each email
48 
49  * 2012-11-24 v1.2: David Nichols <david@qore.org>:
50  + added support for parsing a full URL in the SmtpClient::constructor(); added protocol support and setting the username / password from the URL
51  + use Message::checkSendPossible() to throw a more descriptive exception if the message is incomplete and not ready to be sent
52  + implemented support for automatically detecting if the server accepts the STARTTLS command and, if so, automatically setting the STARTTLS flag if it's not already set
53 
54  * 2012-06-14 v1.1: David Nichols <david@qore.org>:
55  + removed the Message and Attachment classes to the MailMessage module to be reused in the Pop3Client module
56 
57  * 2012-05-21 v1.0: David Nichols <david@qore.org>:
58  + updated to a user module, added initial rudimentary ESMTP handling, STARTTLS and quoted-printable encoding support + the bane of all developers: documentation :)
59 
60  ritzinwo, 20090716
61 
62  based on:
63  - http://james.apache.org/server/rfclist/smtp/rfc0821.txt
64  - http://tools.ietf.org/html/rfc821: initial SMTP protocol spec
65  - http://tools.ietf.org/html/rfc1521: quoted printable & base 64 transfer encodings
66  - http://tools.ietf.org/html/rfc2045: mime headers, content types, etc
67  - http://tools.ietf.org/html/rfc2047: "Q" and "B" encoded words (implemented by the Mime module)
68  - http://tools.ietf.org/html/rfc2822: message structure, headers, body, etc
69 */
70 
144 
152 namespace SmtpClient {
154  const DefaultReadTimeout = 15s;
155 
158 
161 
162 public:
164 
171  constructor(string sender, string subject) ;
172 
173  };
174 
177 
178 public:
179  };
180 
182 
185  class SmtpClient {
186 
187 public:
189  private :
190  Socket sock();
191 
192  // connect string
193  string connect;
194 
195  // ensures exclusive access to the object
196  Mutex mutex();
197 
198  bool nosend = False;
199 
200  // optional info log closure
201  *code log_info;
202 
203  // optional debug log closure
204  *code log_debug;
205 
206  // tls flag (ie \c "STARTTLS" flag; ie application layer security)
207  bool tls = False;
208 
209  // ssl flag (for TLS/SSL connections - ie transport layer instead of application layer security)
210  bool ssl = False;
211 
212  // esmtp flag
213  bool esmtp;
214 
215  // authentication credentials
216  *string user;
217  *string pass;
218 
219  // logged in flag
220  bool logged_in = False;
221 
222  // read timeout in milliseconds
223  timeout readTimeout = DefaultReadTimeout;
224 
225  // connect timeout in milliseconds
226  timeout connectTimeout = DefaultConnectTimeout;
227 
228  // HELO/EHLO reply
229  hash hello_reply;
230 
231  const MaxDebugLine = 2048;
232 
233 public:
235 
236  public :
238  const SmtpPort = 25;
239 
241  const SmtpsPort = 465;
242 
244  const EsmtpPort = 587;
245 
247  const Protocols = (
248  "smtp": (
249  "port": SmtpPort,
250  "ssl": False,
251  "tls": False,
252  ),
253  "smtps": (
254  "port": SmtpsPort,
255  "ssl": True,
256  "tls": False,
257  ),
258  "smtptls": (
259  "port": SmtpsPort,
260  "ssl": False,
261  "tls": True,
262  ),
263  "esmtp": (
264  "port": EsmtpPort,
265  "ssl": False,
266  "tls": False,
267  ),
268  "esmtptls": (
269  "port": EsmtpPort,
270  "ssl": False,
271  "tls": True,
272  ),
273  );
274 
275 public:
276 
278 
283  constructor(string host, softint port, *code log, *code dbglog);
284 
285 
287 
300  constructor(string url, *code log, *code dbglog);
301 
302 
304 
306  destructor();
307 
308 
310 
312  tls(bool n_tls);
313 
314 
316  bool tls();
317 
318 
320 
322  ssl(bool n_ssl);
323 
324 
326  bool ssl();
327 
328 
330 
337  setUserPass(string n_user, string n_pass);
338 
339 
341  test(bool ns);
342 
343 
345  bool test();
346 
347 
349 
353  connect();
354 
355 
357  bool isConnected();
358 
359 
361 
363  disconnect();
364 
365 
367  setReadTimeout(timeout to);
368 
369 
371  int getReadTimeoutMs();
372 
373 
376 
377 
379  setConnectTimeout(timeout to);
380 
381 
383  int getConnectTimeoutMs();
384 
385 
388 
389 
391 
404 
405 
407 
409  forceDisconnect();
410 
411 
413 
422  nothing clearWarningQueue();
423 
424 
426 
456  nothing setWarningQueue(int warning_ms, int warning_bs, Queue queue, any arg, timeout min_ms = 1s);
457 
458 
460 
478  hash getUsageInfo();
479 
480 
482 
491  clearStats();
492 
493 
495  // don't reimplement this method; fix/enhance it in the module
496  final private disconnectIntern();
497 
498 
499  private log(string msg);
500 
501 
502  private logDbg(string msg);
503 
504 
505  private connectIntern();
506 
507 
508  private loginIntern();
509 
510 
511  // send data over the socket
512  private sendDataIntern(data str);
513 
514 
515  // send data and log in the debug log if set
516  private sendData(string str);
517 
518 
519  // send data and log in the debug log if set
520  private sendData(binary b);
521 
522 
523  // send a command over the socket and return the response as a hash
524  // don't reimplement this method; fix/enhance it in the module
525  final private hash sendCommand(string str);
526 
527 
528  // read a line from the socket (terminated with \n)
529  private string readLine(timeout to);
530 
531 
532  // sends the message header (without body & attachments) to the SMTP server
533  // don't reimplement this method; fix/enhance it in the module
534  final private hash sendMessageInfoIntern(MailMessage::Message message);
535 
536 
537  private forceDisconnectIntern();
538 
540  };
541 };
date date(date dt)
date getConnectTimeoutDate()
returns the connect timeout as a relative time value
const SmtpPort
default SMTP port
Definition: SmtpClient.qm.dox.h:238
the class that&#39;s used to communicate with an SMTP server and supports optional TLS/SSL encryption ...
Definition: SmtpClient.qm.dox.h:185
int getReadTimeoutMs()
returns the read timeout as an integer giving milliseconds
const True
setConnectTimeout(timeout to)
sets the connect timeout
nothing setWarningQueue(int warning_ms, int warning_bs, Queue queue, any arg, timeout min_ms=1s)
Sets a Queue object to receive socket warnings.
bool tls()
returns the TLS/SSL flag
binary binary()
const DefaultReadTimeout
15 second read timeout
Definition: SmtpClient.qm.dox.h:154
const DefaultConnectTimeout
30 second connect timeout
Definition: SmtpClient.qm.dox.h:157
destructor()
disconnects if connected and destroys the object
const False
for backwards-compatibility only
Definition: SmtpClient.qm.dox.h:176
constructor(string host, softint port, *code log, *code dbglog)
creates the SmtpClient object
forceDisconnect()
force disconnect of socket without error
nothing clearWarningQueue()
Removes any warning Queue object from the Socket.
bool ssl()
returns the SSL connection flag
const EsmtpPort
default ESMTP port
Definition: SmtpClient.qm.dox.h:244
setUserPass(string n_user, string n_pass)
sets the username and password for authenticated connections
date getReadTimeoutDate()
returns the read timeout as a relative time value
for backwards-compatibility and convenience
Definition: SmtpClient.qm.dox.h:160
disconnect()
disconnect from the server
bool isConnected()
return connection status
bool test()
returns the test mode flag
constructor(string sender, string subject)
creates a Message object from the arguments given; this variant of the constructor is designed to be ...
hash sendMessage(MailMessage::Message message)
send a Message to the server
connect()
Connect to the server with the connection parameters set in the constructor()
const SmtpsPort
default SMTPS port; note that this port is currently assigned to source-specific multicast audio/vide...
Definition: SmtpClient.qm.dox.h:241
const Protocols
procotol config
Definition: SmtpClient.qm.dox.h:247
hash hash(object obj)
int getConnectTimeoutMs()
returns the connect timeout as an integer giving milliseconds
setReadTimeout(timeout to)
sets the read timeout
clearStats()
Clears performance statistics.
hash getUsageInfo()
Returns performance statistics for the socket.