MailUtils.m
author Jens Alfke <jens@mooseyard.com>
Sun May 10 18:57:43 2009 -0700 (2009-05-10)
changeset 29 8874aff14cc9
permissions -rw-r--r--
* Added kv*Set utility functions for handling KV grunge when mutating an NSSet property.
* Change CFMakeCollectable to NSMakeCollectable.
jens@17
     1
//
jens@17
     2
//  MailUtils.m
jens@17
     3
//  YourMove
jens@17
     4
//
jens@17
     5
//  Created by Jens Alfke on 7/13/08.
jens@17
     6
//  Copyright 2008 Jens Alfke. All rights reserved.
jens@17
     7
//  Adapted from Apple's "SBSendEmail" sample app.
jens@17
     8
//
jens@17
     9
jens@17
    10
#import "MailUtils.h"
jens@17
    11
#import "MailBridge.h"
jens@17
    12
#import "CollectionUtils.h"
jens@17
    13
jens@17
    14
jens@17
    15
@implementation OutgoingEmail
jens@17
    16
jens@17
    17
jens@17
    18
- (id) init
jens@17
    19
{
jens@17
    20
    self = [super init];
jens@17
    21
    if (self != nil) {
jens@17
    22
        _toRecipients = [[NSMutableArray alloc] init];
jens@17
    23
        _attachments  = [[NSMutableArray alloc] init];
jens@17
    24
    }
jens@17
    25
    return self;
jens@17
    26
}
jens@17
    27
jens@17
    28
- (id) initWithSubject: (NSString*)subject body: (NSString*)body
jens@17
    29
{
jens@17
    30
    self = [self init];
jens@17
    31
    if (self != nil) {
jens@17
    32
        self.subject = subject;
jens@17
    33
        self.body = body;
jens@17
    34
    }
jens@17
    35
    return self;
jens@17
    36
}
jens@17
    37
jens@17
    38
- (void) dealloc
jens@17
    39
{
jens@17
    40
    [_message release];
jens@17
    41
    [_subject release];
jens@17
    42
    [_body release];
jens@17
    43
    [_sender release];
jens@17
    44
    [_toRecipients release];
jens@17
    45
    [_attachments release];
jens@17
    46
    [super dealloc];
jens@17
    47
}
jens@17
    48
jens@17
    49
jens@17
    50
@synthesize subject=_subject, sender=_sender, body=_body,
jens@17
    51
            toRecipients=_toRecipients, attachments=_attachments;
jens@17
    52
jens@17
    53
jens@17
    54
+ (MailApplication*) mailApp
jens@17
    55
{
jens@17
    56
    /* create a Scripting Bridge object for talking to the Mail application */
jens@17
    57
    MailApplication *mail = [SBApplication applicationWithBundleIdentifier:@"com.apple.Mail"];
jens@17
    58
    mail.timeout = 5*60; // in ticks
jens@17
    59
    return mail;
jens@17
    60
}
jens@17
    61
jens@17
    62
+ (BOOL) isMailRunning
jens@17
    63
{
jens@17
    64
    return [self mailApp].isRunning;
jens@17
    65
}
jens@17
    66
jens@17
    67
- (MailOutgoingMessage*) _message
jens@17
    68
{
jens@17
    69
    if( ! _message ) {
jens@17
    70
        MailApplication *mail = [[self class] mailApp];
jens@17
    71
        
jens@17
    72
        /* create a new outgoing message object */
jens@17
    73
        MailOutgoingMessage *emailMessage =
jens@17
    74
            [[[mail classForScriptingClass:@"outgoing message"] alloc] initWithProperties:
jens@17
    75
                                                                 $dict({@"subject", self.subject},
jens@17
    76
                                                                       {@"content", self.body})];
jens@17
    77
        
jens@17
    78
        /* set the sender, show the message */
jens@17
    79
        if( _sender )
jens@17
    80
            emailMessage.sender = _sender;
jens@17
    81
        
jens@17
    82
        /* Have to add this to a container now, else the scripting bridge complains */
jens@17
    83
        [[mail outgoingMessages] addObject: emailMessage];
jens@17
    84
jens@17
    85
        /* create a new recipient and add it to the recipients list */
jens@17
    86
        for( NSString *recipient in _toRecipients ) {
jens@17
    87
            MailToRecipient *theRecipient =
jens@17
    88
                [[[mail classForScriptingClass:@"to recipient"] alloc] initWithProperties:
jens@17
    89
                                                                 $dict({@"address", recipient})];
jens@17
    90
            [emailMessage.toRecipients addObject: theRecipient];
jens@17
    91
        }
jens@17
    92
        
jens@17
    93
        /* add an attachment, if one was specified */
jens@17
    94
        for( NSString *attachmentPath in self.attachments ) {
jens@17
    95
            /* create an attachment object */
jens@17
    96
            MailAttachment *theAttachment = 
jens@17
    97
                [[[mail classForScriptingClass:@"attachment"] alloc] initWithProperties:
jens@17
    98
                                                                 $dict({@"fileName", attachmentPath})];
jens@17
    99
            
jens@17
   100
            /* add it to the list of attachments */
jens@17
   101
            [[emailMessage.content attachments] addObject: theAttachment];
jens@17
   102
        }
jens@17
   103
        
jens@17
   104
        /* add the object to the mail app  */
jens@17
   105
        _message = [emailMessage retain];
jens@17
   106
    }
jens@17
   107
    return _message;
jens@17
   108
}
jens@17
   109
jens@17
   110
jens@17
   111
- (void) show
jens@17
   112
{
jens@17
   113
	self._message.visible = YES;
jens@17
   114
    [[[self class] mailApp] activate];
jens@17
   115
}
jens@17
   116
jens@17
   117
jens@17
   118
- (void) send
jens@17
   119
{
jens@17
   120
	[self._message send];
jens@17
   121
}
jens@17
   122
jens@17
   123
jens@17
   124
@end
jens@17
   125
jens@17
   126
jens@17
   127
jens@17
   128
jens@17
   129
TestCase(MailUtils) {
jens@17
   130
    OutgoingEmail *m = [[OutgoingEmail alloc] initWithSubject: @"This is a test"
jens@17
   131
                                                         body: @"Hi there! This is a test email from an automated test case. http://mooseyard.com/"];
jens@17
   132
    [m.toRecipients addObject: @"jens@mooseyard.com"];
jens@17
   133
    [m show];
jens@17
   134
    //[m send];
jens@17
   135
    [m release];
jens@17
   136
}