MFMailComposeViewController csv attachment not being attached, but showing inline instead

Harris picture Harris · Nov 7, 2009 · Viewed 7.4k times · Source

I am having a problem with sending csv attachments via MFMailComposeViewController. Sometimes they come through just fine, but for other users they don't come through as attachments, but rather as text inline in the email (with <br/> instead of line returns.) It's very strange. Anybody know what I'm doing wrong? Here is a snippet of my code:

MFMailComposeViewController *mailComposeViewController = [[MFMailComposeViewController alloc] init];
mailComposeViewController.mailComposeDelegate = self;

NSString *csv = @"foo,bar,blah,hello";
NSData *csvData = [csv dataUsingEncoding:NSUTF8StringEncoding];
[mailComposeViewController addAttachmentData:csvData mimeType:@"text/csv" fileName:@"testing.csv"];

[mailComposeViewController setSubject:@"testing sending csv attachment"];
[mailComposeViewController setMessageBody:@"csv file should be attached" isHTML:NO];
[self presentModalViewController:mailComposeViewController animated:YES];

Answer

jal picture jal · Feb 22, 2011
-(IBAction)btnPressed:(id)sender {
    NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *docDir = [arrayPaths objectAtIndex:0];
    NSString *Path = [docDir stringByAppendingString:@"/CSVFile.csv"];
    NSData *csvData = [NSData dataWithContentsOfFile:Path]; 

    MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
    controller.mailComposeDelegate = self;

    [controller setSubject:@"For csv file..."];
    [controller setMessageBody:@"...csv file is hear.." isHTML:NO];
    [controller addAttachmentData:csvData mimeType:@"text/csv" fileName:@"CSVFile.csv"];
    [self presentModalViewController:controller animated:YES];
    [controller release];
}