source: trunk/src/libsrc/unix/sendmail.c @ 2170

Revision 2170, 3.6 KB checked in by dietz, 14 years ago (diff)

Added "from" argument which was required in Windows version. It's a
dummy argument in this Unix version

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1
2/*
3 *   THIS FILE IS UNDER RCS - DO NOT MODIFY UNLESS YOU HAVE
4 *   CHECKED IT OUT USING THE COMMAND CHECKOUT.
5 *
6 *    $Id$
7 *
8 *    Revision history:
9 *     $Log$
10 *     Revision 1.2  2006/05/19 23:56:20  dietz
11 *     Added "from" argument which was required in Windows version. It's a
12 *     dummy argument in this Unix version
13 *
14 *     Revision 1.1  2005/07/15 18:20:20  friberg
15 *     Unix version of libsrc for POSIX systems
16 *
17 *     Revision 1.5  2004/04/12 22:27:51  dietz
18 *     included string.h
19 *
20 *     Revision 1.4  2001/03/15 23:53:31  alex
21 *     removed "free( cmnd )" commands, since cmnd is not malloced! Alex
22 *
23 *     Revision 1.3  2001/03/14 21:11:37  lucky
24 *     Changed the way that the message is built and sent -- no more
25 *     standard input method -- just echo the message into the mail program.
26 *
27 *     Revision 1.2  2000/06/30 17:35:12  lombard
28 *     Added call to stat() to check for existence of mail program
29 *
30 *     Revision 1.1  2000/02/14 18:46:17  lucky
31 *     Initial revision
32 *
33 *
34 */
35
36                     /*******************************
37                      *          sendmail           *
38                      *                             *
39                      *   Function to send email.   *
40                      *******************************/
41
42#include <stdio.h>
43#include <string.h>
44#include <malloc.h>
45#include <sys/stat.h>
46#include <sys/types.h>
47#include <errno.h>
48#include <earthworm.h>
49
50
51#define DEFAULT_MAIL_PROG       "/usr/ucb/Mail"
52
53/*
54 * SendMail ()
55 *   list of receipients
56 *   number of emails to send
57 *   mail program to use
58 *   subject of the message
59 *   message to send
60 *   message prefix
61 *   message suffix
62 *   mail server to use
63 *
64 *
65 * Modified by Lucky Vidmar Tue Jan 19 16:05:00 MST 1999
66 *  to include the message and the configurable mail program
67 *
68 */
69
70
71int SendMail( char person[][60], int nmail, char *mailProg, 
72                      char *subject, char *msg, char *msgPrefix, 
73                      char *msgSuffix, char *mailServer, char *from )
74{
75   FILE *f;
76   char MsgText[MAX_EMAIL_MSG_SIZE]; 
77   char cmnd[2*MAX_EMAIL_MSG_SIZE]; 
78   char *msg_ptr;       
79   int  i, max_char;
80   struct stat mailstat;
81
82   /* Check incomming arguments */
83   if (mailServer == NULL)
84   {
85      fprintf( stderr, "sendmail: Invalid arguments passed in\n" );
86      return( -1 );
87   }
88
89   if (mailProg == NULL)
90   {
91      mailProg = (char *) calloc ((strlen (DEFAULT_MAIL_PROG) + 1), 
92                          sizeof (char));
93      strcpy (mailProg, DEFAULT_MAIL_PROG);
94   }
95
96   /* Make sure we can find the mail program */
97   if ( stat(mailProg, &mailstat) < 0)
98   {
99     fprintf(stderr, "SendMail: error accessing mail program <%s>: %s\n",
100             mailProg, strerror(errno));
101     return( -1 );
102   }
103   /* Maybe we should check for execute permissions but that takes more work */
104
105
106   /* Build the message */
107   if (msgPrefix != NULL)
108       sprintf (MsgText, "%s%s", msgPrefix, msg);
109   else 
110                sprintf (MsgText, "%s", msg);
111   
112   if (msgSuffix != NULL)
113        strcat (MsgText, msgSuffix);
114
115   for ( i = 0; i < nmail; i++ )
116   {
117      /* Mail message without a subject */
118      if (subject == NULL)
119      {
120         (void) sprintf( cmnd, "echo \"%s\" | %s %s", 
121                                                                MsgText, mailProg, person[i] );
122      }
123      /* Mail message with the given subject */
124      else
125      {
126         (void) sprintf( cmnd, "echo \"%s\" | %s -s \"%s\" %s", 
127                                                                        MsgText, mailProg, subject, person[i] );
128      }
129
130      if ( ( f = popen( cmnd, "w" ) ) == NULL )
131      {
132         fprintf( stderr, "sendmail: Can't run command \"%s\"\n", cmnd );
133         return( -1 );
134      }
135      pclose( f );
136   }
137   return( 0 );
138}
139
Note: See TracBrowser for help on using the repository browser.