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