00001
00002
00003
00004
00005
00006
00007
00008
00009 #include <fstream>
00010 #include <string>
00011 #include "ocMail.h"
00012 #include "ocString.h"
00013 #include "ocCoder.h"
00014
00015 #include "openLogger.h"
00016
00017
00018
00019 void ocSendMail::getTime( void )
00020 {
00021 time_t lt;
00022 time(<);
00023 localtime_r(<,&tm_time);
00024 }
00025 string ocSendMail::makeBoundary( void )
00026 {
00027 ocString ret = "=_-_-Open_Mail_Boundary_-";
00028 getTime();
00029 ret.append( tm_time.tm_year );
00030 ret.append( tm_time.tm_mon );
00031 ret.append( tm_time.tm_mday );
00032 ret += "_";
00033 ret.append( tm_time.tm_hour );
00034 ret.append( tm_time.tm_min );
00035 ret.append( tm_time.tm_sec );
00036 while( m_strBody.find( ret ) != string::npos ) ret += "_=";
00037 m_beginBoundary = "--";
00038 m_beginBoundary += ret;
00039 m_endBoundary = m_beginBoundary;
00040 m_endBoundary += "--";
00041 return ret;
00042 }
00043
00044 bool ocSendMail::loadAddresses ( const char * path )
00045 {
00046 bool bRet = false;
00047 ifstream addressFile( path );
00048 if( addressFile )
00049 {
00050 char addressBuffer[512];
00051 addressFile.getline( addressBuffer, sizeof(addressBuffer) );
00052 while( addressFile.rdstate() == ios::goodbit )
00053 {
00054 string valuePair = addressBuffer;
00055 string::size_type pos = valuePair.find("=");
00056 if( pos != string::npos )
00057 {
00058 string name = valuePair.substr(0, pos);
00059 string value = valuePair.substr( pos+1 );
00060 m_addresses[name] = value;
00061 }
00062 addressFile.getline( addressBuffer, sizeof(addressBuffer) );
00063 }
00064 bRet = true;
00065 }
00066 return bRet;
00067 }
00068
00069 const char * ocSendMail::getAddress( const char * name )
00070 {
00071 const char * retval = NULL;
00072 strStrMap::iterator pos = m_addresses.find(name);
00073 if( pos != m_addresses.end() )
00074 {
00075 retval = pos->second.c_str();
00076 }
00077 return retval;
00078 }
00079
00080 ocSendMail::ocSendMail( const char * sendMailPath )
00081 {
00082 m_strExePath = sendMailPath;
00083 }
00084
00085 ocSendMail::~ocSendMail()
00086 {
00087 ;
00088 }
00089
00090 bool ocSendMail::addHeader ( const char * header, const char * value )
00091 {
00092 bool bRet = false;
00093 if( header && strlen(header) &&
00094 value && strlen(value) )
00095 {
00096 m_strHeaders += header;
00097 m_strHeaders += ": ";
00098 m_strHeaders += value;
00099 m_strHeaders += "\r\n";
00100 bRet = true;
00101 }
00102 return bRet;
00103 }
00104
00105
00106
00107 bool ocSendMail::setMimeType( const char * type )
00108 {
00109 bool bRet = false;
00110 if( type && strlen( type ) )
00111 {
00112 m_content_type = "Content-Type";
00113 m_content_type += ": ";
00114 m_content_type += type;
00115 m_content_type += "\r\n";
00116 bRet = true;
00117 }
00118 return bRet;
00119 }
00120
00121 bool ocSendMail::openRoute( const char * from,
00122 const char * to,
00123 const char * subject,
00124 const char * cc,
00125 const char * bcc )
00126 {
00127 bool bRet = false;
00128
00129
00130 m_strHeaders = "";
00131 m_strBody = "";
00132
00133
00134 string command = m_strExePath;
00135
00136 command += " -f\"";
00137 command += from;
00138 command += "\" ";
00139
00140 command += to;
00141
00142 if( cc )
00143 {
00144 command += ", ";
00145 command += cc;
00146 }
00147 if( bcc )
00148 {
00149 command += ", ";
00150 command += bcc;
00151 }
00152
00153
00154 m_pfOut = popen( command.c_str(), "w" );
00155 if(m_pfOut != NULL)
00156 {
00157 bRet = true;
00158 if( from && strlen(from) )
00159 {
00160 addHeader( "From", from );
00161 }
00162 if( to && strlen(to) )
00163 {
00164 addHeader( "To", to );
00165 }
00166 if( cc && strlen(cc) )
00167 {
00168 addHeader( "Cc", cc );
00169 }
00170 if( bcc && strlen(bcc) )
00171 {
00172 addHeader( "Bcc", bcc );
00173 }
00174 if( subject && strlen(subject) )
00175 {
00176 addHeader( "Subject", subject );
00177 }
00178 }
00179
00180 setMimeType( "text/plain" );
00181 return bRet;
00182 }
00183
00184 bool ocSendMail::write( const char * text )
00185 {
00186 bool bRet = false;
00187 if( m_pfOut != NULL && text && strlen(text) )
00188 {
00189 bRet = true;
00190 m_strBody += text;
00191 m_strBody += "\r\n";
00192 }
00193 return bRet;
00194 }
00195
00196 bool ocSendMail::send( void )
00197 {
00198 bool bRet = false;
00199 if(m_pfOut != NULL)
00200 {
00201
00202
00203
00204
00205
00206
00207
00208 writelog("writing headers to process");
00209 fprintf( m_pfOut, m_strHeaders.c_str() );
00210
00211 if( m_files.size() )
00212 {
00213 fprintf( m_pfOut, "MIME-Version: 1.0\r\n" );
00214 fprintf( m_pfOut, "Content-Type: multipart/mixed;\r\n" );
00215 fprintf( m_pfOut, "\tboundary=\"%s\"\r\n", makeBoundary().c_str() );
00216 fprintf( m_pfOut, "%s\r\n", m_beginBoundary.c_str() );
00217 }
00218
00219
00220 fprintf( m_pfOut, m_content_type.c_str() );
00221
00222
00223 fprintf( m_pfOut, "\r\n" );
00224 fprintf( m_pfOut, m_strBody.c_str() );
00225 writelog("wrote body");
00226
00227
00228 if( m_files.size() )
00229 {
00230 for( ocFiles::iterator pos = m_files.begin();
00231 pos != m_files.end();
00232 pos ++ )
00233 {
00234 writelog2( "Writing out attachment: ", pos->second.name );
00235 fprintf( m_pfOut, "%s\r\n", m_beginBoundary.c_str() );
00236 fprintf( m_pfOut, "Content-Type: %s;\r\n", pos->second.type.c_str() );
00237 fprintf( m_pfOut, "\tname=\"%s\"\r\n", pos->second.name.c_str() );
00238 fprintf( m_pfOut, "Content-Transfer-Encoding: base64\r\n" );
00239 fprintf( m_pfOut, "Content-Disposition: attachment;\r\n" );
00240 fprintf( m_pfOut, "\tfilename=\"%s\"\r\n\r\n", pos->second.name.c_str() );
00241 decodeFile( pos );
00242 }
00243 fprintf( m_pfOut, "%s\r\n", m_endBoundary.c_str() );
00244 }
00245 writelog( "closing process" );
00246 bRet = ( pclose( m_pfOut ) != -1 );
00247 bRet = true;
00248 }
00249 return bRet;
00250 }
00251
00252 void ocSendMail::decodeFile( ocFiles::iterator & pos )
00253 {
00254 ifstream testen( pos->second.path.c_str() );
00255 ocCoder enc;
00256 unsigned char buff[64];
00257 while( testen.eof() == false )
00258 {
00259
00260 testen.read((char*)buff,57);
00261 string & decoded = enc.base64encode(buff,testen.gcount());
00262 writelog2( "Decoded: ", decoded );
00263 fprintf( m_pfOut, "%s\r\n", decoded.c_str() );
00264 }
00265 }
00266
00267 bool ocSendMail::attach ( ocFile & inFile )
00268 {
00269 bool bRet = false;
00270 m_files[ inFile.name ] = inFile;
00271 return bRet;
00272 }
00273
00274 bool ocSendMail::urlAttach ( const char * url )
00275 {
00276 bool bRet = false;
00277 if( m_pfOut != NULL && url && strlen(url) )
00278 {
00279 bRet = true;
00280 m_strBody += " <a href='";
00281 m_strBody += url;
00282 m_strBody += "'>Attachment</a> ";
00283 }
00284 return bRet;
00285 }
00286