00001 00002 /* 00003 * THIS FILE IS UNDER RCS - DO NOT MODIFY UNLESS YOU HAVE 00004 * CHECKED IT OUT USING THE COMMAND CHECKOUT. 00005 * 00006 * $Id: sendmail_8c-source.html 2161 2006-05-19 16:55:03Z paulf $ 00007 * 00008 * Revision history: 00009 * $Log$ 00009 * Revision 1.1 2006/05/19 16:55:02 paulf 00009 * first inclusion 00009 * 00010 * Revision 1.3 2001/10/02 18:15:18 dietz 00011 * Changed to use COMPUTERNAME in the email sender field 00012 * instead of Earthworm. 00013 * 00014 * Revision 1.2 2000/05/23 18:05:31 dietz 00015 * Changed blat sender to root@<MailHost> 00016 * 00017 * Revision 1.1 2000/02/14 18:53:30 lucky 00018 * Initial revision 00019 * 00020 * 00021 */ 00022 00023 /***************************************************************** 00024 * sendmail * 00025 * * 00026 * Function to send email. Windows NT version. * 00027 * * 00028 * This function requires a mail server computer. * 00029 * * 00030 * person: List of email recipients * 00031 * nmail: The number of recipients * 00032 * mailProg: Mail program to use - for solaris compatibility * 00033 * subject: Subject of the message * 00034 * msg : The body of the email message * 00035 * msgPrefix : Prefix to the body of the message * 00036 * msgSuffix : Suffix to the body of the message * 00037 * mailServer: Computer that sends the mail message. * 00038 * * 00039 * Returns -1 if an error occurred, 0 otherwise * 00040 * * 00041 * mailProg, subject, msgPrefix, and msgSuffix added by * 00042 * Lucky Vidmar Tue Jan 19 16:17:01 MST 1999 * 00043 * * 00044 *****************************************************************/ 00045 00046 #include <string.h> 00047 #include <ctype.h> 00048 #include <stdio.h> 00049 #include <stdlib.h> 00050 #include <windows.h> 00051 00052 #define BUFFSIZE 200 00053 00054 static char *Sender = NULL; 00055 00056 int SendMail( char person[][60], int nmail, char *mailProg, char *subject, 00057 char *msg, char *msgPrefix, char *msgSuffix, char *mailServer ) 00058 { 00059 FILE *fp; 00060 DWORD pathSize; 00061 char tmp[BUFFSIZE]; 00062 char pathBuffer[BUFFSIZE]; 00063 char tempFilename[MAX_PATH]; 00064 char commandLine[BUFFSIZE]; 00065 STARTUPINFO startUpInfo; 00066 BOOL success; 00067 DWORD priorityClass; 00068 PROCESS_INFORMATION procInfo; 00069 DWORD exitCode; 00070 DWORD rc; 00071 int i; 00072 00073 /* Check arguments 00074 ***************/ 00075 if ( nmail < 1 ) 00076 return -1; 00077 00078 /* Have Sender point to this computer's name 00079 *****************************************/ 00080 if ( Sender == NULL ) Sender = getenv( "COMPUTERNAME" ); 00081 00082 /* Get the path of the temporary file directory 00083 ********************************************/ 00084 pathSize = GetTempPath( BUFFSIZE, pathBuffer ); 00085 if ( pathSize < BUFFSIZE ) 00086 pathBuffer[pathSize] = 0; 00087 else 00088 { 00089 printf( "Error getting path of temporary file directory." ); 00090 return -1; 00091 } 00092 00093 /* Create a unique file in the temporary file directory 00094 ****************************************************/ 00095 if ( GetTempFileName( pathBuffer, "mai", 0, tempFilename ) == 0 ) 00096 { 00097 printf( "Error getting name of temporary file.\n" ); 00098 return -1; 00099 } 00100 00101 /* Write the mail message to the temporary file 00102 ********************************************/ 00103 fp = fopen( tempFilename, "w" ); 00104 if ( fp == NULL ) 00105 { 00106 printf( "Error opening the temporary file.\n" ); 00107 return -1; 00108 } 00109 00110 if (msgPrefix != NULL) 00111 fputs( msgPrefix, fp ); 00112 00113 fputs( msg, fp ); 00114 00115 if (msgSuffix != NULL) 00116 fputs( msgSuffix, fp ); 00117 00118 fclose( fp ); 00119 00120 /* Build a command line for the Blat program, which sends 00121 the temporary file to the mail server computer. 00122 ******************************************************/ 00123 strcpy( commandLine, "blat " ); 00124 strcat( commandLine, tempFilename ); 00125 00126 if (subject != NULL) 00127 { 00128 sprintf ( tmp, " -s \"%s\"", subject ); 00129 strcat( commandLine, tmp ); 00130 } 00131 00132 strcat( commandLine, " -f root@" ); 00133 strcat( commandLine, mailServer ); 00134 strcat( commandLine, " -i " ); 00135 strcat( commandLine, Sender ); 00136 strcat( commandLine, " -server " ); 00137 strcat( commandLine, mailServer ); 00138 strcat( commandLine, " -t " ); 00139 strcat( commandLine, &person[0][0] ); 00140 00141 for ( i = 1; i < nmail; i++ ) 00142 { 00143 strcat( commandLine, "," ); 00144 strcat( commandLine, &person[i][0] ); 00145 } 00146 00147 /* Invoked the Blat program 00148 ************************/ 00149 GetStartupInfo( &startUpInfo ); 00150 00151 priorityClass = GetPriorityClass( GetCurrentProcess() ); 00152 00153 success = CreateProcess( 0, 00154 commandLine, /* Command line to invoke child */ 00155 0, 0, /* No security attributes */ 00156 FALSE, /* No inherited handles */ 00157 0 | /* Child does not get a window */ 00158 priorityClass, /* Same as priority of parent */ 00159 0, /* Not passing environmental vars */ 00160 0, /* Current dir same as parent */ 00161 &startUpInfo, /* Attributes of process window */ 00162 &procInfo ); /* Attributes of child process */ 00163 if ( !success ) 00164 { 00165 printf( "Error starting Blat: %d\n", GetLastError() ); 00166 return -1; 00167 } 00168 00169 /* Wait 5 minutes for Blat to complete. 00170 Check the process exit code for abnormal termination. 00171 ****************************************************/ 00172 rc = WaitForSingleObject( procInfo.hProcess, 300000 ); 00173 00174 if ( rc == WAIT_FAILED ) 00175 { 00176 printf( "Blat WaitForSingleObject() failed with error: %d\n", GetLastError() ); 00177 return -1; 00178 } 00179 if ( rc == WAIT_TIMEOUT ) 00180 { 00181 printf( "Error. Blat not completed within 5 minutes.\n" ); 00182 return -1; 00183 } 00184 success = GetExitCodeProcess( procInfo.hProcess, &exitCode ); 00185 if ( !success ) 00186 { 00187 printf( "Error getting the Blat exit code: %d\n", GetLastError() ); 00188 return -1; 00189 } 00190 if ( exitCode != 0 ) 00191 { 00192 printf( "Blat failed.\n" ); 00193 return -1; 00194 } 00195 00196 /* Delete the mail file 00197 ********************/ 00198 success = DeleteFile( tempFilename ); 00199 if ( !success ) 00200 { 00201 printf( "Error deleting temporary file: %s\n", tempFilename ); 00202 return -1; 00203 } 00204 00205 /* Everything went smoothly 00206 ************************/ 00207 return 0; 00208 } 00209