Mail send using c++

main.c==> 
#define __MAIN__    1

#include "mailsend.h"


/* exits after writing the usage */
static void usage(void)
{
    char
        **p;

    static char
        *options[]=
        {
            "   -d    domain*          - domain name for SMTP Helo",
            "   -smtp hostname/IP*    - of the SMTP server",
            "   -p    SMTP port       - SMTP port",
            "   -t    to,to..*        - email address/es of the reciepient/s",
            "   -cc   cc,cc..         - Carbon copy address/es",
            "   +cc                   - don't ask for Carbon Copy",
            "   -bc   bcc,bcc..       - Blind carbon copy address/es",
            "   +bc                   - don't ask for Blind carbon copy",
            "   +D                    - don't add Date header",
            "   -f    address*        - email address of the sender",
            "   -sub  subject         - subject",
            "   -l    file            - a file containing the email addresses",
            "   -a    file,mime_type  - attach this file",
            "   -cs   character set   - for text/plain attachments (default is us-ascii)",
            "   -m    file,mime_type,[i/a] (i=inline,a=attachment)",
            "                         - attach the file (text,html etc) as body (inline)",
            "   -M    \"one line msg\"  - attach this one line text message",
            "   -v                    - verbose mode",
            "   -V                    - show version info",
            "   -w                    - wait for a CR after sending the mail",
            "   -rt  email_address    - add Reply-To header",
            "   -rrr email_address    - request read receipts to this address",
            "   -help                 - shows this help",
            (char *) NULL
        };
    (void) printf("\n");
    (void) printf("Version: %.1024s\n\n",MAILSEND_VERSION);
    (void) printf("Copyright: %.1024s\n\n",NO_SPAM_STATEMENT);

    (void) printf("usage: mailsend [options]\n");
    (void) printf("Where the options are:\n");

    for (p=options; *p != NULL; p++)
        (void) printf("%s\n",*p);

    (void) fprintf(stdout,"\nThe options with * must the specified\n");
    (void) fprintf(stdout,"Example (Note: type without newline):\n");
    (void) fprintf(stdout,
"\n"
" mailsend -f muquit@example.com -d example.com -smtp 10.100.30.1\n"
"  -t muquit@muquit.com -sub test -a \"file.txt,text/plain\"\n"
"  -a \"/usr/file.gif,image/gif\" -a \"file.jpeg,image/jpg\"\n\n");

(void) fprintf(stdout,
" mailsend -f muquit@example.com -d example.com -smtp 192.168.0.2\n"
"  -t muquit@muquit.com -sub test +cc +bc\n"
"  -a \"c:\\file.gif,image/gif\" -M \"Sending a GIF file\"\n\n");

(void) fprintf(stdout,
" mailsend -f muquit@example.com -d example.com -smtp 192.168.0.2\n"
"  -t muquit@muquit.com -sub test +cc +bc -cs \"ISO-8859-1\"\n"
"  -a \"file2.txt,text/plain\"\n\n");

    (void) fprintf(stdout,"Change content disposition to inline:\n");
    (void) fprintf(stdout,
" mailsend -f muquit@example.com -d example.com -smtp 10.100.30.1\n"
"  -t muquit@muquit.com -sub test -a \"nf.jpg,image/jpeg,i\"\n"
"  -M \"content disposition is inline\"\n\n");
    exit(0);
}

int main(int argc,char **argv)
{
    char
        *x,
        buf[BUFSIZ],
        *option;

    int
        is_mime=0,
        add_dateh=1,
        port=(-1),
        rc,
        no_cc=0,
        no_bcc=0,
        i;

    char
        *address_file=NULL,
        *helo_domain=NULL,
        *smtp_server=NULL,
        *attach_file=NULL,
        *msg_body_file=NULL,
        *the_msg=NULL,
        *to=NULL,
        *save_to=NULL,
        *save_cc=NULL,
        *save_bcc=NULL,
        *from=NULL,
        *sub=NULL,
        *cc=NULL,
        *bcc=NULL,
        *rt=NULL,
        *rrr=NULL;

    g_verbose=0;
    g_wait_for_cr=0;
    (void) strcpy(g_charset,"us-ascii");

    for  (i=1; i < argc; i++)
    {
        option=argv[i];
        switch (*(option+1))
        {

            case 'a':
            {
                if (strncmp("attach",option+1,1) == 0)
                {
                    if (*option == '-')
                    {
                        i++;
                        if (i == argc)
                        {
                            errorMsg("Missing file to attach");
                            return (1);
                        }
                        attach_file=argv[i];
                        add_attachment_to_list(attach_file);
                    }
                }

                break;
            }

            case 'b':
            {
                if (strncmp("bcc",option+1,2) == 0)
                {
                    if (*option == '-')
                    {
                        i++;
                        if (i == argc)
                        {
                            errorMsg("Missing BCc address/es");
                            return (1);
                        }
                        bcc=argv[i];
                        save_bcc=mutilsStrdup(bcc);

                        /* collapse all spaces to a comma */
                        mutilsSpacesToChar(bcc,',');
                        addAddressToList(bcc,"Bcc");
                    }
                    else if (*option == '+')
                    {
                        no_bcc=1;
                    }
                }
                else
                {
                    errorMsg("Unknown flag: %s\n",option);
                    return(1);
                }
                break;
            }


            case 'c':
            {
                if (strncmp("cc",option+1,2) == 0)
                {
                    if (*option == '-')
                    {
                        i++;
                        if (i == argc)
                        {
                            errorMsg("Missing Cc address/es");
                            return (1);
                        }
                        cc=argv[i];
                        save_cc=mutilsStrdup(cc);

                        /* collapse all spaces to a comma */
                        mutilsSpacesToChar(cc,',');
                        addAddressToList(cc,"Cc");
                    }
                    else if (*option == '+')
                    {
                        no_cc=1;
                    }
                }
                else if (strncmp("cs",option+1,2) == 0)
                {
                    if (*option == '-')
                    {
                        i++;
                        if (i == argc)
                        {
                            errorMsg("Missing character set");
                            return (1);
                        }
                        mutilsSafeStrcpy(g_charset,argv[i],sizeof(g_charset)-1);
                    }

                }
                else
                {
                    errorMsg("Unknown flag: %s\n",option);
                    return(1);
                }
                break;
            }

            case 'D':
            {
                if (strncmp("D",option+1,1) == 0)
                {
                    if (*option == '+')
                    {
                        add_dateh=0;
                    }
                }
                break;
            }

            case 'd':
            {
                if (strncmp("domain",option+1,1) == 0)
                {
                    if (*option == '-')
                    {
                        i++;
                        if (i == argc)
                        {
                            errorMsg("Missing domain name");
                            return (1);
                        }
                        helo_domain=argv[i];
                    }
                }
                break;
            }

            case 'f':
            {
                if (strncmp("from",option+1,1) == 0)
                {
                    if (*option == '-')
                    {
                        i++;
                        if (i == argc)
                        {
                            errorMsg("Missing From address/es");
                            return (1);
                        }
                        from=argv[i];
                    }
                }
                break;
            }

            case 'h':
            {
                if (strncmp("help",option+1,1) == 0)
                {
                    usage();
                }
                /* won't be here */
                break;
            }

            case 'l':
            {
                if (strncmp("list_address",option+1,1) == 0)
                {
                    if (*option == '-')
                    {
                        i++;
                        if (i == argc)
                        {
                            errorMsg("Missing address list file"); 
                            return (1);
                        }
                        address_file=argv[i];
                    }
                }
                break;
            }


            case 'p':
            {
                if (strncmp("port",option+1,1) == 0)
                {
                    if (*option == '-')
                    {
                        i++;
                        if (i == argc)
                        {
                            errorMsg("Missing SMTP Port");
                            return (1);
                        }
                        port=atoi(argv[i]);
                    }
                }
                break;
            }


            case 'm':
            {
                if (strncmp("msgbody",option+1,1) == 0)
                {
                    if (*option == '-')
                    {
                        i++;
                        if (i == argc)
                        {
                            errorMsg("Missing message body file");
                            return (1);
                        }
                        msg_body_file=argv[i];
                    }
                }
                break;
            }

            case 'M':
            {
                if (strncmp("Message",option+1,1) == 0)
                {
                    if (*option == '-')
                    {
                        i++;
                        if (i == argc)
                        {
                            errorMsg("Missing text message");
                            return (1);
                        }
                        the_msg=argv[i];
                    }
                }
                break;
            }

            case 's':
            {
                if (strncmp("smtp",option+1,3) == 0)
                {
                    if (*option == '-')
                    {
                        i++;
                        if (i == argc)
                        {
                            errorMsg("Missing smtp server");
                            return (1);
                        }
                        smtp_server=argv[i];
                    }
                }
                else if (strncmp("subject",option+1,3) == 0)
                {
                    if (*option == '-')
                    {
                        i++;
                        if (i == argc)
                        {
                            errorMsg("Missing subject");
                            return (1);
                        }
                        sub=argv[i];
                    }
                }
                else
                {
                    errorMsg("Unknown flag: %s\n",option);
                    return(1);
                }

                break;
            }

            case 'v':
            {
                if (strncmp("verbose",option+1,1) == 0)
                {
                    if (*option == '-')
                    {
                        g_verbose=1;
                    }
                }
                break;
            }

            case 'V':
            {
                (void) fprintf(stderr,"mailsend Version: %.1024s\n",MAILSEND_VERSION);
                exit(0);
                break;
            }

           case 't':
           {
                if (strncmp("to",option+1,1) == 0)
                {
                    if (*option == '-')
                    {
                        i++;
                        if (i == argc)
                        {
                            (void) fprintf(stderr,"Error: missing to addresses\n");
                            return (1);
                        }
                        to=argv[i];
                        save_to=mutilsStrdup(to);
                        if (save_to == NULL)
                        {
                            errorMsg("memory allocation problem for -to");
                            return(-1);
                        }
                        save_to=fix_to(save_to);
                        to=fix_to(to);
                        /* collapse all spaces to a comma */
                        mutilsSpacesToChar(to,',');

                        /* add addresses to a singly linked list */
                        addAddressToList(to,"To");
                        /* Note: to is modifed now! */
                    }
                }
                break;
            }

            case 'r':
            {
                if (strncmp("rrr",option+1,3) == 0)
                {
                    if (*option == '-')
                    {
                        i++;
                        if (i == argc)
                        {
                            (void) fprintf(stderr,"Error: missing to addresses for -rrr\n");
                            return (1);
                        }
                        rrr=mutilsStrdup(argv[i]);
                        if (rrr == NULL)
                        {
                            errorMsg("memory allocation problem for -rrr");
                            return(1);
                        }
                    }
                }
                else if (strncmp("rt",option+1,2) == 0)
                {
                    if (*option == '-')
                    {
                        i++;
                        if (i == argc)
                        {
                            (void) fprintf(stderr,"Error: missing to addresses for -rt\n");
                            return (1);
                        }
                        rt=mutilsStrdup(argv[i]);
                        if (rt == NULL)
                        {
                            errorMsg("memory allocation problem for -rt");
                            return(1);
                        }
                    }
                }
                else
                {
                    errorMsg("Unknown flag: %s\n",option);
                    return(1);
                }

                break;
            }

            case 'w':
            {
                if (strncmp("wait",option+1,1) == 0)
                {
                    if (*option == '-')
                    {
                        g_wait_for_cr=1;
                    }
                }

                break;
            }

            default:
            {
                (void) fprintf(stderr,"Error: Unrecognized option: %s\n",
                               option);
                return (1);
            }

        }
    }
    print_attachemtn_list();

    /*
    if (the_msg && txt_msg_file)
    {
        (void) fprintf(stderr,"\nError: -m and -M can not be used together\n");
        return(1);
    }
  
    if (attach_file || the_msg || msg_body_file)
    {
        is_mime=1;
    }

    if (smtp_server == NULL)
    {
        memset(buf,0,sizeof(buf));
        x=askFor(buf,sizeof(buf)-1,"SMTP server address/IP: ",EMPTY_NOT_OK);
        if (x)
            smtp_server=xStrdup(x);
    }

    if (helo_domain == NULL)
    {
        memset(buf,0,sizeof(buf));
        x=askFor(buf,sizeof(buf)-1,"Domain: ",EMPTY_NOT_OK);
        if (x)
            helo_domain=xStrdup(x);
    }

    if (from == NULL)
    {
        memset(buf,0,sizeof(buf));
        x=askFor(buf,sizeof(buf)-1,"From: ",EMPTY_NOT_OK);
        if (x)
            from=xStrdup(x);
    }



  
    if (save_to == NULL)
    {
        memset(buf,0,sizeof(buf));
        x=askFor(buf,sizeof(buf)-1,"To: ",EMPTY_NOT_OK);
        if (x)
        {
            save_to=xStrdup(x);
            addAddressToList(x,"To");
        }
    }

    
#ifdef WINNT
    if (attach_file == NULL && isInConsole(_fileno(stdin)))
#else
    if (attach_file == NULL && isatty(fileno(stdin)))
#endif /* WINNT */
    {
        if (save_cc == NULL && !no_cc)
        {
            memset(buf,0,sizeof(buf));
            x=askFor(buf,sizeof(buf)-1,"Carbon copy: ",EMPTY_OK);
            if (x)
            {
                save_cc=xStrdup(x);
                addAddressToList(x,"Cc");
            }
        }

        if (save_bcc == NULL && ! no_bcc)
        {
            memset(buf,0,sizeof(buf));
            x=askFor(buf,sizeof(buf)-1,"Blind Carbon copy: ",EMPTY_OK);
            if (x)
            {
                save_bcc=xStrdup(x);
                addAddressToList(x,"BCc");
            }
        }

        if (sub == NULL)
        {
            memset(buf,0,sizeof(buf));
            x=askFor(buf,sizeof(buf)-1,"Subject: ",EMPTY_OK);
            if (x)
                sub=xStrdup(x);
        }
    }

    /* if address file specified, add the addresses to the list as well */
    if (address_file != NULL)
        addAddressesFromFileToList(address_file);

    printAddressList();

    /* TODO: read from default file or registry */
    rc=validateMusts(from,save_to,smtp_server,helo_domain);
    if (rc == -1)
        return (1); /* exit */

#ifdef UNIX
    signal(SIGPIPE,SIG_IGN);
#endif /* UNIX */

    rc=send_the_mail(from,save_to,save_cc,save_bcc,sub,smtp_server,port,
                helo_domain,attach_file,msg_body_file,the_msg,is_mime,rrr,rt,add_dateh);

    if (rc == 0)
    {
        if (isInteractive())
        {
            (void) printf("Mail sent successfully\n");
            (void) fflush(stdout);
        }
    }
    else
    {
        if (isInteractive())
        {
            (void) printf("Could not send mail\n");
        }
    }

    if (isInteractive())
    {
        if (g_wait_for_cr)
        {
            printf("\nPress Enter to Exit: ");
            fgets(buf,sizeof(buf)-1,stdin);
        }
    }


    return (rc);
}

mailsend.h==>

#ifndef MAILSEND_H
#define MAILSEND_H

#include <stdio.h>
#include <string.h>
#include <stdarg.h>

#include <math.h>

#include "mutils.h"
#include "msock.h"
#include "sll.h"

#ifdef UNIX
#include <signal.h>
#endif /* UNIX */

#define MFL __FILE__,__LINE__

#define MAILSEND_VERSION    "@(#) mailsend v1.14"
#define MAILSEND_PROG       "mailsend"
#define NO_SPAM_STATEMENT   "GNU GPL. It is illegal to use this software for Spamming"

#define MAILSEND_SMTP_PORT  25
#define MAILSEND_DEF_SUB    ""

#define A_SPACE ' '

#define EMPTY_OK        0x01
#define EMPTY_NOT_OK    0x02
#define ATTACHMENT_SEP  ','


#ifdef EXTERN
#undef EXTERN
#endif /* EXTERN */

#ifndef __MAIN__
#define EXTERN extern
#else 
#define EXTERN
#endif /* __MAIN__ */

#ifdef WINNT
#define snprintf _snprintf
#endif /* WINNT */

#define CFL __FILE__,__LINE__

#define CHECK_MALLOC(x) \
do \
{ \
    if (x == NULL) \
    { \
        (void) fprintf(stderr,"%s (%d) - Memory allocation failed\n",CFL); \
        exit(0); \
    }\
}while(0)

#define ERR_STR strerror(errno)

EXTERN int  g_verbose;
EXTERN int  g_wait_for_cr;
EXTERN char g_charset[33];

typedef struct _Address
{

    /*
    ** label holds strings like "To" "Cc" "Bcc". 
    ** The address is the email address.
    */

    char
        *label,     /* To: Cc: Bcc: */
        *address;   /* the email address */
}Address;

typedef struct _Attachment
{
    char
        *file_path,
        *file_name;
    char
        *mime_type;
    char
        *content_disposition;
}Attachment;

/* the mail sturct */
typedef struct _TheMail
{
    SOCKET
        fd;

    Address
        *address;

    char
        *from,
        *subject,
        *x_mailer,
        *smtp_server,
        *helo_domain,
        *msg_file;
} TheMail;


/* struct for $HOME/.mailsendrc */
typedef struct _Mailsendrc
{
    char
        *domain,
        *from,
        *smtp_server;
}Mailsendrc;

/* function prototypes */
char        *xStrdup(char *string);
int         addAddressToList(char *a,char *label);
TheMail     *initTheMail(void);
Address     *newAddress(void);
Sll         *getAddressList(void);
void        printAddressList(void);
int         send_the_mail(char *from,char *to,char *cc,char *bcc,char *sub,
                     char *smtp_server,int smtp_port,char *helo_domain,
                     char *attach_file,char *txt_msg_file,char *the_msg,
                     int is_mime,char *rrr,char *rt,int add_dateh);
TheMail     *newTheMail(void);
void        errorMsg(char *format,...);
void        showVerbose(char *format,...);
int         addAddressesFromFileToList(char *adress_list_file);
int         validateMusts(char *from,char *to,char *smtp_server,
                          char *helo_domain);
char        *askFor(char *buf,int buflen,char *label,int loop);
int         isInConsole(int fd);
int         add_attachment_to_list(char *file_path_mime);
Sll         *get_attachment_list();
void        print_attachemtn_list();
char        *fix_to(char *to);
int         isInteractive(void);
int         get_filepath_mimetype(char *str,char *filename,int fn_size,
                                  char *mype_type,int mt_size);
int         rfc822_date(time_t when,char *datebuf,int bufsiz);

#endif /* ! MAIL_SEND_H */

posted under |

0 comments:

Post a Comment

Newer Post Older Post Home

About

Cool na? :) Now Learn How to create one by Clicking here


Followers

About This Blog