On Thursday 28 March 2002 10:14 pm, Jim Dawson wrote: > Could you send me a copy of this or post it to a ftp or web site? I've > been looking (unsuccessfully) for such a utility for quite a while. >
OK , I added some error checking and it should be reasonably foolproof.
I am attaching the files clmail and clmail.c
If someone wants to put these up on a ftp site, OK by me.
I don't know if the list allows attachments so they might not go through.
su -
password
cp clmail /usr/local/bin/clmail
chmod 711 /usr/local/bin/clmail
exit
entering clmail with no arguements yields:
5 arguments are supported
useage: smtpserver TO FROM "subject" "message"
Note: The "subject" and "message" must be enclosed in "" if they contain
whitespace.
--
Gerald Waugh Linux user # 255245
http://www.frontstreetnetworks.com
New Haven, CT, United States of America
11:49pm up 7 days, 8:14, 2 users, load average: 1.21, 1.06, 1.01
/* ==================================================
* clmail.c - simple smtp command line email application
*
* By Gerald Waugh
* March 29, 2002
*
* to install:
* s u -
* password
* cp clmail /usr/local/bin/clmail
* chmod 711 /usr/local/bin/clmail
* exit
* entering clmail with no arguements yields:
* 5 arguments are supported
* useage: smtpserver TO FROM "subject" "message"
*
* Note: The "subject" and "message" must be enclosed in "" if they contain whitespace.
*
* =================================================== */
#include <netdb.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
void die (char *s)
{
printf("Error: %s\n",s);
}
//=============================================
int main(int argc, char ** argv) {
struct sockaddr_in address;
struct in_addr inaddr;
struct hostent * host;
int sock, nBytes;
int buffsize = 1024;
char Buff[buffsize];
strcpy(Buff, "");
//=====================================================
//============== C H E C K A R G S =================
//=====================================================
if (argc != 6) {
fprintf(stderr, "5 arguments are supported\n\
useage: smtpserver TO FROM \"subject\" \"message\"\n\0");
return 1;
}
/* If the argument can be converted to an IP, do so. If not, try
to look it up in DNS. */
if (inet_aton(argv[1], &inaddr))
host = gethostbyaddr((char *) &inaddr, sizeof(inaddr), AF_INET);
else
host = gethostbyname(argv[1]);
if (!host) {
/* We can't find an IP number */
herror("error looking up host");
exit(1);
}
//=========================================================
//=============== C R E A T E S O C K E T ===============
//=========================================================
if ((sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
die("socket");
exit(1);
}
address.sin_family = AF_INET;
address.sin_port = htons(25);
/* Take the first IP address associated with this hostname */
memcpy(&address.sin_addr, host->h_addr_list[0], sizeof(address.sin_addr));
if (connect(sock, (struct sockaddr *) &address, sizeof(address)))
die("connect");
//========================================================
//============== I N I T G A T E W A Y =================
//========================================================
nBytes = recv(sock, Buff, buffsize, 0);
// if(nBytes > 0)
// printf("%s\n", Buff);
//========================================================
strcpy(Buff, "HELO ");
strcat(Buff, argv[1]);
strcat(Buff, "\n");
send(sock, Buff, strlen(Buff), 0); // send 'mail from' command
// printf("%s\n", Buff);
nBytes = recv(sock, Buff, buffsize, 0);
if(nBytes > 0) {
if(Buff[0] == '5') {
Buff[nBytes] = '\0';
printf("Error: %s", Buff);
exit(1);
}
}
//========================================================
strcpy(Buff, "MAIL FROM: ");
strcat(Buff, argv[3]);
strcat(Buff, "\n");
send(sock, Buff, strlen(Buff), 0); // send 'mail from' command
// printf("%s\n", Buff);
nBytes = recv(sock, Buff, buffsize, 0);
if(nBytes > 0) {
if(Buff[0] == '5') {
Buff[nBytes] = '\0';
printf("Error: %s", Buff);
exit(1);
}
}
//========================================================
strcpy(Buff, "RCPT TO: ");
strcat(Buff, argv[2]);
strcat(Buff, "\n");
send(sock, Buff, strlen(Buff), 0); // send mail to command
// printf("%s\n", Buff);
nBytes = recv(sock, Buff, buffsize, 0);
if(nBytes > 0) {
if(Buff[0] == '5') {
Buff[nBytes] = '\0';
printf("Error: %s", Buff);
exit(1);
}
}
//========================================================
strcpy(Buff, "DATA\n");
send(sock, Buff, strlen(Buff), 0); // send DATA command
// printf("%s\n", Buff);
nBytes = recv(sock, Buff, buffsize, 0);
if(nBytes > 0) {
if(Buff[0] == '5') {
Buff[nBytes] = '\0';
printf("Error: %s", Buff);
exit(1);
}
}
//========================================================
strcpy(Buff, "To: ");
strcat(Buff, argv[2]);
strcat(Buff, "\n");
send(sock, Buff, strlen(Buff), 0); // send To command
// printf("%s\n", Buff);
strcpy(Buff, "Subject: ");
strcat(Buff, argv[4]);
strcat(Buff, "\n");
send(sock, Buff, strlen(Buff), 0); // send To command
// printf("%s\n", Buff);
strcpy(Buff, argv[5]);
strcat(Buff, "\r\n");
send(sock, Buff, strlen(Buff), 0); // send mesage
// printf("%s\n", Buff);
strcpy(Buff, "\r\n.\r\n");
send(sock, Buff, strlen(Buff), 0); // send END DATA '.' command
// printf("%s\n", Buff);
nBytes = recv(sock, Buff, buffsize, 0);
if(nBytes > 0) {
// if(Buff[0] == '5') {
Buff[nBytes] = '\0';
printf("%s",Buff);
// exit(1);
// }
}
//========================================================
strcpy(Buff, "QUIT\n");
send(sock, Buff, strlen(Buff), 0); // send quit command
// printf("%s\n", Buff);
nBytes = recv(sock, Buff, buffsize, 0);
// Buff[nBytes] = '\0';
// printf(
close(sock);
return 0;
}
clmail
Description: c executable file
Want to buy your Pack or Services from MandrakeSoft? Go to http://www.mandrakestore.com
