Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MFMailComposeViewController csv attachment not being attached, but showing inline instead

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];
like image 297
Elijah Avatar asked Nov 07 '09 07:11

Elijah


2 Answers

-(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];
}
like image 111
jal Avatar answered Sep 22 '22 11:09

jal


Hi I put sample code for Creating CSV file and attach it with mail but make sure you have to add MessageUI.Framework and import its related header "MessageUI/MessageUI.h" "MessageUI/MFMailComposeViewController.h" and deligate "MFMailComposeViewControllerDelegate"...I hope this wl useful for others

- (void)viewDidLoad {

arrCsv=[[NSArray alloc]initWithObjects:@"Hello",@"Hi",@"traun",@"fine",nil];
NSArray *paths = NSSearchPathForDirectoriesInDomains

(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *fileName = [NSString stringWithFormat:@"%@/try.csv", documentsDirectory];

[[arrCsv componentsJoinedByString:@","] writeToFile:fileName atomically:YES encoding:NSUTF8StringEncoding error:NULL];

 }



-(ibAction)btnMail   {

 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];

}


- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
{   message.hidden = NO;
switch (result)
{
    case MFMailComposeResultCancelled:
        message.text = @"Result: canceled";
        break;
    case MFMailComposeResultSaved:
        message.text = @"Result: saved";
        break;
    case MFMailComposeResultSent:
        message.text = @"Result: sent";
        break;
    case MFMailComposeResultFailed:
        message.text = @"Result: failed";
        break;
    default:
        message.text = @"Result: not sent";
        break;
}
[self dismissModalViewControllerAnimated:YES];
}
like image 35
sinh99 Avatar answered Sep 23 '22 11:09

sinh99