2007-08-09 22:54:52 +02:00
|
|
|
/* SOGoMailer.m - this file is part of SOGo
|
|
|
|
*
|
2010-11-04 19:34:51 +01:00
|
|
|
* Copyright (C) 2007-2010 Inverse inc.
|
2007-08-09 22:54:52 +02:00
|
|
|
*
|
|
|
|
* Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
|
|
|
|
*
|
|
|
|
* This file is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This file is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import <Foundation/NSArray.h>
|
|
|
|
#import <Foundation/NSEnumerator.h>
|
|
|
|
#import <Foundation/NSException.h>
|
|
|
|
#import <Foundation/NSString.h>
|
|
|
|
|
|
|
|
#import <NGObjWeb/NSException+HTTP.h>
|
|
|
|
#import <NGExtensions/NSObject+Logs.h>
|
|
|
|
#import <NGMail/NGSendMail.h>
|
|
|
|
#import <NGMail/NGSmtpClient.h>
|
|
|
|
#import <NGMime/NGMimePartGenerator.h>
|
2009-12-26 17:01:08 +01:00
|
|
|
#import <NGStreams/NGInternetSocketAddress.h>
|
2007-08-09 22:54:52 +02:00
|
|
|
|
|
|
|
#import "NSString+Utilities.h"
|
2009-11-29 05:19:32 +01:00
|
|
|
#import "SOGoDomainDefaults.h"
|
|
|
|
#import "SOGoSystemDefaults.h"
|
2007-08-09 22:54:52 +02:00
|
|
|
|
2009-11-29 05:19:32 +01:00
|
|
|
#import "SOGoMailer.h"
|
2007-08-09 22:54:52 +02:00
|
|
|
|
|
|
|
@implementation SOGoMailer
|
|
|
|
|
2009-11-29 05:19:32 +01:00
|
|
|
+ (SOGoMailer *) mailerWithDomainDefaults: (SOGoDomainDefaults *) dd
|
2007-08-09 22:54:52 +02:00
|
|
|
{
|
2009-11-29 05:19:32 +01:00
|
|
|
return [[self alloc] initWithDomainDefaults: dd];
|
|
|
|
}
|
2007-08-09 22:54:52 +02:00
|
|
|
|
2009-11-29 05:19:32 +01:00
|
|
|
- (id) initWithDomainDefaults: (SOGoDomainDefaults *) dd
|
|
|
|
{
|
|
|
|
if ((self = [self init]))
|
|
|
|
{
|
|
|
|
ASSIGN (mailingMechanism, [dd mailingMechanism]);
|
|
|
|
ASSIGN (smtpServer, [dd smtpServer]);
|
|
|
|
}
|
2007-08-09 22:54:52 +02:00
|
|
|
|
2009-11-29 05:19:32 +01:00
|
|
|
return self;
|
2007-08-09 22:54:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) init
|
|
|
|
{
|
|
|
|
if ((self = [super init]))
|
|
|
|
{
|
2009-11-29 05:19:32 +01:00
|
|
|
mailingMechanism = nil;
|
|
|
|
smtpServer = nil;
|
2007-08-09 22:54:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
[mailingMechanism release];
|
|
|
|
[smtpServer release];
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSException *) _sendmailSendData: (NSData *) mailData
|
|
|
|
toRecipients: (NSArray *) recipients
|
|
|
|
sender: (NSString *) sender
|
|
|
|
{
|
|
|
|
NSException *result;
|
|
|
|
NGSendMail *mailer;
|
|
|
|
|
|
|
|
mailer = [NGSendMail sharedSendMail];
|
|
|
|
if ([mailer isSendMailAvailable])
|
|
|
|
result = [mailer sendMailData: mailData
|
|
|
|
toRecipients: recipients
|
|
|
|
sender: sender];
|
|
|
|
else
|
|
|
|
result = [NSException exceptionWithHTTPStatus: 500
|
|
|
|
reason: @"cannot send message:"
|
|
|
|
@" no sendmail binary!"];
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSException *) _sendMailData: (NSData *) mailData
|
|
|
|
withClient: (NGSmtpClient *) client
|
|
|
|
{
|
|
|
|
NSException *result;
|
|
|
|
|
|
|
|
if ([client sendData: mailData])
|
|
|
|
result = nil;
|
|
|
|
else
|
|
|
|
result = [NSException exceptionWithHTTPStatus: 500
|
|
|
|
reason: @"cannot send message:"
|
|
|
|
@" (smtp) failure when sending data"];
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSException *) _smtpSendData: (NSData *) mailData
|
|
|
|
toRecipients: (NSArray *) recipients
|
|
|
|
sender: (NSString *) sender
|
|
|
|
{
|
2009-12-26 17:01:08 +01:00
|
|
|
NGInternetSocketAddress *addr;
|
|
|
|
NSString *currentTo, *host;
|
2010-11-04 19:34:51 +01:00
|
|
|
NSMutableArray *toErrors;
|
2009-12-26 17:01:08 +01:00
|
|
|
NSEnumerator *addresses;
|
2007-08-09 22:54:52 +02:00
|
|
|
NGSmtpClient *client;
|
|
|
|
NSException *result;
|
2009-12-26 17:01:08 +01:00
|
|
|
NSRange r;
|
|
|
|
|
2010-11-04 19:34:51 +01:00
|
|
|
unsigned int port;
|
2007-08-09 22:54:52 +02:00
|
|
|
|
|
|
|
client = [NGSmtpClient smtpClient];
|
2009-12-26 17:01:08 +01:00
|
|
|
host = smtpServer;
|
|
|
|
port = 25;
|
|
|
|
|
|
|
|
// We check if there is a port specified in the smtpServer ivar value
|
|
|
|
r = [smtpServer rangeOfString: @":"];
|
|
|
|
|
|
|
|
if (r.length)
|
|
|
|
{
|
|
|
|
port = [[smtpServer substringFromIndex: r.location+1] intValue];
|
|
|
|
host = [smtpServer substringToIndex: r.location];
|
|
|
|
}
|
|
|
|
|
|
|
|
addr = [NGInternetSocketAddress addressWithPort: port
|
|
|
|
onHost: host];
|
|
|
|
|
2010-11-10 21:12:54 +01:00
|
|
|
NS_DURING
|
2007-08-09 22:54:52 +02:00
|
|
|
{
|
2010-11-10 21:12:54 +01:00
|
|
|
[client connectToAddress: addr];
|
2008-10-23 19:37:21 +02:00
|
|
|
if ([client mailFrom: sender])
|
2007-08-09 22:54:52 +02:00
|
|
|
{
|
2010-11-04 19:34:51 +01:00
|
|
|
toErrors = [NSMutableArray array];
|
2007-08-09 22:54:52 +02:00
|
|
|
addresses = [recipients objectEnumerator];
|
|
|
|
currentTo = [addresses nextObject];
|
|
|
|
while (currentTo)
|
|
|
|
{
|
|
|
|
if (![client recipientTo: [currentTo pureEMailAddress]])
|
|
|
|
{
|
|
|
|
[self logWithFormat: @"error with recipient '%@'", currentTo];
|
2010-11-04 19:34:51 +01:00
|
|
|
[toErrors addObject: [currentTo pureEMailAddress]];
|
2007-08-09 22:54:52 +02:00
|
|
|
}
|
|
|
|
currentTo = [addresses nextObject];
|
|
|
|
}
|
2010-11-04 19:34:51 +01:00
|
|
|
if ([toErrors count] == [recipients count])
|
2007-08-09 22:54:52 +02:00
|
|
|
result = [NSException exceptionWithHTTPStatus: 500
|
|
|
|
reason: @"cannot send message:"
|
|
|
|
@" (smtp) all recipients discarded"];
|
2010-11-04 19:34:51 +01:00
|
|
|
else if ([toErrors count] > 0)
|
|
|
|
result = [NSException exceptionWithHTTPStatus: 500
|
|
|
|
reason: [NSString stringWithFormat:
|
|
|
|
@"cannot send message (smtp) - recipients discarded:\n%@",
|
|
|
|
[toErrors componentsJoinedByString: @", "]]];
|
2007-08-09 22:54:52 +02:00
|
|
|
else
|
2010-11-04 19:34:51 +01:00
|
|
|
result = [self _sendMailData: mailData withClient: client];
|
2007-08-09 22:54:52 +02:00
|
|
|
}
|
|
|
|
[client quit];
|
|
|
|
[client disconnect];
|
|
|
|
}
|
2010-11-10 21:12:54 +01:00
|
|
|
NS_HANDLER
|
|
|
|
{
|
|
|
|
NSLog(@"Could not connect to the SMTP server %@ on port %d", host, port);
|
|
|
|
result = [NSException exceptionWithHTTPStatus: 500
|
|
|
|
reason: @"cannot send message:"
|
|
|
|
@" (smtp) error when connecting"];
|
|
|
|
}
|
|
|
|
NS_ENDHANDLER;
|
2007-08-09 22:54:52 +02:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSException *) sendMailData: (NSData *) data
|
|
|
|
toRecipients: (NSArray *) recipients
|
|
|
|
sender: (NSString *) sender
|
|
|
|
{
|
|
|
|
NSException *result;
|
|
|
|
|
|
|
|
if (![recipients count])
|
|
|
|
result = [NSException exceptionWithHTTPStatus: 500
|
|
|
|
reason: @"cannot send message: no recipients set"];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (![sender length])
|
|
|
|
result = [NSException exceptionWithHTTPStatus: 500
|
|
|
|
reason: @"cannot send message: no sender set"];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ([mailingMechanism isEqualToString: @"sendmail"])
|
|
|
|
result = [self _sendmailSendData: data
|
|
|
|
toRecipients: recipients
|
|
|
|
sender: [sender pureEMailAddress]];
|
|
|
|
else
|
|
|
|
result = [self _smtpSendData: data
|
|
|
|
toRecipients: recipients
|
|
|
|
sender: [sender pureEMailAddress]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSException *) sendMimePart: (id <NGMimePart>) part
|
|
|
|
toRecipients: (NSArray *) recipients
|
|
|
|
sender: (NSString *) sender
|
|
|
|
{
|
|
|
|
NSData *mailData;
|
|
|
|
|
|
|
|
mailData = [[NGMimePartGenerator mimePartGenerator]
|
|
|
|
generateMimeFromPart: part];
|
|
|
|
|
|
|
|
return [self sendMailData: mailData
|
|
|
|
toRecipients: recipients
|
|
|
|
sender: sender];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSException *) sendMailAtPath: (NSString *) filename
|
|
|
|
toRecipients: (NSArray *) recipients
|
|
|
|
sender: (NSString *) sender
|
|
|
|
{
|
|
|
|
NSException *result;
|
|
|
|
NSData *mailData;
|
|
|
|
|
|
|
|
mailData = [NSData dataWithContentsOfFile: filename];
|
|
|
|
if ([mailData length] > 0)
|
|
|
|
result = [self sendMailData: mailData
|
|
|
|
toRecipients: recipients
|
|
|
|
sender: sender];
|
|
|
|
else
|
|
|
|
result = [NSException exceptionWithHTTPStatus: 500
|
|
|
|
reason: @"cannot send message: no data"
|
|
|
|
@" (missing or empty file?)"];
|
|
|
|
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|