Commit 89227684 892276842d0b34c94d42cd87b2ac36d92846b08e by Sam Roberts

Added msg-send example of using the mailer_t API.

1 parent c4d6a2e7
...@@ -9,7 +9,7 @@ LDLIBS = -lsocket ...@@ -9,7 +9,7 @@ LDLIBS = -lsocket
9 9
10 MULIBS = ../mailbox/.libs/libmailbox.a ../lib/libmailutils.a 10 MULIBS = ../mailbox/.libs/libmailbox.a ../lib/libmailutils.a
11 11
12 EXES = addr mbox-explode mbox-dates mbox-auth url-parse mbox-check mimetest 12 EXES = addr mbox-explode mbox-dates mbox-auth url-parse mbox-check mimetest msg-send
13 13
14 default: $(EXES) 14 default: $(EXES)
15 15
......
1 #include <sys/types.h>
2 #include <sys/stat.h>
3
4 #include <errno.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9
10 #include <mailutils/address.h>
11 #include <mailutils/mailer.h>
12 #include <mailutils/message.h>
13 #include <mailutils/registrar.h>
14 #include <mailutils/stream.h>
15
16 #define C(X) {int e; if((e = X) != 0) { \
17 fprintf(stderr, "%s failed: %s\n", #X, strerror(e)); \
18 exit(1); } }
19
20 const char USAGE[] =
21 "usage: mailer [-hd] [-m mailer] [-f from] [to]..."
22 ;
23 const char HELP[] =
24 " -h print this helpful message\n"
25 " -m a mailer URL (default is \"sendmail:\")\n"
26 " -f the envelope from address (default is from user environment)\n"
27 " to a list of envelope to addresses (default is from message)\n"
28 "\n"
29 "An RFC2822 formatted message is read from stdin and delivered using\n"
30 "the mailer.\n"
31 ;
32
33 int
34 main (int argc, char *argv[])
35 {
36 int opt;
37 int optdebug = 0;
38 char* optmailer = "sendmail:";
39 char* optfrom = 0;
40
41 stream_t in = 0;
42 message_t msg = 0;
43 mailer_t mailer = 0;
44 address_t from = 0;
45 address_t to = 0;
46
47 while((opt = getopt(argc, argv, "hdm:f:")) != -1)
48 {
49 switch(opt)
50 {
51 case 'h':
52 printf("%s\n%s", USAGE, HELP);
53 return 0;
54 case 'd':
55 optdebug++;
56 break;
57 case 'm':
58 optmailer = optarg;
59 break;
60 case 'f':
61 optfrom = optarg;
62 break;
63 default:
64 fprintf(stderr, "%s\n", USAGE);
65 break;
66 }
67 }
68
69 /* Register mailers. */
70 {
71 list_t bookie;
72 registrar_get_list (&bookie);
73 C( list_append (bookie, smtp_record) )
74 C( list_append (bookie, sendmail_record) )
75 }
76
77 if(optfrom)
78 {
79 C( address_create(&from, optfrom) )
80 }
81
82 if(argv[optind])
83 {
84 char** av = argv + optind;
85
86 C( address_createv(&to, (const char**) av, -1) )
87 }
88
89 C( stdio_stream_create(&in, stdin, 0) )
90
91 C( stream_open(in) )
92
93 C( message_create (&msg, NULL) )
94
95 C( message_set_stream(msg, in, NULL) )
96
97 C( mailer_create(&mailer, optmailer) )
98
99 if(optdebug)
100 {
101 mu_debug_t debug;
102 mailer_get_debug (mailer, &debug);
103 mu_debug_set_level (debug, MU_DEBUG_TRACE | MU_DEBUG_PROT);
104 }
105
106 C( mailer_open(mailer, 0) )
107
108 C( mailer_send_message(mailer, msg, from, to) )
109
110 return 0;
111 }
112