2 * Copyright 2003-2016, Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
6 * Santiago (Jacques) Lema
7 * Jérôme Duval, jerome.duval@gmail.com
8 * Augustin Cavalier, <waddlesplash>
9 * Alexander G. M. Smith <agmsmith@ncf.ca>
16 #include <Application.h>
20 #define APP_SIG "application/x-vnd.Haiku-mail_utils-mail"
23 int main(int argc
, char* argv
[])
25 BApplication
mailApp(APP_SIG
);
27 // No arguments, show usage
29 fprintf(stdout
,"This program can only send mail, not read it.\n");
30 fprintf(stdout
,"usage: %s [-v] [-s subject] [-c cc-addr] "
31 "[-b bcc-addr] to-addr ...\n", argv
[0]);
35 char *subject
= "No subject";
42 for (int i
= 1; i
< argc
; i
++) {
43 if (strcmp(argv
[i
], "-v") == 0)
45 else if (strcmp(argv
[i
], "-s") == 0) {
48 } else if (strcmp(argv
[i
], "-c") == 0) {
51 } else if (strcmp(argv
[i
], "-b") == 0) {
62 fprintf(stdout
, "\n");
63 fprintf(stdout
, "To:\t%s\n", to
.String());
64 fprintf(stdout
, "Cc:\t%s\n", cc
);
65 fprintf(stdout
, "Bcc:\t%s\n", bcc
);
66 fprintf(stdout
, "Subj:\t%s\n", subject
);
67 fprintf(stdout
, "\n");
70 // Check if recipients are valid
71 if (strcmp(to
.String(), "") == 0 &&
72 strcmp(cc
, "") == 0 &&
73 strcmp(bcc
, "") == 0) {
75 fprintf(stderr
, "[Error]: You must specify at least one recipient "
76 "in to, cc or bcc fields.\n");
80 bool isTerminal
= isatty(STDIN_FILENO
) != 0;
82 fprintf(stderr
, "Now type your message.\n"
83 "Type '.' alone on a line to end your text and send it.\n");
87 char line
[32768] = "";
89 // Read each line and collect the body text until we get an end of text
90 // marker. That's a single dot "." on a line typed in by the user,
91 // or end of file when reading a file.
93 if (fgets(line
, sizeof(line
), stdin
) == NULL
) {
94 // End of file or an error happened, just send collected body text.
98 if (isTerminal
&& strcmp(line
, ".\n") == 0)
105 fprintf(stdout
, "\nBody:\n%s\n", body
.String());
108 fprintf(stderr
, "Sending E-mail...\n");
112 mail
.AddHeaderField(B_MAIL_TO
, to
.String());
113 mail
.AddHeaderField(B_MAIL_CC
, cc
);
114 mail
.AddHeaderField(B_MAIL_BCC
, bcc
);
115 mail
.AddHeaderField(B_MAIL_SUBJECT
, subject
);
116 mail
.AddContent(body
.String(), body
.Length());
117 status_t result
= mail
.Send();
119 if (result
== B_OK
) {
121 fprintf(stderr
, "Message was sent successfully.\n");
125 fprintf(stderr
, "Message failed to send: %s\n", strerror(result
));