smtp.c
/*
** write MIME headers, encode attachment if needed
** and write to socket
** returns 0 on success -1 on failure
*/
write MIME headers, encode attachment if needed
and write to socket
returns 0 on success -1 on failure
write MIME headers encode attachment if needed and write to socket returns 0 on success - 1 on failure
int send_attachment(Attachment *a, const char *boundary)
{
    Attachment
        *encoded_attachment;
    encoded_attachment = get_encoded_attachment(a);
    if (encoded_attachment == NULL)
    {
        errorMsg("%s (%d) - Could not encode attachment %s",
                MFL,
                a->file_path);
        return(-1);
    }
    (void) snprintf(buf, bufsz,"--%s\r\n",boundary);
    write_to_socket(buf);
    if (a->charset && strncmp(a->charset,"none", 4) != 0)
    {
        (void) snprintf(buf, bufsz, "Content-Type: %s; charset=%s\r\n",
                a->mime_type,
                a->charset);
    }
    else
    {
        if (a->attachment_name)
        {
            (void) snprintf(buf, bufsz, "Content-Type: %s; name=\"%s\"\r\n",
                    a->mime_type,
                    a->attachment_name);
        }
        else
        {
            (void) snprintf(buf, bufsz, "Content-Type: %s\r\n",a->mime_type);
        }
    }
    write_to_socket(buf);
    if (a->content_id == NULL)
    {
        if (strncmp(a->content_disposition, "inline", 6) == 0)
        {
            (void) snprintf(buf, bufsz, "Content-Disposition: %s\r\n",a->content_disposition);
        }
        else
        {
            if (a->attachment_name)
            {
                (void) snprintf(buf, bufsz, "Content-Disposition: %s; filename=\"%s\"\r\n",
                    a->content_disposition,
                    a->attachment_name);
            }
            else if (a->file_name)
            {
                (void) snprintf(buf, bufsz, "Content-Disposition: %s; filename=\"%s\"\r\n",
                    a->content_disposition,
                    a->file_name);
            }
            else
            {
                (void) snprintf(buf, bufsz, "Content-Disposition: %s\r\n",a->content_disposition);
            }
        }
        write_to_socket(buf);
    }
    if (a->content_id)
    {
        (void) snprintf(buf, bufsz, "Content-ID: <%s>\r\n",a->content_id);
        write_to_socket(buf);
        (void) snprintf(buf, bufsz, "X-Attachment-Id: %s\r\n",a->content_id);
        write_to_socket(buf);
    }
    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);
    }
    else
    {
        write_to_socket("\r\n");
    }
    while (fgets(buf, bufsz, a->fp_read))
    {
        write_to_socket(buf);
    }
    cleanup_attachment(a);
    return(0);
}
int send_attachment ( Attachment * a , const char * boundary ) { Attachment * encoded_attachment ; encoded_attachment = get_encoded_attachment ( a ) ; if ( encoded_attachment == NULL ) { errorMsg ( " " , MFL , a -> file_path ) ; return ( -1 ) ; } ( void ) snprintf ( buf , bufsz , " \r \n " , boundary ) ; write_to_socket ( buf ) ; if ( a -> charset && strncmp ( a -> charset , " " , 4 ) != 0 ) { ( void ) snprintf ( buf , bufsz , " \r \n " , a -> mime_type , a -> charset ) ; } else { if ( a -> attachment_name ) { ( void ) snprintf ( buf , bufsz , " \" \" \r \n " , a -> mime_type , a -> attachment_name ) ; } else { ( void ) snprintf ( buf , bufsz , " \r \n " , a -> mime_type ) ; } } write_to_socket ( buf ) ; if ( a -> content_id == NULL ) { if ( strncmp ( a -> content_disposition , " " , 6 ) == 0 ) { ( void ) snprintf ( buf , bufsz , " \r \n " , a -> content_disposition ) ; } else { if ( a -> attachment_name ) { ( void ) snprintf ( buf , bufsz , " \" \" \r \n " , a -> content_disposition , a -> attachment_name ) ; } else if ( a -> file_name ) { ( void ) snprintf ( buf , bufsz , " \" \" \r \n " , a -> content_disposition , a -> file_name ) ; } else { ( void ) snprintf ( buf , bufsz , " \r \n " , a -> content_disposition ) ; } } write_to_socket ( buf ) ; } if ( a -> content_id ) { ( void ) snprintf ( buf , bufsz , " \r \n " , a -> content_id ) ; write_to_socket ( buf ) ; ( void ) snprintf ( buf , bufsz , " \r \n " , a -> content_id ) ; write_to_socket ( buf ) ; } if ( strncmp ( a -> content_transfer_encoding , " " , 4 ) != 0 ) { ( void ) snprintf ( buf , bufsz , " \r \n \r \n " , a -> content_transfer_encoding ) ; write_to_socket ( buf ) ; } else { write_to_socket ( " \r \n " ) ; } while ( fgets ( buf , bufsz , a -> fp_read ) ) { write_to_socket ( buf ) ; } cleanup_attachment ( a ) ; return ( 0 ) ; }
write MIME headers, encode attachment if needed
and write to socket
returns 0 on success -1 on failure
write MIME headers encode attachment if needed and write to socket returns 0 on success - 1 on failure
/* our FILE pointer is already open and read to read from */ /* close file, remove tmp file if needed */