2 Copyright (C) 2000-2005 SKYRIX Software AG
4 This file is part of SOPE.
6 SOPE 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 SOPE 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 SOPE; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 #include <NGObjWeb/WOMailDelivery.h>
23 #include <NGObjWeb/WOComponent.h>
24 #include <NGObjWeb/WOResponse.h>
27 @implementation WOMailDelivery
33 WOMailDelivery *sharedInstance = nil;
35 + (id)sharedInstance {
36 if (sharedInstance == nil)
37 sharedInstance = [[WOMailDelivery alloc] init];
38 return sharedInstance;
43 - (id)composeEmailFrom:(NSString *)_senderAddress
44 to:(NSArray *)_receiverAddresses
45 cc:(NSArray *)_ccAddresses
46 subject:(NSString *)_subject
47 plainText:(NSString *)_text
50 NSMutableDictionary *email = [NSMutableDictionary dictionaryWithCapacity:16];
53 if (_senderAddress == nil) return nil;
54 if ([_receiverAddresses count] < 1) return nil;
56 if (_subject == nil) _subject = @"";
57 if (_text == nil) _text = @"";
58 if (_ccAddresses == nil) _ccAddresses = [NSArray array];
60 [email setObject:_subject forKey:@"subject"];
61 [email setObject:_receiverAddresses forKey:@"to"];
62 [email setObject:_ccAddresses forKey:@"cc"];
63 [email setObject:_senderAddress forKey:@"from"];
64 [email setObject:@"text/plain; charset=us-ascii" forKey:@"content-type"];
66 content = [NSData dataWithBytes:[_text cString] length:[_text cStringLength]];
67 [email setObject:content forKey:@"body"];
68 [email setObject:[NSNumber numberWithInt:[content length]]
69 forKey:@"content-length"];
72 if (![self sendEmail:email])
78 - (id)composeEmailFrom:(NSString *)_senderAddress
79 to:(NSArray *)_receiverAddresses
80 cc:(NSArray *)_ccAddresses
81 subject:(NSString *)_subject
82 component:(WOComponent *)_component
85 NSMutableDictionary *email = [NSMutableDictionary dictionaryWithCapacity:16];
87 if (_senderAddress == nil) return nil;
88 if ([_receiverAddresses count] < 1) return nil;
89 if (_subject == nil) _subject = @"";
90 if (_ccAddresses == nil) _ccAddresses = [NSArray array];
92 [email setObject:_subject forKey:@"subject"];
93 [email setObject:_receiverAddresses forKey:@"to"];
94 [email setObject:_ccAddresses forKey:@"cc"];
95 [email setObject:_senderAddress forKey:@"from"];
100 NSString *contentType;
102 response = [_component generateResponse];
103 if ([response status] != 200)
104 // could not generate response
107 contentType = [response headerForKey:@"content-type"];
108 if (contentType == nil) contentType = @"text/html";
110 [email setObject:contentType forKey:@"content-type"];
111 [email setObject:[response content] forKey:@"body"];
112 [email setObject:[NSNumber numberWithInt:[[response content] length]]
113 forKey:@"content-length"];
117 if (![self sendEmail:email])
125 - (BOOL)sendEmail:(id)_email {
126 NSMutableString *sendmail = [NSMutableString stringWithCapacity:256];
130 to = [_email objectForKey:@"to"];
131 cc = [_email objectForKey:@"cc"];
133 [sendmail appendString:[[NSUserDefaults standardUserDefaults]
134 stringForKey:@"WOSendMail"]];
135 [sendmail appendString:@" "];
136 [sendmail appendString:[to componentsJoinedByString:@" "]];
137 [sendmail appendString:@" "];
138 [sendmail appendString:[cc componentsJoinedByString:@" "]];
140 if ((toMail = popen([sendmail cString], "w"))) {
141 NSEnumerator *e = nil;
145 if ((tmp = [[_email objectForKey:@"from"] stringValue])) {
146 if (fprintf(toMail, "Reply-To: %s\n", [tmp cString]) < 0)
148 if (fprintf(toMail, "From: %s\n", [tmp cString]) < 0)
152 e = [to objectEnumerator];
153 while ((entry = [e nextObject])) {
154 if (fprintf(toMail, "To:%s\n", [[entry stringValue] cString]) < 0)
158 e = [cc objectEnumerator];
159 while ((entry = [e nextObject])) {
160 if (fprintf(toMail, "Cc:%s\n", [[entry stringValue] cString]) < 0)
164 if ((tmp = [[_email objectForKey:@"subject"] stringValue])) {
165 if (fprintf(toMail, "Subject:%s\n", [tmp cString]) < 0)
169 if ((tmp = [[_email objectForKey:@"content-type"] stringValue])) {
170 if (fprintf(toMail, "Content-type:%s\n", [tmp cString]) < 0)
173 if ((tmp = [[_email objectForKey:@"content-length"] stringValue])) {
174 if (fprintf(toMail, "Content-length:%s\n", [tmp cString]) < 0)
179 if (fprintf(toMail, "\n") < 0)
184 NSData *body = [_email objectForKey:@"body"];
185 if (fwrite([body bytes], [body length], 1, toMail) < 0)
188 fprintf(toMail, "\n");
200 @end /* WOMailDelivery */