smtpthread/SMTPConnection.h

00001 // DOCUMENT:  SMTPConnection-Class
00002 // VERSION:   $Revision: 1.4 $
00003 // DATE:      $Date: 2007-10-26 23:09:19 $
00004 // AUTHOR:    M.Beranek
00005 // COPYRIGHT: M.Beranek
00006 
00007 #ifndef _SMTPCONNECTION_H
00008 #define _SMTPCONNECTION_H
00009 
00010 #include <string>
00011 #include <errno.h>
00012 #include <sys/socket.h>
00013 #include <sys/types.h>
00014 #include <netinet/in.h>
00015 #include <arpa/inet.h>
00016 #include <pthread.h>
00017 #include "Thread.h"
00018 
00019 using namespace std;
00020 
00021 namespace SmtpThread
00022 {
00023 
00024     #define STATE_NOT_CONNECTED  0
00025     #define STATE_CONNECTED      1
00026     #define STATE_HELO           2
00027     #define STATE_MAILFROM       3
00028     #define STATE_RCPTTO         4
00029     #define STATE_DATA           5
00030     #define STATE_DATA_SENDING   6
00031 
00032 
00033     #define SA struct sockaddr
00034     #define SAI struct sockaddr_in
00035 
00036 
00037 
00038     /**
00039      * SMTPConnection-Class.
00040      *
00041      * This class is inherited from the Thread-class.
00042      * This means, you can initialize a SMTPConnection-object,
00043      * setup your SMTPConnection, start threading and
00044      * push a message to the SMTPConnection.
00045      * While the SMTPConnection send the message in its thread,
00046      * you can continue doing other stuff.
00047      *
00048      * SMTPConnection speaks plain SMTP, no ESMTP.
00049      *
00050      */
00051     class SMTPConnection
00052     {
00053 
00054 
00055     public:
00056 
00057         /** Constructor. */
00058         SMTPConnection();
00059 
00060         /** Destructor. */
00061         ~SMTPConnection();
00062 
00063         /**
00064          * Definition of the possible states of an SMTP-connection.
00065          */
00066         enum SMTPState
00067         {
00068             NOT_CONNECTED,
00069             CONNECTED    ,
00070             HELO         ,
00071             MAILFROM     ,
00072             RCPTTO       ,
00073             DATA         ,
00074             DATA_SENDING
00075         };
00076 
00077         /**
00078          * Gets the current state of the connection.
00079          * Since this is an unthreaded class, this will
00080          * either be "SMTPState::NOT_CONNECTED" or
00081          * "SMTPState::CONNECTED".
00082          * @return SMTPConnection::SMTPState
00083          */
00084         SMTPState getSMTPState();
00085 
00086         /**
00087          * Sets the IP of the SMTP-host to connect to.
00088          * @param string host IP-address of SMTP-host.
00089          */
00090         void setSMTPHost( string host );
00091 
00092         /**
00093          * Sets the port-number of the SMTP-host to connect to.
00094          * @param int port Port-number on SMTP-host.
00095          */
00096         void setSMTPPort( int port );
00097 
00098         /**
00099          * Sets the hostname to send in the HELO-command of the SMTP-dialog.
00100          * @param string myname HELO-hostname.
00101          */
00102         void setMyHostname( string myname );
00103 
00104         /**
00105          * Sends a mail via SMTP.
00106          * @param string& from FROM-email
00107          * @param string& to TO-email
00108          * @param  string& msg Message-body of email. Should include additional headers like "Subject" etc.
00109          * @return int status: 0 = success, -1 = error
00110          */
00111         int sendMail( string& from, string& to, string& msg );
00112 
00113         /**
00114          * When connected, disconnects from the SMTP-Server.
00115          */
00116         void disconnect();
00117 
00118 
00119     private:
00120 
00121         // State of the current SMTP-dialog.
00122         SMTPState _smtp;
00123 
00124         // IP-Address of SMTP-Host:
00125         string _host;
00126 
00127         // HELO-Name
00128         string _myname;
00129 
00130         // SMTP-Port:
00131         int _port;
00132 
00133         // Email-From:
00134         string _from;
00135 
00136         // Email-To:
00137         string _to;
00138 
00139         // Email-Body:
00140         string _body;
00141 
00142         // Socket:
00143         int _tcpSocket;
00144 
00145         // Socket-Structure:
00146         SAI * _remoteEnd;
00147 
00148         /** Initializes the socket */
00149         void initSocket();
00150 
00151         /** Closes the socket: */
00152         void closeSocket();
00153 
00154         /**
00155          * Reads a line from the SMTP-Server, returning the SMTP-Code and ignoring
00156          * the rest of the line.
00157          * @return int SMTP-Status-Code
00158          */
00159         int readSMTPState();
00160 
00161         /**
00162          * Sends a command to the SMTP-Server.
00163          * @param const string msg SMTP-Command.
00164          */
00165         int sendSMTP( const string msg );
00166 
00167         /**
00168          * Sends a command to the SMTP-Server and a CRLF, indicating "end-of-command".
00169          * @param const string msg SMTP-Command.
00170          */
00171         int sendSMTP_CRLF( const string msg );
00172 
00173         /**
00174          * Reads a line from the SMTP-Server.
00175          * @return int SMTP-Status
00176          */
00177         int readSMTPLine();
00178 
00179         /**
00180          * Send the stored Email.
00181          * The actual work is done here.
00182          * @return int error-code, 0 = success, -1 on error
00183          */
00184         int sendSMTPMessage();
00185 
00186         /**
00187          * Initializes the socket-structures.
00188          */
00189         void initConnection();
00190 
00191         /**
00192          * Used to store the SMTP-Status-Code, 3 characters for the code and one for
00193          * the terminating nullbyte.
00194          */
00195         char _state[4];
00196 
00197         /**
00198          * Buffer for reading a line of max. 999 characters from the SMTP-Server.
00199          */
00200         char _buf1[ 1000 ];
00201 
00202 
00203         /**
00204          * We are reading just one character from the SMTP-Server.
00205          */
00206         char _buf2;
00207 
00208     };
00209 
00210 }
00211 #endif  /* _SMTPCONNECTION_H */
00212 

Generated on Thu Nov 1 09:51:21 2007 for libSmtpThread by  doxygen 1.5.1