forked from spmfilter/libcmime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_mime_message_part_from_file.c
110 lines (89 loc) · 3.18 KB
/
create_mime_message_part_from_file.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "../src/cmime_message.h"
void usage() {
printf("\n");
printf("libcmime - simple api demonstration\n");
printf("-----------------------------------\n");
printf("demonstrates: manual creation of new CMimeMessage_T mime message. Furthermore the message is manually extended by an attachment\n");
printf("output: output is written to stdout if no output file is specified (-f /path/to/out_file.txt)\n");
printf("\n");
}
int main(int argc, char *argv[]) {
// addresses can either be specified "just as an address" or like "John Doe <[email protected]>"
char from[] = "[email protected]";
char to[] = "[email protected]";
char *file = NULL;
char *out = NULL;
char *msgid = NULL;
char *attachment = NULL;
int option;
int retval = 0;
// check command line parameters
while((option = getopt(argc,argv,"hf:a:")) != EOF) {
switch(option) {
case 'f':
asprintf(&file, "%s", optarg);
break;
case 'a':
asprintf(&attachment, "%s", optarg);
break;
case 'h':
usage();
break;
default:
usage();
}
}
if(attachment != NULL) {
CMimeMessage_T *message = cmime_message_new();
CMimePart_T *part = cmime_part_new();
// set the sender of the message
cmime_message_set_sender(message,from);
// add an recipient, this can also be done with cmime_message_add_recipient_to
cmime_message_add_recipient(message, to, CMIME_ADDRESS_TYPE_TO);
// set subject
cmime_message_set_subject(message, "This is an exmaple");
// generate date header
cmime_message_set_date_now(message);
// generate a message id and add it to our message
msgid = cmime_message_generate_message_id();
cmime_message_set_message_id(message, msgid);
// add content to the body
cmime_message_set_body(message,"This is the message body");
// add the attachment
cmime_message_add_generated_boundary(message);
cmime_part_from_file(&part, attachment,message->linebreak);
if (cmime_message_append_part(message, part) != 0) {
printf("failed to add mimepart\n");
}
// assign the email to out or write it to file (depending on cli options)
if(file != NULL) {
if(cmime_message_to_file(message,file) > 0) {
printf("file created: %s\n", file);
} else {
printf("error writing file: %s\n", file);
}
} else {
out = cmime_message_to_string(message);
printf("%s\n", out);
}
// free the initialized object
cmime_message_free(message);
} else {
printf("you have to specify an attachment with -a\n");
retval = -1;
}
// some clean up
if(out != NULL)
free(out);
if(msgid != NULL)
free(msgid);
if(file != NULL)
free(file);
if(attachment != NULL)
free(attachment);
return retval;
}