Beispiel #1
0
/**
 * Creates a signed message with gpg and takes into 
 * account the correct MIME message types to add to 
 * the message.
**/
static dstrbuf *
createGpgEmail(dstrbuf *msg, GpgCallType gpg_type)
{
	dstrbuf *tmpbuf=DSB_NEW;
	dstrbuf *gpgdata=NULL, *buf=DSB_NEW;
	dstrbuf *border1=NULL, *border2=NULL;

	assert(msg != NULL);

	/* Create two borders if we're attaching files */
	border1 = mimeMakeBoundary();
	if (Mopts.attach) {
		border2 = mimeMakeBoundary();
	} else {
		border2 = DSB_NEW;
	}

	if (makeGpgMessage(msg, tmpbuf, border2->str) < 0) {
		dsbDestroy(buf);
		buf=NULL;
		goto end;
	}

	gpgdata = callGpg(tmpbuf, gpg_type);
	if (!gpgdata) {
		dsbDestroy(buf);
		buf=NULL;
		goto end;
	}
	printHeaders(border1->str, buf, IS_ASCII);

	dsbPrintf(buf, "\r\n--%s\r\n", border1->str);
	if (gpg_type & GPG_ENC) {
		dsbPrintf(buf, "Content-Type: application/pgp-encrypted\r\n\r\n");
		dsbPrintf(buf, "Version: 1\r\n");
		dsbPrintf(buf, "\r\n--%s\r\n", border1->str);
		dsbPrintf(buf, "Content-type: application/octet-stream; "
			       "name=encrypted.asc\r\n\r\n");
	} else if (gpg_type & GPG_SIG) {
		dsbPrintf(buf, "%s\r\n", tmpbuf->str);
		dsbPrintf(buf, "\r\n--%s\r\n", border1->str);
		dsbPrintf(buf, "Content-Type: application/pgp-signature\r\n");
		dsbPrintf(buf, "Content-Description: This is a digitally signed message\r\n\r\n");
	} 
	dsbPrintf(buf, "%s", gpgdata->str);
	dsbPrintf(buf, "\r\n--%s--\r\n", border1->str);

end:
	dsbDestroy(tmpbuf);
	dsbDestroy(gpgdata);
	dsbDestroy(border1);
	dsbDestroy(border2);
	return buf;
}
Beispiel #2
0
/**
 * Creates a plain text (or html) email and 
 * specifies the necessary MIME types if needed
 * due to attaching base64 files.
 * when this function is done, it will rewind
 * the file position and return an open file
**/
static dstrbuf *
createPlainEmail(dstrbuf *msg) 
{
	dstrbuf *border=NULL;
	dstrbuf *buf=DSB_NEW;
	CharSetType cs;

	if (Mopts.attach) {
		border = mimeMakeBoundary();
	} else {
		border = DSB_NEW;
	}

	if (Mopts.encoding) {
		cs = getCharSet((u_char *)msg->str);
	} else {
		cs = IS_ASCII;
	}
	printHeaders(border->str, buf, cs);
	if (makeMessage(msg, buf, border->str, cs) < 0) {
		dsbDestroy(buf);
		buf=NULL;
	}
	dsbDestroy(border);
	return buf;
}
Beispiel #3
0
int mail_SendMessage( KMail mail, KMsg msg )
{
    Pair addr;
    int rc = 1;
    string out = NULL;
    string related = NULL;
    string multipart = NULL;
    char mp_boundary[36];
    char r_boundary[36];

    if( !smtp_MAIL_FROM( mail->smtp, A_EMAIL(msg->from) ) )
    {
        rc = 0;
        goto pmend;
    }

    addr = lfirst( msg->to );
    while( addr )
    {
        if( !smtp_RCPT_TO( mail->smtp, A_EMAIL(addr) ) )
        {
            rc = 0;
            goto pmend;
        }
        addr = lnext( msg->to );
    }
    addr = lfirst( msg->cc );
    while( addr )
    {
        if( !smtp_RCPT_TO( mail->smtp, A_EMAIL(addr) ) )
        {
            rc = 0;
            goto pmend;
        }
        addr = lnext( msg->cc );
    }

    addr = lfirst( msg->bcc );
    while( addr )
    {
        if( !smtp_RCPT_TO( mail->smtp, A_EMAIL(addr) ) )
        {
            rc = 0;
            goto pmend;
        }
        addr = lnext( msg->bcc );
    }

    out = msg_CreateHeaders( msg );
    if( !out )
    {
        rc = 0;
        goto pmend;
    }

    if( mail->flags & KMAIL_VERBOSE_MSG )
    {
        fprintf( stderr, "%s", sstr( out ) );
    }

    if( !smtp_DATA( mail->smtp ) || !smtp_write( mail->smtp, sstr( out ) ) )
    {
        rc = 0;
        goto pmend;
    }

    sdel( out );
    out = msg_CreateBody( msg );
    if( !out )
    {
        rc = 0;
        goto pmend;
    }

    if( mail->flags & KMAIL_VERBOSE_MSG )
    {
        fprintf( stderr, "%s", sstr( out ) );
    }

    if( msg->efiles->size )
    {
        mimeMakeBoundary( r_boundary );
        related = sfromchar( "Content-Type: multipart/related; boundary=\"" );
        if( !related || !xscatc( related, r_boundary, "\"\r\n\r\n", NULL ) )
        {
            rc = 0;
            goto pmend;
        }
    }

    if( msg->afiles->size )
    {
        mimeMakeBoundary( mp_boundary );
        multipart = sfromchar( "Content-Type: multipart/mixed; boundary=\"" );
        if( !multipart
                || !xscatc( multipart, mp_boundary, "\"\r\n\r\n", NULL ) )
        {
            rc = 0;
            goto pmend;
        }
        if( !smtp_write( mail->smtp, sstr( multipart ) ) )
        {
            rc = 0;
            goto pmend;
        }
        if( slen(out) && !write_boundary( mail->smtp, mp_boundary ) )
        {
            rc = 0;
            goto pmend;
        }

        if( related )
        {
            if( !smtp_write( mail->smtp, sstr( related ) )
                    || !write_boundary( mail->smtp, r_boundary )
                    || !smtp_write( mail->smtp, sstr( out ) )
                    || !mail_EmbedFiles( mail, msg, r_boundary )
                    || !write_boundary_end( mail->smtp, r_boundary ) )
            {
                rc = 0;
                goto pmend;
            }
        }

        if( !mail_AttachFiles( mail, msg, mp_boundary )
                || !write_boundary_end( mail->smtp, mp_boundary ) )
        {
            rc = 0;
            goto pmend;
        }
    }
    else if( msg->efiles->size )
    {
        if( !smtp_write( mail->smtp, sstr( related ) )
                || !write_boundary( mail->smtp, r_boundary )
                || !smtp_write( mail->smtp, sstr( out ) )
                || !mail_EmbedFiles( mail, msg, r_boundary )
                || !write_boundary_end( mail->smtp, r_boundary ) )
        {
            rc = 0;
            goto pmend;
        }
    }
    else
    {
        if( slen( out ) && !smtp_write( mail->smtp, sstr( out ) ) )
        {
            rc = 0;
            goto pmend;
        }
    }

    pmend: sdel( out );
    sdel( related );
    sdel( multipart );
    smtp_END_DATA( mail->smtp );
    return rc;
}