00001
00002
00003
00004
00005
00006
00007
00008
00009 #include <fstream>
00010 #include <string>
00011 #include <uuid/uuid.h>
00012 #include "oc_Mail.h"
00013 #include "ocString.h"
00014 #include "ocCoder.h"
00015
00016 #include "openLogger.h"
00017
00018 void ocSendMail::getTime( void )
00019 {
00020 time_t lt;
00021 time(<);
00022 localtime_r(<,&tm_time);
00023 }
00024
00025 string ocSendMail::makeUUID(void)
00026 {
00027 string UUID;
00028 uuid_t uuid;
00029 char uuid_val[37];
00030 memset(uuid,'\0',sizeof(uuid));
00031 uuid_generate(uuid);
00032 uuid_unparse(uuid, uuid_val);
00033 ocString uuid_work(uuid_val);
00034 UUID = uuid_work.replaceAll("-","");
00035 return UUID;
00036 }
00037
00038 string ocSendMail::makeBoundary( void )
00039 {
00040
00041 ocString ret = "-Open_Mail_Boundary-";
00042 getTime();
00043 ret += makeUUID();
00044 m_beginBoundary = "--";
00045 m_beginBoundary += ret;
00046 m_endBoundary = m_beginBoundary;
00047 m_endBoundary += "--";
00048 return ret;
00049 }
00050
00051 bool ocSendMail::loadAddresses ( string path )
00052 {
00053 bool bRet = false;
00054 ifstream addressFile( path.c_str() );
00055 if( addressFile )
00056 {
00057 char addressBuffer[512];
00058 addressFile.getline( addressBuffer, sizeof(addressBuffer) );
00059 while( addressFile.rdstate() == ios::goodbit )
00060 {
00061 string valuePair = addressBuffer;
00062 string::size_type pos = valuePair.find("=");
00063 if( pos != string::npos )
00064 {
00065 string name = valuePair.substr(0, pos);
00066 string value = valuePair.substr( pos+1 );
00067 m_addresses[name] = value;
00068 }
00069 addressFile.getline( addressBuffer, sizeof(addressBuffer) );
00070 }
00071 bRet = true;
00072 }
00073 return bRet;
00074 }
00075
00076 string ocSendMail::getAddress( string name )
00077 {
00078 string retval;
00079 strStrMap::iterator pos = m_addresses.find(name);
00080 if( pos != m_addresses.end() )
00081 {
00082 retval = pos->second.c_str();
00083 }
00084 return retval;
00085 }
00086
00087 ocSendMail::ocSendMail( string address, int port ):smtp(address,port)
00088 {
00089
00090 setMimeType( "text/plain" );
00091 }
00092
00093 ocSendMail::~ocSendMail()
00094 {
00095 ;
00096 }
00097
00098 bool ocSendMail::addHeader ( string header, string value )
00099 {
00100 bool bRet = false;
00101 if( header.length() &&
00102 value.length() )
00103 {
00104 m_strHeaders = header;
00105 m_strHeaders += ": ";
00106 m_strHeaders += value;
00107 bRet = smtp(m_strHeaders);
00108 }
00109 return bRet;
00110 }
00111
00112 bool ocSendMail::setMimeType( string type )
00113 {
00114 bool bRet = false;
00115 if( type.length() )
00116 {
00117 m_content_type = "Content-Type";
00118 m_content_type += ": ";
00119 m_content_type += type;
00120 m_content_type += "\r\n";
00121 bRet = true;
00122 }
00123 return bRet;
00124 }
00125
00126 bool parseRecipients( ocString addresses )
00127 {
00128
00129
00130 }
00131 bool ocSendMail::openRoute( string from,
00132 string to,
00133 string subject,
00134 string cc,
00135 string bcc,
00136 string serverAddress )
00137 {
00138 bool bRet = false;
00139
00140
00141 m_strHeaders = "";
00142 m_strBody = "";
00143
00144 if( smtp.isConnected )
00145 {
00146 if( smtp.helo(serverAddress) == false )
00147 {
00148 send_result += "Hello fail ";
00149 send_result += smtp.error;
00150 send_result += "\n";
00151 smtp.quit();
00152 return bRet;
00153 }
00154
00155 if( smtp.mail_from(from) == false )
00156 {
00157 send_result += "Mail fail ";
00158 send_result += smtp.error;
00159 send_result += "\n";
00160 smtp.quit();
00161 return bRet;
00162 }
00163 writelog( smtp.response );
00164
00165 if( smtp.rcpt_to( to ) == false )
00166 {
00167 send_result = "Recipient fail " + smtp.error;
00168 smtp.quit();
00169 return bRet;
00170 }
00171 if( smtp.data() == false )
00172 {
00173 send_result += "Data fail";
00174 send_result += smtp.error;
00175 send_result += "\n";
00176 smtp.quit();
00177 return bRet;
00178 }
00179 bRet = true;
00180 if( from.length() )
00181 {
00182 addHeader( "From", from );
00183 addHeader( "Sender", from );
00184 }
00185 if( to.length() )
00186 {
00187 addHeader( "To", to );
00188 }
00189 if( cc.length() )
00190 {
00191 addHeader( "Cc", cc );
00192 }
00193 if( bcc.length() )
00194 {
00195 addHeader( "Bcc", bcc );
00196 }
00197 if( subject.length() )
00198 {
00199 addHeader( "Subject", subject );
00200 }
00201 }
00202 else
00203 {
00204 send_result = "Did not recieve proper connect (220) response: " + smtp. response;
00205 }
00206 return bRet;
00207 }
00208
00209 bool ocSendMail::write( string text )
00210 {
00211 bool bRet = false;
00212 if( text.length() )
00213 {
00214 bRet = true;
00215 m_strBody += text;
00216 m_strBody += "\r\n";
00217 }
00218 return bRet;
00219 }
00220
00221 bool ocSendMail::send( void )
00222 {
00223 bool bRet = false;
00224 if(smtp.isConnected)
00225 {
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235 if( m_files.size() )
00236 {
00237 smtp( "MIME-Version: 1.0" );
00238 smtp( "MIME-Version: 1.0" );
00239 smtp( "Content-Type: multipart/mixed;" );
00240 string boundary = "\tboundary=\"";
00241 boundary += makeBoundary();
00242 smtp( boundary );
00243 boundary = m_beginBoundary;
00244 smtp( boundary );
00245 }
00246
00247
00248
00249 smtp( m_content_type );
00250
00251
00252 smtp( "" );
00253
00254 smtp( m_strBody.replaceAll( "\n.\n", "\n..\n" ) );
00255
00256 writelog2("wrote body",m_strBody);
00257
00258
00259 if( m_files.size() )
00260 {
00261 string tmp;
00262 for( ocFiles::iterator pos = m_files.begin();
00263 pos != m_files.end();
00264 pos ++ )
00265 {
00266 writelog2( "Writing out attachment: ", pos->second.name );
00267 smtp( m_beginBoundary );
00268 tmp = "Content-Type: ";
00269 tmp += pos->second.type;
00270 tmp += "\"";
00271 smtp( tmp );
00272 tmp = "\tname=\"";
00273 tmp += pos->second.name;
00274 tmp += "\"";
00275 smtp( tmp );
00276 smtp( "Content-Transfer-Encoding: base64" );
00277 smtp( "Content-Disposition: attachment;" );
00278 tmp = "\tfilename=\"";
00279 tmp += pos->second.name;
00280 tmp += "\"";
00281 smtp( tmp );
00282 smtp( "" );
00283 decodeFile( pos );
00284 }
00285 smtp( m_endBoundary );
00286 }
00287 writelog( "closing mail" );
00288 smtp.end_data();
00289 smtp.quit();
00290 bRet = true;
00291 }
00292 else
00293 {
00294 writelog("not connected!!!!!!!!!!");
00295 }
00296 return bRet;
00297 }
00298
00299 void ocSendMail::decodeFile( ocFiles::iterator & pos )
00300 {
00301 ifstream testen( pos->second.path.c_str() );
00302 ocCoder enc;
00303 unsigned char buff[64];
00304 while( testen.eof() == false )
00305 {
00306
00307 testen.read((char*)buff,57);
00308 string & decoded = enc.base64encode(buff,testen.gcount());
00309 writelog2( "Decoded: ", decoded );
00310 smtp( decoded );
00311 }
00312 }
00313
00314 bool ocSendMail::attach ( ocFile & inFile )
00315 {
00316 bool bRet = false;
00317 m_files[ inFile.name ] = inFile;
00318 return bRet;
00319 }
00320