00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "cgi.h"
00018
00019 #ifdef XP_WIN
00020 #include <winsock2.h>
00021 #endif
00022
00024 int WinsockConnect(char* host, int port, unsigned int*s);
00025
00026 int Reply(unsigned int s)
00027 {
00028 char reply[256];
00029 int i=0;
00030 for (i=0; i<256; i++)
00031 {
00032 if (!recv(s, reply+i, 1, 0))
00033 return 0;
00034 if (reply[i] == '\n')
00035 {
00036 reply[i]=0;
00037 break;
00038 }
00039 }
00040
00041 reply[255]=0;
00042 return atoi(reply);
00043 }
00044
00045 int SMTPSendMail(char * host, char* subject, char* recip, char* textnote)
00046 {
00047 unsigned int s;
00048 #if defined(XP_WIN)
00049 WSADATA d;
00050 #else
00051 #define INVALID_SOCKET 0xffffffff
00052 #endif
00053 int err;
00054
00055 #if defined(XP_WIN)
00056 if (WSAStartup(2, &d) != 0)
00057 {
00058 WSACleanup();
00059 return 61;
00060 }
00061 #endif
00062
00063 err = WinsockConnect(host, 25, &s);
00064 if (!err)
00065 {
00066 err = 2;
00067
00068 if (Reply(s) != 220) goto done;
00069 send(s, "HELO ", 5, 0);
00070 send(s, host, strlen(host), 0);
00071 send(s, "\r\n", 2, 0);
00072
00073 if (Reply(s) != 250) goto done;
00074
00075 err = 0;
00076 send(s, "MAIL FROM: <", 14, 0);
00077 send(s, recip, strlen(recip), 0);
00078 send(s, ">\r\nRCPT TO: <", 13, 0);
00079 send(s, recip, strlen(recip), 0);
00080 send(s, ">\r\nDATA\r\n", 9, 0);
00081 send(s, "Subject: ", 9, 0);
00082 send(s, subject, strlen(subject), 0);
00083 send(s, "\r\nFrom: ", 8, 0);
00084 send(s, recip, strlen(recip), 0);
00085 send(s, "\r\nTo: ", 7, 0);
00086 send(s, recip, strlen(recip), 0);
00087
00088 if (strnicmp(textnote,"<HTML",5))
00089 {
00090 send(s, "\r\n\r\n", 4,0);
00091 send(s, textnote,strlen(textnote),0);
00092 }
00093 else
00094 {
00095 char*c = "\r\nMIME-Version: 1.0\r\n"
00096 "Content-Type: multipart/alternative; boundary=\"_Mime-Boundary_72616F\"\r\n\r\n";
00097 send(s,c , strlen(c),0);
00098 c = " This is a multi-part message in MIME format.\r\n\r\n"
00099 "--_Mime-Boundary_72616F\r\n"
00100 "Content-Type: text/html; charset=\"utf-8\""
00101 "Content-Transfer-Encoding: 8bit\r\n\r\n";
00102 send(s,c , strlen(c),0);
00103 send(s, textnote,strlen(textnote),0);
00104 c = "\r\n--_Mime-Boundary_72616F--";
00105 send(s,c , strlen(c),0);
00106 }
00107
00108 send(s, "\r\n.\r\n" , 5 , 0);
00109
00110 }
00111
00112 done:
00113 if (s != INVALID_SOCKET)
00114 {
00115 send(s, "QUIT\r\n" , 6 , 0);
00116 shutdown(s, 2);
00117 #ifdef XP_WIN
00118 closesocket(s);
00119 #endif
00120 #ifdef XP_UNIX
00121 close(s);
00122 #endif
00123 }
00124
00125 #if defined(XP_WIN)
00126 WSACleanup();
00127 #endif
00128
00129 return err;
00130 }
00131
00132 #if defined(__WINCE__)
00133
00134
00135 int SendMail(char * recip, char * subject,
00136 char * textnote, char* server, char* profile, char* name, char* password)
00137 {
00138
00139
00140
00141
00142 return 0;
00143 }
00144
00145 #elif defined(XP_WIN)
00146 #include <winsock2.h>
00147
00148 #define CMC_USE_MSWIN_NT
00149
00150 #ifdef __BORLANDC__
00151 #include <win32/xcmc.h>
00152 #include <win32/xcmcext.h>
00153 #include <win32/xcmcmsxt.h>
00154 #else
00155 #include "xcmc.h"
00156 #include "xcmcext.h"
00157 #include "xcmcmsxt.h"
00158 #endif
00159
00160 #define CMC_API FAR PASCAL
00161 #define CMC_CALLBACK CMC_API *
00162
00163 typedef CMC_return_code (CMC_CALLBACK PCMCLOGOFF)
00164 (CMC_session_id session,
00165 CMC_ui_id ui_id,
00166 CMC_flags logoff_flags,
00167 CMC_extension *logoff_extensions);
00168
00169 typedef CMC_return_code (CMC_CALLBACK PCMCLOOKUP)
00170 (CMC_session_id session,
00171 CMC_recipient *recipient_in,
00172 CMC_flags look_up_flags,
00173 CMC_ui_id ui_id,
00174 CMC_uint32 *count,
00175 CMC_recipient **recipient_out,
00176 CMC_extension *look_up_extensions);
00177
00178 typedef CMC_return_code (CMC_CALLBACK PCMCLOGON)
00179 (CMC_string service,
00180 CMC_string user,
00181 CMC_string password,
00182 CMC_object_identifier character_set,
00183 CMC_ui_id ui_id,
00184 CMC_uint16 caller_cmc_version,
00185 CMC_flags logon_flags,
00186 CMC_session_id *session,
00187 CMC_extension *logon_extensions);
00188
00189 typedef CMC_return_code (CMC_CALLBACK PCMCSEND)
00190 (CMC_session_id session,
00191 CMC_message *message,
00192 CMC_flags send_flags,
00193 CMC_ui_id ui_id,
00194 CMC_extension *send_extensions);
00195
00196 typedef CMC_return_code (CMC_CALLBACK PCMCFREE)
00197 (CMC_buffer memory);
00198
00199 int CMCSendMail(char * recip, char * subject,
00200 char * textnote, char* profile, char* name, char* password)
00201 {
00202 PCMCLOGOFF CMCLogoff;
00203 PCMCLOGON CMCLogon;
00204 PCMCSEND CMCSend;
00205 PCMCLOOKUP CMCLookUp;
00206 PCMCFREE CMCFree;
00207 CMC_session_id cmcSessionID;
00208 char LibName[1024];
00209 HANDLE library;
00210 CMC_return_code cmcsts;
00211 CMC_recipient recipient;
00212 CMC_recipient * CMCReciparray=0;
00213 CMC_message message;
00214 unsigned long int recipcount = 1;
00215
00216
00217
00218 GetProfileString("Mail", "CMCDLLNAME32", "MAPI32.DLL", LibName, 1024);
00219 library = LoadLibrary(LibName);
00220 if ((UINT) library < 32) return 31;
00221
00222
00223 CMCLogoff = (PCMCLOGOFF)GetProcAddress(library,"cmc_logoff");
00224 CMCLogon = (PCMCLOGON) GetProcAddress(library,"cmc_logon");
00225 CMCSend = (PCMCSEND) GetProcAddress(library,"cmc_send");
00226 CMCLookUp = (PCMCLOOKUP)GetProcAddress(library,"cmc_look_up");
00227 CMCFree = (PCMCFREE)GetProcAddress(library,"cmc_free");
00228
00229 if (!CMCLogoff || !CMCLogon || !CMCSend || !CMCFree)
00230 {FreeLibrary(library); return 32;}
00231
00232 cmcsts = CMCLogon(profile,name,password,0,0,
00233 CMC_VERSION,0,&cmcSessionID,0);
00234
00235 if (cmcsts) {FreeLibrary(library); return 32;}
00236
00237
00238 recipient.name = recip;
00239 recipient.name_type = CMC_TYPE_UNKNOWN;
00240 recipient.address = NULL;
00241 recipient.role = CMC_ROLE_TO;
00242 recipient.recip_flags = 0;
00243 recipient.recip_extensions = NULL;
00244 recipient.recip_flags = CMC_RECIP_LAST_ELEMENT;
00245
00246 if (CMCLookUp)
00247 cmcsts = CMCLookUp(cmcSessionID,
00248 &recipient,
00249 CMC_LOOKUP_RESOLVE_PREFIX_SEARCH,
00250 0,
00251 &recipcount,
00252 &CMCReciparray,
00253 0
00254 );
00255
00256 if (cmcsts)
00257 {
00258 CMCLogoff(cmcSessionID,0,0,0);
00259 FreeLibrary(library);
00260 return 33;
00261 }
00262
00263
00264
00265 message.message_reference =NULL;
00266 message.message_type = "CMC: IPM";
00267 message.subject = subject;
00268 memset(&message.time_sent,0,sizeof(message.time_sent));
00269 message.text_note = textnote;
00270 message.recipients = CMCReciparray;
00271 message.attachments = NULL;
00272 message.message_flags = CMC_MSG_UNSENT;
00273 message.message_extensions = NULL;
00274
00275 cmcsts = CMCSend(cmcSessionID,&message,0,0,0);
00276
00277 if (cmcsts)
00278 {
00279 CMCFree(CMCReciparray);
00280 CMCLogoff(cmcSessionID,0,0,0);
00281 FreeLibrary(library);
00282 return 34;
00283 }
00284
00285
00286
00287 CMCFree(CMCReciparray);
00288 CMCLogoff(cmcSessionID,0,0,0);
00289 FreeLibrary(library);
00290
00291 return 0;
00292 }
00293
00294
00295 int SendMail(char * recip, char * subject,
00296 char * text, char* server, char* profile, char* name, char* password)
00297 {
00298 int i = 1;
00299
00300 if (server)
00301 if (*server)
00302 i = SMTPSendMail(server, subject, recip, text);
00303
00304 if (i != 0)
00305 i = CMCSendMail(recip, subject,text,profile,name,password);
00306
00307 return i;
00308 }
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355 #else
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387 int SendMail(char * recip, char * subject,
00388 char * textnote, char* server, char* profile, char* name, char* password)
00389 {
00390 FILE * f;
00391
00392 if (server && *server)
00393 {
00394 if (!SMTPSendMail(server, subject, recip, textnote))
00395 return 0;
00396 }
00397
00398 f = popen("sendmail -t","w");
00399 if (!f) f = popen("/usr/sbin/sendmail -t","w");
00400 if (!f) f = popen("/usr/lib/sendmail -t","w");
00401 if (!f) f = popen("/usr/lib/sbin/sendmail -t","w");
00402 if (!f) return 36;
00403
00404 fprintf(f,"To: %s\n",recip);
00405 fprintf(f,"From: %s\n",recip);
00406
00407 if (strnicmp(textnote,"<HTML",5))
00408 {
00409 fprintf(f,"Subject: %s\n",subject);
00410 fprintf(f,"\n");
00411 fwrite(textnote,1,strlen(textnote),f);
00412 }
00413 else
00414 {
00415 char*c = "MIME-Version: 1.0\n"
00416 "Content-Type: multipart/alternative; boundary=\"_Mime-Boundary_72616F\"\n\n";
00417 fprintf(f,"Subject: %s\n",subject);
00418 fwrite(c ,1, strlen(c),f);
00419 c = " This is a multi-part message in MIME format.\n\n";
00420 "--_Mime-Boundary_72616F\n"
00421 "Content-Type: text/html; charset=\"iso-8859-1\""
00422 "Content-Transfer-Encoding: 8bit\n\n";
00423 fwrite(c ,1, strlen(c),f);
00424 fwrite(textnote,1,strlen(textnote),f);
00425 c = "--_Mime-Boundary_72616F--\r\n";
00426 fwrite(c ,1, strlen(c),f);
00427 }
00428
00429 fwrite("\n.\n",1,3,f);
00430 pclose(f);
00431 return 0;
00432 }
00433
00434 #endif