Assorted whitespace cleanup and typo fixes.
[haiku.git] / src / bin / mail.cpp
blobff0ddf8ca698c23fd2ec68c9badb9da96268bf05
1 // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
2 //
3 // Copyright (c) 2003, OpenBeOS
4 //
5 // This software is part of the OpenBeOS distribution and is covered
6 // by the OpenBeOS license.
7 //
8 //
9 // File: mail.cpp
10 // Author: Santiago (Jacques) Lema
11 // Description: sends an e-mail from the command-line
12 // Created : May 23, 2003
13 // Modified by: Jérome Duval
14 //
15 // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
17 #include <stdio.h>
18 #include <Application.h>
19 #include <String.h>
20 #include <E-mail.h>
22 #define APP_SIG "application/x-vnd.OBos-cmd-mail"
24 int main( int argc, char* argv[] )
26 BApplication mailApp(APP_SIG);
28 // No arguments, show usage
29 if (argc==1) {
30 fprintf(stdout,"[OBOS-mail] Sorry: This program can only send mail, not read it.\n");
31 fprintf(stdout,"usage: /bin/mail [-v] [-s subject] [-c cc-addr] [-b bcc-addr] to-addr ...\n");
32 fflush(stdout);
33 return B_OK;
36 char *subject = "No title";
37 char *cc = "";
38 char *bcc = "";
39 BString to = "";
40 BString body = "";
42 bool verbose =false;
43 //parse arguments
44 for (int i=1; i<argc; i++) {
45 if (strcmp (argv[i], "-v") == 0)
46 verbose = true;
47 else if (strcmp(argv[i], "-s") == 0) {
48 subject = argv[i+1];
49 i++;
50 } else if (strcmp(argv[i], "-c") == 0) {
51 cc = argv[i+1];
52 i++;
53 } else if (strcmp(argv[i], "-b") == 0) {
54 bcc = argv[i+1];
55 i++;
56 } else {
57 to.Append(argv[i]);
58 if (i < argc - 1)
59 to.Append(" ");
63 if (verbose) {
64 fprintf(stdout,"\n");
65 fprintf(stdout,"To:\t<%s> \n",to.String());
66 fprintf(stdout,"Cc:\t<%s> \n",cc);
67 fprintf(stdout,"Bcc:\t<%s> \n",bcc);
68 fprintf(stdout,"Subj:\t<%s> \n",subject);
69 fprintf(stdout,"Body:\t<%s> \n",body.String());
70 fprintf(stdout,"\n");
73 //check if valid recipients
74 if (strcmp(to.String(), "") == 0
75 && strcmp(cc, "") == 0
76 && strcmp(bcc, "") == 0) {
78 fprintf(stdout,
79 "[Error]\nYou must specify at least one recipient in to,cc or bcc fields.\n");
80 return B_ERROR;
83 //read each line until we get a single dot "." on a line
84 char line[32768] = "";
86 printf("Now type your message.\nType '.' alone on a line to send it.\n");
87 do {
88 gets(line);
90 if (strcmp(line, ".") != 0) {
91 body.Append(line).Append("\n");
93 //fprintf(stdout,"Line: %s \n",line);
94 } while (strcmp(line, ".") != 0);
97 if (verbose)
98 fprintf(stdout, "\nBody:\n%s\n", body.String());
100 if (verbose)
101 fprintf(stdout, "\nSending E-mail...\n");
104 fflush(stdout);
106 BMailMessage mail;
107 mail.AddHeaderField(B_MAIL_TO, to.String());
108 mail.AddHeaderField(B_MAIL_CC, cc);
109 mail.AddHeaderField(B_MAIL_BCC, bcc);
110 mail.AddHeaderField(B_MAIL_SUBJECT, subject);
111 mail.AddContent(body.String(),strlen(body.String()));
112 status_t result = mail.Send();
114 if (result==B_OK)
115 fprintf(stdout, "\nMessage was sent successfully.\n");