Ejemplo n.º 1
0
/*
** Print multipart/mixed or multipart/related header
** if mixed and embedded images are specified, make
** to use multipart/alternative for the embedded images.
*/
int print_content_type_header(const char *boundary)
{
    Sll
        *oneline_attachment_list,
        *attachment_list,
        *embed_image_list;

    (void) snprintf(buf, bufsz,"MIME-Version: 1.0\r\n");
    write_to_socket(buf);

    if (*g_content_type!='\0')
    {
      (void) snprintf(buf,sizeof(buf)-1,"Content-Type: %s; boundary=\"%s\"\r\n",
              g_content_type,
              boundary);
      write_to_socket(buf);
      return(0);
    }

    oneline_attachment_list = get_oneline_attachment_list();
    attachment_list = get_attachment_list();
    embed_image_list = get_embed_image_attachment_list();
    if (oneline_attachment_list || attachment_list)
    {
        (void) snprintf(buf,sizeof(buf)-1,
                "Content-Type: multipart/mixed; boundary=\"%s\"\r\n", boundary);
        write_to_socket(buf);
        return(0);
    }
    if (embed_image_list)
    {
        (void) snprintf(buf,sizeof(buf)-1,
                "Content-Type: multipart/related; boundary=\"%s\"\r\n", boundary);
        write_to_socket(buf);
        return(0);
    }
    write_to_socket("\r\n");
    return(0);
}
Ejemplo n.º 2
0
int process_embeded_images(const char *boundary)
{
    char
        *ib,
        *b,
        related[17],
        cid[17],
        tbuf[24],
        alternative[17];

    Attachment
        *a;

    Sll
        *il,
        *oneline_attachment_list,
        *attachment_list,
        *embed_image_list;

    int
        rc,
        ic = 1;

    oneline_attachment_list = get_oneline_attachment_list();
    attachment_list = get_attachment_list();

    embed_image_list = get_embed_image_attachment_list();
    if (embed_image_list == NULL)
    {
        return(0);
    }
    memset(related, 0, sizeof(related));
    memset(alternative, 0, sizeof(alternative));

    mutilsGenerateMIMEBoundary(related,sizeof(related));
    mutilsGenerateMIMEBoundary(alternative,sizeof(alternative));
    b = boundary;
    ib = boundary;

    (void) snprintf(buf, bufsz, "--%s\r\n",boundary);
    write_to_socket(buf);

    if (attachment_list || oneline_attachment_list)
    {
        b = related;
        ib = b;
        (void) snprintf(buf, bufsz, "Content-Type: multipart/related; boundary=%s\r\n",b);
        write_to_socket(buf);

        (void) snprintf(buf, bufsz, "Content-Type: multipart/alternative; boundary=%s\r\n\r\n",
                alternative);
        write_to_socket(buf);

        (void) snprintf(buf, bufsz, "--%s\r\n",b);
        write_to_socket(buf);
    }
    else
    {
        ib = alternative;
        (void) snprintf(buf, bufsz, "Content-Type: multipart/alternative; boundary=%s\r\n\r\n",
                alternative);
        write_to_socket(buf);

        (void) snprintf(buf, bufsz, "--%s\r\n",alternative);
        write_to_socket(buf);
    }

    write_to_socket("Content-Type: text/html; charset=ISO-8859-1\r\n\r\n");

    /* write the img tags with cid */
    embed_image_list = get_embed_image_attachment_list();
    for (il = embed_image_list; il; il = il->next)
    {
        a = (Attachment *) il->data;
        mutilsGenerateMIMEBoundary(cid,sizeof(cid));
        (void) snprintf(tbuf,sizeof(tbuf)-1,"ii%d_%s",ic,cid);
        a->content_id = xStrdup(tbuf);
        (void) snprintf(buf, bufsz, "<img src=\"cid:%s\" alt=\"inline image %d\"><br>\n",
                tbuf,
                ic);
        write_to_socket(buf);
        ic++;
    }
    write_to_socket("\r\n");
    (void) snprintf(buf, bufsz, "--%s--\r\n",ib);
    write_to_socket(buf);

    for (il = embed_image_list; il; il = il->next)
    {
        a = (Attachment *) il->data;
        if (a == NULL)
            continue;
        rc = send_attachment(a,b);
        RETURN_IF_NOT_ZERO(rc);
    }
    (void) snprintf(buf, bufsz, "--%s--\r\n",b);
    write_to_socket(buf);
    return(0);

ExitProcessing:
    return(-1);
}
Ejemplo n.º 3
0
/*
** send one line messages, each one is an inline attachment
** return 0 if mail is sent, -1 otherwise
*/
int process_oneline_messages(const char *boundary)
{
    int
        n = (-1);

    Attachment
        *a = NULL;
    Sll
        *l,
        *oneline_attachment_list;

    oneline_attachment_list = get_oneline_attachment_list();
    if (oneline_attachment_list == NULL)
    {
        return(0);
    }
    print_oneline_attachment_list();

    for (l = oneline_attachment_list; l; l = l->next)
    {
        a = (Attachment *) l->data;
        (void) snprintf(buf, bufsz, "\r\n--%s\r\n",boundary);
        write_to_socket(buf);

        if (strcmp(a->charset,"none") != 0)
        {
            (void) snprintf(buf, bufsz, "Content-Type: %s; charset=%s\r\n",
                a->mime_type,
                a->charset);
        }
        else
        {
            (void) snprintf(buf, bufsz, "Content-Type: %s\r\n",a->mime_type);
        }
        write_to_socket(buf);

        (void) strcpy(buf,"Content-Disposition: inline\r\n");
        write_to_socket(buf);

        /* add encoding type if needed */
        if (strncmp(a->content_transfer_encoding,"none",4) != 0)
        {
            (void) snprintf(buf, bufsz, "Content-Transfer-Encoding: %s\r\n\r\n",
                    a->content_transfer_encoding);
            write_to_socket(buf);
        }

        
        if (strncmp(a->content_transfer_encoding,"base64",6) == 0)
        {
            /* encode the mssage to base 64 and write to socket */
            encode2base64andwrite2socket(a->oneline_msg);
        }
        else
        {
            write_to_socket(a->oneline_msg);
            if (g_show_attachment_in_log)
            {
                showVerbose("[C] %s\n",a->oneline_msg);

            }
        }
        write_to_socket("\r\n");
    }
    return(0);

ExitProcessing:
    return(-1);
}
Ejemplo n.º 4
0
/* SMTP: mail */
static int smtpMail(int sfd,char *to,char *cc,char *bcc,char *from,char *rrr,char *rt,
                    char *subject,char *attach_file,char *msg_body_file,
                    char *the_msg,int is_mime,int add_dateh)
{
    char
        *os="Unix",
        boundary[17],
        related[17],
        alternative[17],
        mbuf[1024];

    int
        newline_before;

    Sll
        *oneline_attachment_list,
        *attachment_list,
        *embed_image_list;

#ifdef WINNT
    os="Windows";
#else
    os="Unix";
#endif /* WINNT */

    memset(boundary, 0, sizeof(boundary));
    memset(related, 0, sizeof(related));
    memset(alternative, 0, sizeof(alternative));

    attachment_list=get_attachment_list();
    embed_image_list = get_embed_image_attachment_list();
    oneline_attachment_list = get_oneline_attachment_list();
    if (attachment_list || embed_image_list || oneline_attachment_list)
    {
        is_mime=1;
    }

    if (subject)
    {
        memset(buf,0,sizeof(buf));
        (void) snprintf(buf,sizeof(buf)-1,"Subject: %s\r\n",subject);

        msock_puts(buf);

        showVerbose(buf);
    }

    /* headers */
    if (from)
    {
        memset(buf,0,sizeof(buf));
        if (*g_from_name != '\0')
        {
            /* Name in From: */

            memset(buf,0,sizeof(buf));
            (void) snprintf(buf,sizeof(buf)-1,"From: %s <%s>\r\n",
                            g_from_name,from);
        }
        else
        {
            (void) snprintf(buf,sizeof(buf)-1,"From: %s\r\n",from);
        }
        msock_puts(buf);

        showVerbose(buf);
    }

    if (add_dateh)
    {
        /* add Date: header */
        char
            datebuf[65];

        memset(datebuf,0,sizeof(datebuf));
        if (rfc822_date(time(NULL),datebuf,sizeof(datebuf)-1) == 0)
        {
            memset(buf,0,sizeof(buf));
            (void) snprintf(buf,sizeof(buf)-1,"Date: %s\r\n",datebuf);
            msock_puts(buf);

            showVerbose(buf);
        }
    }
    
    if (to)
    {
        memset(buf,0,sizeof(buf));
        (void) snprintf(buf,sizeof(buf)-1,"To: %s\r\n",to);
        msock_puts(buf);

        showVerbose(buf);

    }

    if (cc)
    {
        memset(buf,0,sizeof(buf));
        (void) snprintf(buf,sizeof(buf)-1,"Cc: %s\r\n",cc);
        msock_puts(buf);
        showVerbose(buf);
    }

    /*
    if (bcc)
    {
        memset(buf,0,sizeof(buf));
        (void) snprintf(buf,sizeof(buf)-1,"Bcc: %s\r\n",bcc);
        msock_puts(buf);

        showVerbose(buf);
    }
    */

    if (rt != NULL)
    {
        memset(buf,0,sizeof(buf));
        (void) snprintf(buf,sizeof(buf)-1,"Reply-To: %s\r\n",rt);
        msock_puts(buf);
        showVerbose(buf);

    }
    if (rrr != NULL)
    {
        memset(buf,0,sizeof(buf));
        (void) snprintf(buf,sizeof(buf)-1,"Disposition-Notification-To: %s\r\n",rrr);
        msock_puts(buf);
        showVerbose(buf);
    }

    /* add custom headers if any. No verification is done */
    {
        Sll 
            *l,
            *custom_header_list;
        custom_header_list = get_custom_header_list();
        if (custom_header_list)
        {
            for (l = custom_header_list; l; l = l->next)
            {
                if (l->data)
                {
                    msock_puts((char *) l->data);
                    msock_puts("\r\n");
                    showVerbose((char *) l->data);
                    showVerbose("\r\n");
                }
            }
        }
    }


    memset(buf,0,sizeof(buf));
    (void) snprintf(buf,sizeof(buf)-1,"X-Mailer: %s (%s)\r\n",MAILSEND_VERSION,os);
    msock_puts(buf);
    showVerbose(buf);

    memset(buf,0,sizeof(buf));
    (void) snprintf(buf,sizeof(buf)-1,"X-Copyright: %s\r\n",NO_SPAM_STATEMENT);
    msock_puts(buf);
    showVerbose(buf);

    if (is_mime)
    {
        int
            rc;
        srand(time(NULL));
        memset(boundary,0,sizeof(boundary));
        mutilsGenerateMIMEBoundary(boundary,sizeof(boundary));

        /* if msg body file is specified, include and return */
        if (msg_body_file)
        {
            return (include_msg_body());
        }

        rc = print_content_type_header(boundary);
        RETURN_IF_NOT_ZERO(rc);

        rc = process_oneline_messages(boundary);
        RETURN_IF_NOT_ZERO(rc);

        rc = process_embeded_images(boundary);
        RETURN_IF_NOT_ZERO(rc);

        rc = process_attachments(boundary);
        RETURN_IF_NOT_ZERO(rc);

        /* handle MIME attachments ends */
        goto done;
    } /* is_mime */

    /* mail body */
    if (attach_file == NULL && the_msg == NULL) /* read from stdin */
    {

        /* if stdin is a terminal, print the instruction */
        if (isInConsole(_fileno(stdin)))
        {
            (void) printf("=========================================================================\n");
            (void) printf("Type . in a new line and press Enter to end the message, CTRL+C to abort\n");
            (void) printf("=========================================================================\n");
        }

#ifdef WINNT
        SetConsoleCtrlHandler(CntrlHandler,TRUE);
#endif /* WINNT */

        newline_before=1;
        while (fgets(mbuf,sizeof(mbuf)-1,stdin) && (break_out == 0))
        {
            if (newline_before && *mbuf == '.')
            {
                break;
            }
            else
            {
                int
                    len;
                /* vinicio qmail fix */
                len=strlen(mbuf);
                if (mbuf[len-1] != '\n')
                    strcat(mbuf,"\r\n");
                else
                {
                   mbuf[--len]='\0';
                   strcat(mbuf,"\r\n");
                }
                /* vinicio qmail fix */
                msock_puts(mbuf);
                showVerbose("[C] %s",mbuf);
            }
            newline_before=(*mbuf != '\0' && mbuf[strlen(mbuf)-1] == '\n');
            if (break_out == 1)
            {
                (void) fprintf(stderr," Breaking out\n");
                return (0);
            }
        }
    }
done:

    return (0);
}