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