cgi_mail.c

Go to the documentation of this file.
00001 /******************************************************************************/
00002 /* cgi_mail.c © Copyright 2005-2007 by Raosoft Inc. All Rights Reserved.      */
00003 /*                                                                            */
00004 /* You may use and modify this file for your own use, but may not distribute  */
00005 /* it or derivative works without the prior written consent of Raosoft, Inc.  */
00006 /*                                                                            */
00007 /* If you choose to share your modifications with Raosoft, Inc. the company   */
00008 /* will attempt to incorporate them into future versions of this file.        */
00009 /*                                                                            */
00010 /* This software is provided "as is," and Raosoft makes no warranty, express  */
00011 /* or implied, of fitness for a particular application. Every measure has been*/
00012 /* taken to anticipate risks inherent to computer networks, but we cannot     */
00013 /* guarantee safety or reliability of this program in every situation.        */
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)     /* init winsock lib. want v2.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) /* shutdown and close the socket if needed */
00114    {
00115      send(s, "QUIT\r\n" , 6 , 0);
00116      shutdown(s, 2);   /* shutdown both channels (read+write) */
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 /* #include <mapi.h> */
00134 
00135 int SendMail(char * recip, char * subject,
00136    char * textnote, char* server, char* profile, char* name, char* password)
00137 {
00138 /*
00139  personalserver.cpp overrides SendMail() for Windows CE and stand-alone servers
00140  by queing outbox.ezs file and sending with a background process.
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   /* load library */
00217 
00218   GetProfileString("Mail", "CMCDLLNAME32", "MAPI32.DLL", LibName, 1024);
00219   library = LoadLibrary(LibName);
00220   if ((UINT) library < 32) return 31;
00221 
00222   /* log on */
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   /* look up address */
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   /* send message */
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   /* log off */
00286 
00287   CMCFree(CMCReciparray);
00288   CMCLogoff(cmcSessionID,0,0,0);
00289   FreeLibrary(library);
00290   /* done */
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 int SendDataToEmail(CGINameValue* data, char* database,
00311    char * recip, char* SMTPServer, char* profile, char* name, char* password)
00312    {
00313     int i;
00314     int length;
00315     char* text, *c;
00316     char subject[1024];
00317 
00318     strcpy(subject,"EZSurvey data for ");
00319     strcat(subject,database);
00320 
00321     length = 256;
00322 
00323     for (i=0; data[i].name; i++)
00324      {
00325       if (data[i].name[0] == '_' || data[i].name[0] == 0) continue;
00326       length += strlen(data[i].name);
00327       length += strlen(data[i].value);
00328       length += 4;
00329      }
00330 
00331      text = CGIMALLOC(length);
00332      if (!text) return 35;
00333      c = stpcpy(text,"EZSurvey web data from: ");
00334      c = stpcpy(c,GetFieldValue(data,"HOST"));
00335      c = stpcpy(c,"\r\nX-DATABASE: ");
00336      c = stpcpy(c,database);
00337      c = stpcpy(c,"\r\n");
00338 
00339      for (i=0; data[i].name; i++)
00340      {
00341       if (data[i].name[0] == '_' || data[i].name[0] == 0) continue;
00342       c = stpcpy(c,data[i].name);
00343       c = stpcpy(c,": ");
00344       c = stpcpy(c,data[i].value);
00345       c = stpcpy(c,"\r\n");
00346      }
00347 
00348       i = SendMail(recip, subject,text,SMTPServer,profile,name,password);
00349 
00350      CGIFREE(text);
00351 
00352      return i;
00353    }
00354 */
00355 #else /* XP_WIN */
00356 
00357 /*
00358 int SendDataToEmail(CGINameValue* data, char* database,
00359    char * recip, char* SMTPServer, char* profile, char* name, char* password)
00360    {
00361     FILE * f;
00362     int i;
00363 
00364     f = popen("sendmail -t","w");
00365     if (!f) f = popen(SENDMAIL " -t","w");
00366     if (!f) f = popen("/usr/lib/sendmail -t","w");
00367     if (!f) return 36;
00368 
00369     fprintf(f,"To: %s\n",recip);
00370     fprintf(f,"Subject: EZSurvey web data for %s\n\n",database);
00371     fprintf(f,"EZSurvey web data from: %s\r\nX-DATABASE: %s\n",
00372               GetFieldValue(data,"HOST"),database);
00373 
00374     for (i=0; data[i].name; i++)
00375      {
00376       if (data[i].name[0] == '_' || data[i].name[0] == 0) continue;
00377       fprintf(f,"%s: %s\n",data[i].name,data[i].value);
00378      }
00379     fprintf(f,".\nQUIT\n");
00380 
00381     pclose(f);
00382 
00383     return 0;
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)) /* standard text message */
00408   {
00409    fprintf(f,"Subject: %s\n",subject);
00410    fprintf(f,"\n");
00411    fwrite(textnote,1,strlen(textnote),f);
00412   }
00413   else /* HTML message */
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


Raosoft, Inc.
Raosoft EZReport, EZSurvey, InterForm, RapidReport, Raosoft, and SurveyWin are registered trademarks of Raosoft, Inc. Page contents © 1996-2007 by Raosoft, Inc. You may use and modify this file for your own use, but may not distribute it or derivative works without the prior written consent of Raosoft, Inc. This software is provided "as is," and Raosoft makes no warranty, express or implied, of fitness for a particular application. Every measure has been taken to anticipate risks inherent to computer networks, but we cannot guarantee safety or reliability of this program in every situation.
Tel: 206-525-4025 (US) Email: raosoft@raosoft.com
http://www.raosoft.com/