2 Copyright (C) 2000-2003 SKYRIX Software AG
4 This file is part of OGo
6 OGo is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with OGo; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 #include <NGObjWeb/WOMailDelivery.h>
24 #include <NGObjWeb/WOComponent.h>
25 #include <NGObjWeb/WOResponse.h>
28 @implementation WOMailDelivery
34 WOMailDelivery *sharedInstance = nil;
36 + (id)sharedInstance {
37 if (sharedInstance == nil)
38 sharedInstance = [[WOMailDelivery alloc] init];
39 return sharedInstance;
44 - (id)composeEmailFrom:(NSString *)_senderAddress
45 to:(NSArray *)_receiverAddresses
46 cc:(NSArray *)_ccAddresses
47 subject:(NSString *)_subject
48 plainText:(NSString *)_text
51 NSMutableDictionary *email = [NSMutableDictionary dictionaryWithCapacity:16];
54 if (_senderAddress == nil) return nil;
55 if ([_receiverAddresses count] < 1) return nil;
57 if (_subject == nil) _subject = @"";
58 if (_text == nil) _text = @"";
59 if (_ccAddresses == nil) _ccAddresses = [NSArray array];
61 [email setObject:_subject forKey:@"subject"];
62 [email setObject:_receiverAddresses forKey:@"to"];
63 [email setObject:_ccAddresses forKey:@"cc"];
64 [email setObject:_senderAddress forKey:@"from"];
65 [email setObject:@"text/plain; charset=us-ascii" forKey:@"content-type"];
67 content = [NSData dataWithBytes:[_text cString] length:[_text cStringLength]];
68 [email setObject:content forKey:@"body"];
69 [email setObject:[NSNumber numberWithInt:[content length]]
70 forKey:@"content-length"];
73 if (![self sendEmail:email])
79 - (id)composeEmailFrom:(NSString *)_senderAddress
80 to:(NSArray *)_receiverAddresses
81 cc:(NSArray *)_ccAddresses
82 subject:(NSString *)_subject
83 component:(WOComponent *)_component
86 NSMutableDictionary *email = [NSMutableDictionary dictionaryWithCapacity:16];
88 if (_senderAddress == nil) return nil;
89 if ([_receiverAddresses count] < 1) return nil;
90 if (_subject == nil) _subject = @"";
91 if (_ccAddresses == nil) _ccAddresses = [NSArray array];
93 [email setObject:_subject forKey:@"subject"];
94 [email setObject:_receiverAddresses forKey:@"to"];
95 [email setObject:_ccAddresses forKey:@"cc"];
96 [email setObject:_senderAddress forKey:@"from"];
100 WOResponse *response;
101 NSString *contentType;
103 response = [_component generateResponse];
104 if ([response status] != 200)
105 // could not generate response
108 contentType = [response headerForKey:@"content-type"];
109 if (contentType == nil) contentType = @"text/html";
111 [email setObject:contentType forKey:@"content-type"];
112 [email setObject:[response content] forKey:@"body"];
113 [email setObject:[NSNumber numberWithInt:[[response content] length]]
114 forKey:@"content-length"];
118 if (![self sendEmail:email])
126 - (BOOL)sendEmail:(id)_email {
127 NSMutableString *sendmail = [NSMutableString stringWithCapacity:256];
131 to = [_email objectForKey:@"to"];
132 cc = [_email objectForKey:@"cc"];
134 [sendmail appendString:[[NSUserDefaults standardUserDefaults]
135 stringForKey:@"WOSendMail"]];
136 [sendmail appendString:@" "];
137 [sendmail appendString:[to componentsJoinedByString:@" "]];
138 [sendmail appendString:@" "];
139 [sendmail appendString:[cc componentsJoinedByString:@" "]];
141 if ((toMail = popen([sendmail cString], "w"))) {
142 NSEnumerator *e = nil;
146 if ((tmp = [[_email objectForKey:@"from"] stringValue])) {
147 if (fprintf(toMail, "Reply-To: %s\n", [tmp cString]) < 0)
149 if (fprintf(toMail, "From: %s\n", [tmp cString]) < 0)
153 e = [to objectEnumerator];
154 while ((entry = [e nextObject])) {
155 if (fprintf(toMail, "To:%s\n", [[entry stringValue] cString]) < 0)
159 e = [cc objectEnumerator];
160 while ((entry = [e nextObject])) {
161 if (fprintf(toMail, "Cc:%s\n", [[entry stringValue] cString]) < 0)
165 if ((tmp = [[_email objectForKey:@"subject"] stringValue])) {
166 if (fprintf(toMail, "Subject:%s\n", [tmp cString]) < 0)
170 if ((tmp = [[_email objectForKey:@"content-type"] stringValue])) {
171 if (fprintf(toMail, "Content-type:%s\n", [tmp cString]) < 0)
174 if ((tmp = [[_email objectForKey:@"content-length"] stringValue])) {
175 if (fprintf(toMail, "Content-length:%s\n", [tmp cString]) < 0)
180 if (fprintf(toMail, "\n") < 0)
185 NSData *body = [_email objectForKey:@"body"];
186 if (fwrite([body bytes], [body length], 1, toMail) < 0)
189 fprintf(toMail, "\n");
201 @end /* WOMailDelivery */