00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 #ifndef OC_SMTP_H
00056 #define OC_SMTP_H
00057
00058 #include "../include/openLogger.h"
00059 #include "ocSocket.h"
00060 #include "ocString.h"
00061
00062
00063 const int reasonableBufSize = 200;
00064
00065
00066 class oc_SMTP: protected ocSocket
00067 {
00068
00069 string crlf;
00070 string sender;
00071
00072 public:
00073
00074 bool isConnected;
00075 ocString response;
00076 string error;
00077
00078 oc_SMTP( string address, int port=25 )
00079 :ocSocket( address.c_str(), port ),crlf("\r\n")
00080 {
00081 isConnected = Response("220");
00082 }
00083
00084 virtual ~oc_SMTP()
00085 {;}
00086
00087
00088
00089 bool Command( string cmdIn )
00090 {
00091 bool bRet = false;
00092 if( cmdIn.length() )
00093 {
00094 int cmdLen = cmdIn.length();
00095
00096
00097 if( cmdLen > 1 && cmdIn[cmdLen-1] == '\n' ) cmdIn.resize(cmdLen-1);
00098 if( cmdLen > 2 && cmdIn[cmdLen-2] == '\r' ) cmdIn.resize(cmdLen-2);
00099
00100 cmdIn += crlf;
00101 cmdLen = cmdIn.length();
00102
00103
00104
00105 bRet = ( cmdLen == Write( cmdIn.c_str(), cmdLen ) );
00106 }
00107 return bRet;
00108 }
00109 bool Response( string expected = "250" )
00110 {
00111 bool bret = false;
00112 error = "";
00113
00114 response = ReadLine();
00115 response.parseInit();
00116 if( response.parse(" ") == expected )
00117 bret=true;
00118 else
00119 error = response;
00120 return bret;
00121 }
00122
00123
00124
00125
00126 bool helo( string domain )
00127 {
00128 bool bret = false;
00129 string command = "HELO ";
00130 command += domain;
00131 if( Command(command.c_str()) )
00132 {
00133 bret = Response();
00134 }
00135 return bret;
00136 }
00137
00138 bool mail_from( string sender )
00139 {
00140 bool bret = false;
00141 string command = "MAIL FROM:<";
00142 command += sender;
00143 command += ">";
00144 if( Command(command.c_str()) )
00145 {
00146 bret = Response();
00147 }
00148 return bret;
00149 }
00150
00151
00152 bool rcpt_to( string recipient )
00153 {
00154 bool bret = false;
00155 string command = "RCPT TO:<";
00156 command += recipient;
00157 command += ">";
00158 if( Command(command.c_str()) )
00159 {
00160 bret = Response();
00161 }
00162 return bret;
00163 }
00164
00165
00166 bool data( void )
00167 {
00168 bool bret = false;
00169 string command = "DATA";
00170 if( Command(command.c_str()) )
00171 {
00172 writelog2("data Command", command );
00173 bret = Response("354");
00174 }
00175 return bret;
00176 }
00177
00178
00179 bool operator () ( string msg )
00180 {
00181 bool bret = false;
00182 string command;
00183
00184 if (msg.length() && msg[0] == '.') command=".";
00185 command += msg;
00186 bret = Command(command.c_str());
00187 return bret;
00188 }
00189
00190
00191 bool end_data( void )
00192 {
00193 bool bret = false;
00194 string command = crlf;
00195 command += ".";
00196 if( Command(command.c_str()) )
00197 {
00198 bret = Response();
00199 }
00200 return bret;
00201 }
00202
00203
00204 bool quit( void )
00205 {
00206 bool bret = false;
00207 string command = "QUIT";
00208 if( Command(command.c_str()) )
00209 {
00210 bret = Response();
00211 }
00212 return bret;
00213 }
00214
00215 bool reset( void )
00216 {
00217 bool bret = false;
00218 string command = "RESET";
00219 if( Command(command.c_str()) )
00220 {
00221 bret = Response();
00222 }
00223 return bret;
00224 }
00225
00226 bool noop( void )
00227 {
00228 bool bret = false;
00229 string command = "NOOP";
00230 if( Command(command.c_str()) )
00231 {
00232 bret = Response();
00233 }
00234 return bret;
00235 }
00236 bool vrfy( string address )
00237 {
00238 bool bret = false;
00239 string command = "VRFY ";
00240 command += address;
00241 if( Command(command.c_str()) )
00242 {
00243 bret = Response();
00244 }
00245 return bret;
00246 }
00247 };
00248
00249 #endif
00250
00251 #ifdef IN_T2_TESTHARNESS
00252
00253
00254
00255
00256 oc_SMTP smtp( "127.0.0.1" );
00257 cout << smtp.response << endl;
00258 if( smtp.isConnected )
00259 {
00260 if( smtp.helo("127.0.0.1") == false )
00261 {
00262 cout << "Hello fail " << smtp.error << endl;
00263 smtp.quit();
00264 return 0;
00265 }
00266 cout << smtp.response << endl;
00267
00268 if( smtp.vrfy("davidmc") )
00269 {
00270 cout << "Good! " << smtp.response << endl;
00271 }
00272 else
00273 {
00274 cout << "Bad! " << smtp.error << endl;
00275 }
00276
00277 if( smtp.mail_from( "davidmc@w3sys.com" ) == false )
00278 {
00279 smtp.quit();
00280 return 0;
00281 }
00282 cout << smtp.response << endl;
00283
00284 if( smtp.rcpt_to( " w3sysdesign@gmail.com" ) == false )
00285 {
00286 smtp.quit();
00287 return 0;
00288 }
00289 cout << smtp.response << endl;
00290 if( smtp.data() == false )
00291 {
00292 cout << "Data fail " << smtp.error << endl;
00293 smtp.quit();
00294 return 0;
00295 }
00296 cout << smtp.response << endl;
00297 smtp("From: davidmc@w3sys.com");
00298 smtp("Sender: davidmc@w3sys.com");
00299 smtp("To: w3sysdesign@gmail.com");
00300 smtp("Subject: Testing SMTP");
00301 smtp("");
00302 smtp("This is a test");
00303 smtp.end_data();
00304 smtp.quit();
00305 }
00306 #endif