home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 400_01 / socketpp-1.5 / test / tsendfiles.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-06  |  1.4 KB  |  66 lines

  1. #include <fstream.h>
  2. #include <sockinet.h>
  3. #include <pwd.h>
  4.  
  5. extern "C" {
  6.     int    sprintf(char*, const char*, ...);
  7.     uid_t    getuid();
  8. }
  9.  
  10. static int send_cmd(iosockstream&, const char* cmd=0);
  11.  
  12. main(int ac, char** av)
  13. {
  14.     if (ac < 3) {
  15.     cerr << "USAGE: " << av[0] << " recipient-email-addr files...\n";
  16.     return 1;
  17.     }
  18.     
  19.     char         rcpt[512];
  20.     char         sender[512];
  21.     iosockinet   sio(sockbuf::sock_stream);
  22.     sockinetaddr sina(sio->localhost(), "smtp", "tcp");
  23.     
  24.     sio->connect(sina);
  25.     
  26.     send_cmd(sio, 0);
  27.     send_cmd(sio, "HELO");
  28.     
  29.     
  30.     sprintf(rcpt, "RCPT TO:%s", av[1]);
  31.     
  32.     passwd* pw = getpwuid( getuid() );
  33.     sprintf(sender, "MAIL FROM:<%s@%s>", pw->pw_name, sio->localhost());
  34.     
  35.     for (int i=2; i< ac; i++) {
  36.     send_cmd(sio, sender);
  37.     send_cmd(sio, rcpt);
  38.     send_cmd(sio, "DATA");
  39.     sio << "\r\n------------------------" << av[i]
  40.         << "------------------------\r\n";
  41.     
  42.     ifstream cin(av[i]);
  43.     char buf[512];
  44.     while(cin.getline(buf, 511)) {
  45.         if (buf[0] == '.' && buf[1] == 0) {
  46.         cerr << av[0]
  47.              << ": char '.' on a line of its own\n";
  48.         return 1;
  49.         }
  50.         sio <<  buf << "\r\n";
  51.     }
  52.     sio << "\r\n.\r\n";
  53.     send_cmd(sio, 0);
  54.     }
  55.     send_cmd(sio, "QUIT");
  56. }
  57.  
  58. int send_cmd(iosockstream& s, const char* cmd)
  59. {
  60.     char buf[256];
  61.     if (cmd) s << cmd << "\r\n";
  62.     s.getline(buf, 255);
  63.     cout << buf << endl;
  64.     return 0;
  65. }
  66.