/* Copyright (c) 2000
W. M. Shandruk <walt@erudition.net>. All rights are reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this software
must display the following acknowledgement:
This product includes software developed by W. M. Shandruk.
4. The name of W. M. Shandruk may not be used to endorse or promote
products derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY W. M. SHANDRUK ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
NO EVENT SHALL W. M. SHANDRUK BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
#include "config.h"
#include "monitord.h"
int mail ( char *address, char *eserver, char *subject, char *message ) {
int serversd;
char *buf;
char *token;
char *username;
char *hostname;
char *thisname;
struct sockaddr_in servaddr;
struct hostent *host;
buf = (char *) malloc ( (size_t) _BUFSIZE ); // init the all purpose buffer
token = (char *) malloc ( (size_t) _BUFSIZE ); // init the token buffer
username = (char *) malloc ( (size_t) _BUFSIZE ); // init the username buffer
hostname = (char *) malloc ( (size_t) _BUFSIZE ); // init the hostname buffer
thisname = (char *) malloc ( (size_t) _BUFSIZE ); // init the hostname buffer
bzero (buf, sizeof (buf) );
bzero (token, sizeof (token) );
bzero (username, sizeof (username) );
bzero (hostname, sizeof (hostname) );
bzero (thisname, sizeof (thisname) );
strncpy (buf, address, _BUFSIZE);
username = strtok (buf, "@");
hostname = strtok (NULL, "@");
gethostname (thisname, _BUFSIZE);
/* create socket, if this fails, return value -1 */
if((serversd = socket(AF_INET, SOCK_STREAM, 0)) == -1) return (-1);
/* get IP of mail server */
host = gethostbyname( eserver );
/* fill out servaddr structure for connect() */
bzero( &servaddr, sizeof( servaddr ) );
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons( 25 );
servaddr.sin_addr.s_addr = *(long *)(host->h_addr);
/* attempt connexion to mail server. If this fails, return value -2 */
if(connect(serversd, (struct sockaddr *) &servaddr, sizeof(struct sockaddr)) == -1) return (-2);
read (serversd, buf, _BUFSIZE);
// Set "HELO Protocol"
sprintf(buf, "HELO %s\n", thisname);
write(serversd, buf, strlen(buf));
read (serversd, buf, _BUFSIZE);
// Set "MAIL FROM"
sprintf(buf, "MAIL FROM: <monitord-notification@%s>\n", thisname);
write(serversd, buf, strlen(buf));
read (serversd, buf, _BUFSIZE);
// set "RCPT TO"
bzero (buf, _BUFSIZE );
sprintf(buf, "RCPT TO: <%s>\n", address);
write(serversd, buf, strlen(buf));
read (serversd, buf, _BUFSIZE);
/* send "DATA" command */
bzero (buf, _BUFSIZE );
write(serversd, "DATA\n", 5);
read (serversd, buf, _BUFSIZE);
// Set "FROM"
sprintf(buf, "From: <monitord-notification@%s>\n", thisname);
write(serversd, buf, strlen(buf));
// set "TO"
bzero (buf, _BUFSIZE );
sprintf(buf, "To: <%s>\n", address);
write(serversd, buf, strlen(buf));
// set "SUBJECT"
bzero (buf, _BUFSIZE );
sprintf(buf, "Subject: %s\n", subject);
write(serversd, buf, strlen(buf));
// send actual data
bzero (buf, _BUFSIZE );
sprintf(buf, "%s\n.\n", message);
write(serversd, buf, strlen(buf));
bzero (buf, _BUFSIZE );
read (serversd, buf, _BUFSIZE);
// send "QUIT" command
bzero (buf, _BUFSIZE );
write(serversd, "QUIT\n", 5);
read (serversd, buf, _BUFSIZE);
return (0);
}
syntax highlighted by Code2HTML, v. 0.9.1