All Classes Namespaces Functions Variables Groups Pages
SmtpClient.qm.dox.h
// -*- mode: c++; indent-tabs-mode: nil -*-
// @file SmtpClient.qm SmtpClient module definition
/* SmtpClient.qm Copyright 2012 - 2015 Qore Technologies, sro
Original Authors: Wolfgang Ritzinger, Marian Bonda, Pavol Potoncok
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
// minimum qore version
// need mime definitions
// need MailMessage classes
// assume local var scope, do not use "$" for vars, members, and method calls
/* Version History
* 2015-11-11 v1.5: David Nichols <david@qore.org>:
+ converted to new-style
* 2014-03-17 v1.4: David Nichols <david@qore.org>:
+ fixed missing username and missing password errors
* 2014-02-04 v1.3: David Nichols <david@qore.org>:
+ added socket instrumention support from Qore 0.8.9
* 2013-11-25 v1.3: David Nichols <david@qore.org>:
+ optimized connection and login code; HELO/EHLO and authorization are performed when connecting only; not before each email
* 2012-11-24 v1.2: David Nichols <david@qore.org>:
+ added support for parsing a full URL in the SmtpClient::constructor(); added protocol support and setting the username / password from the URL
+ use Message::checkSendPossible() to throw a more descriptive exception if the message is incomplete and not ready to be sent
+ 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
* 2012-06-14 v1.1: David Nichols <david@qore.org>:
+ removed the Message and Attachment classes to the MailMessage module to be reused in the Pop3Client module
* 2012-05-21 v1.0: David Nichols <david@qore.org>:
+ updated to a user module, added initial rudimentary ESMTP handling, STARTTLS and quoted-printable encoding support + the bane of all developers: documentation :)
ritzinwo, 20090716
based on:
- http://james.apache.org/server/rfclist/smtp/rfc0821.txt
- http://tools.ietf.org/html/rfc821: initial SMTP protocol spec
- http://tools.ietf.org/html/rfc1521: quoted printable & base 64 transfer encodings
- http://tools.ietf.org/html/rfc2045: mime headers, content types, etc
- http://tools.ietf.org/html/rfc2047: "Q" and "B" encoded words (implemented by the Mime module)
- http://tools.ietf.org/html/rfc2822: message structure, headers, body, etc
*/
namespace SmtpClient {
const DefaultReadTimeout = 15s;
const DefaultConnectTimeout = 30s;
class Message : public MailMessage::Message {
public:
constructor(string sender, string subject) ;
};
class Attachment : public MailMessage::Attachment {
public:
};
class SmtpClient {
public:
private :
Socket sock();
// connect string
string connect;
// ensures exclusive access to the object
Mutex mutex();
bool nosend = False;
// optional info log closure
*code log_info;
// optional debug log closure
*code log_debug;
// tls flag (ie \c "STARTTLS" flag; ie application layer security)
bool tls = False;
// ssl flag (for TLS/SSL connections - ie transport layer instead of application layer security)
bool ssl = False;
// esmtp flag
bool esmtp;
// authentication credentials
*string user;
*string pass;
// logged in flag
bool logged_in = False;
// read timeout in milliseconds
timeout readTimeout = DefaultReadTimeout;
// connect timeout in milliseconds
timeout connectTimeout = DefaultConnectTimeout;
// HELO/EHLO reply
hash hello_reply;
const MaxDebugLine = 2048;
public:
public :
const SmtpPort = 25;
const SmtpsPort = 465;
const EsmtpPort = 587;
const Protocols = (
"smtp": (
"port": SmtpPort,
"ssl": False,
"tls": False,
),
"smtps": (
"port": SmtpsPort,
"ssl": True,
"tls": False,
),
"smtptls": (
"port": SmtpsPort,
"ssl": False,
"tls": True,
),
"esmtp": (
"port": EsmtpPort,
"ssl": False,
"tls": False,
),
"esmtptls": (
"port": EsmtpPort,
"ssl": False,
"tls": True,
),
);
public:
constructor(string host, softint port, *code log, *code dbglog);
constructor(string url, *code log, *code dbglog);
destructor();
tls(bool n_tls);
bool tls();
ssl(bool n_ssl);
bool ssl();
setUserPass(string n_user, string n_pass);
test(bool ns);
bool test();
connect();
bool isConnected();
disconnect();
setReadTimeout(timeout to);
int getReadTimeoutMs();
date getReadTimeoutDate();
setConnectTimeout(timeout to);
int getConnectTimeoutMs();
date getConnectTimeoutDate();
hash sendMessage(MailMessage::Message message);
forceDisconnect();
nothing clearWarningQueue();
nothing setWarningQueue(int warning_ms, int warning_bs, Queue queue, any arg, timeout min_ms = 1s);
hash getUsageInfo();
clearStats();
// don't reimplement this method; fix/enhance it in the module
final private disconnectIntern();
private log(string msg);
private logDbg(string msg);
private connectIntern();
private loginIntern();
// send data over the socket
private sendDataIntern(data str);
// send data and log in the debug log if set
private sendData(string str);
// send data and log in the debug log if set
private sendData(binary b);
// send a command over the socket and return the response as a hash
// don't reimplement this method; fix/enhance it in the module
final private hash sendCommand(string str);
// read a line from the socket (terminated with \n)
private string readLine(timeout to);
// sends the message header (without body & attachments) to the SMTP server
// don't reimplement this method; fix/enhance it in the module
final private hash sendMessageInfoIntern(MailMessage::Message message);
private forceDisconnectIntern();
};
};
date
date date(date dt)
SmtpClient::SmtpClient::getConnectTimeoutDate
date getConnectTimeoutDate()
returns the connect timeout as a relative time value
SmtpClient::SmtpClient::SmtpPort
const SmtpPort
default SMTP port
Definition: SmtpClient.qm.dox.h:238
SmtpClient::SmtpClient
the class that&#39;s used to communicate with an SMTP server and supports optional TLS/SSL encryption ...
Definition: SmtpClient.qm.dox.h:185
SmtpClient::SmtpClient::getReadTimeoutMs
int getReadTimeoutMs()
returns the read timeout as an integer giving milliseconds
MailMessage::Message
True
const True
SmtpClient::SmtpClient::setConnectTimeout
setConnectTimeout(timeout to)
sets the connect timeout
SmtpClient::SmtpClient::setWarningQueue
nothing setWarningQueue(int warning_ms, int warning_bs, Queue queue, any arg, timeout min_ms=1s)
Sets a Queue object to receive socket warnings.
SmtpClient::SmtpClient::tls
bool tls()
returns the TLS/SSL flag
binary
binary binary()
SmtpClient::DefaultReadTimeout
const DefaultReadTimeout
15 second read timeout
Definition: SmtpClient.qm.dox.h:154
SmtpClient::DefaultConnectTimeout
const DefaultConnectTimeout
30 second connect timeout
Definition: SmtpClient.qm.dox.h:157
SmtpClient::SmtpClient::destructor
destructor()
disconnects if connected and destroys the object
False
const False
SmtpClient::Attachment
for backwards-compatibility only
Definition: SmtpClient.qm.dox.h:176
SmtpClient::SmtpClient::constructor
constructor(string host, softint port, *code log, *code dbglog)
creates the SmtpClient object
SmtpClient::SmtpClient::forceDisconnect
forceDisconnect()
force disconnect of socket without error
MailMessage::Attachment
SmtpClient::SmtpClient::clearWarningQueue
nothing clearWarningQueue()
Removes any warning Queue object from the Socket.
SmtpClient::SmtpClient::ssl
bool ssl()
returns the SSL connection flag
SmtpClient::SmtpClient::EsmtpPort
const EsmtpPort
default ESMTP port
Definition: SmtpClient.qm.dox.h:244
SmtpClient::SmtpClient::setUserPass
setUserPass(string n_user, string n_pass)
sets the username and password for authenticated connections
SmtpClient::SmtpClient::getReadTimeoutDate
date getReadTimeoutDate()
returns the read timeout as a relative time value
SmtpClient::Message
for backwards-compatibility and convenience
Definition: SmtpClient.qm.dox.h:160
SmtpClient::SmtpClient::disconnect
disconnect()
disconnect from the server
SmtpClient::SmtpClient::isConnected
bool isConnected()
return connection status
SmtpClient::SmtpClient::test
bool test()
returns the test mode flag
SmtpClient::Message::constructor
constructor(string sender, string subject)
creates a Message object from the arguments given; this variant of the constructor is designed to be ...
SmtpClient::SmtpClient::sendMessage
hash sendMessage(MailMessage::Message message)
send a Message to the server
SmtpClient::SmtpClient::connect
connect()
Connect to the server with the connection parameters set in the constructor()
SmtpClient::SmtpClient::SmtpsPort
const SmtpsPort
default SMTPS port; note that this port is currently assigned to source-specific multicast audio/vide...
Definition: SmtpClient.qm.dox.h:241
SmtpClient::SmtpClient::Protocols
const Protocols
procotol config
Definition: SmtpClient.qm.dox.h:247
hash
hash hash(object obj)
SmtpClient::SmtpClient::getConnectTimeoutMs
int getConnectTimeoutMs()
returns the connect timeout as an integer giving milliseconds
SmtpClient::SmtpClient::setReadTimeout
setReadTimeout(timeout to)
sets the read timeout
SmtpClient::SmtpClient::clearStats
clearStats()
Clears performance statistics.
SmtpClient::SmtpClient::getUsageInfo
hash getUsageInfo()
Returns performance statistics for the socket.