Þæs Ofereode - iSendMail.m
#import <Foundation/Foundation.h>
#import <Message/NSMailDelivery.h>
#import <unistd.h>
void usage(void)
{
fprintf(stderr,"iSendMail -f from_addr -t to_addr -s subject\n\n");
exit(EXIT_FAILURE);
}
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSAttributedString *attrMsg;
NSMutableString *msg=[[NSMutableString alloc] init];
NSMutableDictionary *headers=[NSMutableDictionary dictionary];
char argch,msgIn[BUFSIZ];
BOOL mailSent=NO;
//parse command-line args
while((argch=getopt(argc,argv,"f:t:s:"))!=-1)
{
switch(argch)
{
case 'f':
[headers setObject:[NSString stringWithCString:optarg] forKey:@"From"];
break;
case 't':
[headers setObject:[NSString stringWithCString:optarg] forKey:@"To"];
break;
case 's':
[headers setObject:[NSString stringWithCString:optarg] forKey:@"Subject"];
break;
case '?':
default:
usage();
}
}
if([NSMailDelivery hasDeliveryClassBeenConfigured]==NO)
{
//can't send mail...
NSLog(@"Cannot send mail!\n");
[pool release];
exit(EXIT_FAILURE);
}
//get the message from stdin
while(!feof(stdin))
{
fgets(msgIn,BUFSIZ,stdin);
[msg appendString:[NSString stringWithCString:msgIn]];
}
//create an attributed string
attrMsg=[[NSAttributedString alloc] initWithString:msg];
// check we have enough information
if([headers objectForKey:@"From"]==nil || [headers objectForKey:@"To"]==nil || [headers objectForKey:@"Subject"]==nil)
{
usage();
}
mailSent=[NSMailDelivery deliverMessage:attrMsg headers:headers format:NSASCIIMailFormat protocol:nil];
if(mailSent==YES)
{
NSLog(@"Mail sent successfully.\n");
}
else
{
NSLog(@"Mail delivery failed!\n");
}
[pool release];
return 0;
}
Raw Source