home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / hyprmail.zip / mail.c < prev    next >
C/C++ Source or Header  |  1997-03-16  |  5KB  |  202 lines

  1. /*
  2. ** mail.c 1.0
  3. ** This tiny CGI program constructs an email form (GET) and
  4. ** sends the contents (POST).
  5. **
  6. ** Copyright (C) 1994, Enterprise Integration Technologies Corp.        
  7. ** All Rights Reserved.
  8. ** Jay Weber, weber@eit.com
  9. ** Kevin Hughes, kevinh@eit.com 
  10. ** 7/28/94
  11. */
  12.  
  13. #include <stdio.h>
  14. #include <sys/types.h>
  15. #include <sys/socket.h>
  16. #include <netdb.h>
  17. #include "libcgi/cgi.h"
  18.  
  19. #define CGINAME  "/cgi-bin/mail"
  20. #define SENDMAIL "sendmail"
  21. #define ROWS     25
  22. #define COLS     60
  23. #define MAXLEN   80
  24.  
  25. void printform();
  26. char *strdup();
  27. void progerr();
  28. char *lookupnumaddr();
  29.  
  30. cgi_main(ci)
  31.      cgi_info *ci;
  32. {
  33.     form_entry *parms, *p;
  34.     form_entry *get_form_entries();
  35.     char *from, *to, *subject, *replyto, *body, *host;
  36.     char tmpstr[MAXLEN];
  37.     FILE *f;
  38.  
  39.     print_mimeheader("text/html");
  40.  
  41.     from = to = subject = body = replyto = "";
  42.     parms = get_form_entries(ci);
  43.  
  44.     if (parms) {
  45.         for (p = parms; p; p = p->next) {
  46.             if (!stricmp(p->name, "from"))
  47.                 from = p->val;
  48.             else if (!stricmp(p->name, "to"))
  49.                 to = p->val;
  50.             else if (!stricmp(p->name, "subject"))
  51.                 subject = p->val;
  52.             else if (!stricmp(p->name, "body"))
  53.                 body = p->val;
  54.             else if (!stricmp(p->name, "replyto"))
  55.                 replyto = p->val;
  56.         }
  57.     }
  58.  
  59.     sprintf(tmpstr, lookupnumaddr(ci->remote_addr));
  60.     if (strchr(tmpstr, '.'))
  61.         host = (char *) strdup(tmpstr);
  62.     else
  63.         host = (char *) strdup(ci->remote_addr);
  64.  
  65.     sprintf(tmpstr, "%s@%s", (ci->remote_user && *(ci->remote_user)) ?
  66.     ci->remote_user : "", host);
  67.     if (from == NULL || from[0] == '\0')
  68.         from = (char *) strdup(tmpstr);
  69.  
  70.     switch (mcode(ci)) {
  71.  
  72.     case MCODE_HEAD:
  73.         return;
  74.  
  75.     case MCODE_GET:
  76.         printform(to, from, subject, body, replyto);
  77.         break;
  78.     
  79.     case MCODE_POST:
  80.         if (from == NULL || from[0] == '\0' || from[0] == '@' ||
  81.         from[strlen(from) - 1] == '@' || !strchr(from, '@'))
  82.             progerr("Invalid <b>From:</b> address.");
  83.         if (to == NULL || to[0] == '\0' || to[0] == '@' ||
  84.         to[strlen(to) - 1] == '@' || !strchr(to, '@'))
  85.             progerr("Invalid <b>To:</b> address.");
  86.         if (subject == NULL || subject[0] == '\0')
  87.             progerr("The subject field is blank.");
  88.         if (body == NULL || body[0] == '\0')
  89.             progerr("No message has been written.");
  90.  
  91.         sprintf(tmpstr, "%s -t", SENDMAIL);
  92.         if (f = popen(tmpstr, "w")) {
  93.             fprintf(f, "From: %s\nTo: %s\n", from, to);
  94.             if (replyto != NULL && replyto[0] != '\0')
  95.                 fprintf(f, "In-Reply-To: <%s>\n", replyto);
  96.             fprintf(f, "Subject: %s\nX-Sender: %s\n\n%s",
  97.             subject, host, body);
  98.             pclose(f);
  99.  
  100.             printf("<title>Email Gateway Response</title>\n");
  101.             printf("<h1>Your message has been sent</h1>\n");
  102.             printf("The following message has been sent to ");
  103.             printf("<b>%s</b>:\n<p>\n<pre>\n<hr>\n\n", to);
  104.  
  105.             printf("From: %s\n", from);
  106.             printf("To: %s\n", to);
  107.             if (replyto != NULL && replyto[0] != '\0')
  108.                 printf("In-Reply-To: <%s>\n", replyto);
  109.             printf("Subject: %s\n", subject);
  110.             printf("X-Sender: %s\n\n", host);
  111.             printf("%s\n", body);
  112.  
  113.             printf("\n<hr></pre>\n");
  114.         }
  115.         else
  116.             progerr("The message was not sent - sendmail error.");
  117.         break;
  118.  
  119.         default:
  120.             sprintf(tmpstr, "Unrecognized method used: \"%s\".",
  121.             ci->request_method);
  122.             progerr(tmpstr);
  123.     }
  124.  
  125.     free_form_entries(parms);
  126. }
  127.  
  128. void printform(to, from, subject, body, replyto)
  129.      char *to;
  130.      char *from;
  131.      char *subject;
  132.      char *body;
  133.      char *replyto;
  134. {
  135.     printf("<title>Email Gateway</title>\n");
  136.     printf("<h1>Email Gateway</h1>\n");
  137.     printf("<i>Send an email message by filling in the form below.</i>\n");
  138.     printf("<p>\n");
  139.  
  140.     printf("<form method=POST action=\"%s\">\n", CGINAME);
  141.     printf("<pre>\n");
  142.     printf("<b>         To:</b> ");
  143.     printf("<input size=45 name=\"to\" value=\"%s\">\n", to);
  144.     printf("<b>       From:</b> ");
  145.     printf("<input size=45 name=\"from\" value=\"%s\">\n", from);
  146.     printf("<b>    Subject:</b> ");
  147.     printf("<input size=45 name=\"subject\" value=\"%s\">\n", subject);
  148.     if (replyto != NULL && replyto[0] != '\0') {
  149.         printf("<b>In reply to:</b> ");
  150.         printf("<input size=45 name=\"replyto\" value=\"%s\">\n",
  151.         replyto);
  152.     }
  153.     printf("\n");
  154.  
  155.     printf("<textarea name=\"body\" value=\"\" ");
  156.     printf("rows=%d cols=%d>%s\n</textarea>\n\n", ROWS, COLS, body);
  157.  
  158.     printf("               ");
  159.     printf("<input type=submit value=\"    Send Email    \">     ");
  160.     printf("<input type=reset value=\"    Reset Form    \">");
  161.     printf("</pre>\n</form>\n");
  162. }
  163.  
  164. char *strdup(s)
  165.      char *s;
  166. {
  167.     char *p;
  168.  
  169.     p = (char *) malloc(strlen(s) + 1);
  170.     strcpy(p, s);
  171.     return p;
  172. }
  173.  
  174. void progerr(s)
  175.      char *s;
  176. {
  177.     printf("<title>Email Gateway Error</title>\n");
  178.     printf("<h1>Email Gateway Error</h1>\n");
  179.  
  180.     printf("This gateway program encountered an error:\n<p>\n");
  181.     printf("<code>%s</code>\n", s);
  182.  
  183.     exit(0);
  184. }
  185.  
  186. char *lookupnumaddr(numaddress)
  187.      char *numaddress;
  188. {
  189.         unsigned long addr;
  190.         struct hostent *he;
  191.  
  192.         addr = inet_addr(numaddress);
  193.         if (addr == -1)
  194.                 return numaddress;
  195.  
  196.         he = gethostbyaddr((char *) &addr, sizeof(addr), AF_INET);
  197.         if (he)
  198.                 return he->h_name;
  199.         else
  200.                 return numaddress;
  201. }
  202.