MFMailComposeViewController csv attachment not being attached, but showing inline instead
Asked Answered
A

6

7

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];
Amboceptor answered 7/11, 2009 at 7:14 Comment(0)
B
10
-(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];
}
Beeves answered 22/2, 2011 at 11:14 Comment(0)
H
2

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];
}
Hartwell answered 17/5, 2011 at 5:28 Comment(0)
A
1

set the mime type as "application/octet-stream" and that should do the trick to remove inline attachments (I still named the extension of my file i.e. pdf)

Adolescent answered 3/10, 2013 at 16:39 Comment(2)
With iOS 8, this is the solution that worked for me.Generatrix
Did not help me with jpeg attachments on iOS 8.Keeley
W
0

I believe the second parameter to setMessageBody:isHTML: must be YES for attachments to not show up inline.

Warfore answered 21/12, 2009 at 19:16 Comment(0)
C
0

Even if you set isHTML param to YES, your message body can be sent as plain/text if the message body can be represented as such. And attachments in plain/text messages are not always recognized correctly by some email clients (Outlook).

In my case adding a link in the message body helped. Formatting text as bold with HTML tags works too. Tricky!

Tested on iPod 1G 3.1.3.

Cockswain answered 1/3, 2010 at 22:59 Comment(0)
C
0

This may not be the case here, but one thing to watch out for is that:

[NSString dataUsingEncoding:] 

returns a valid but empty NSData object if the conversion to the specified encoding is not possible. Better to use the full version:

[NSString dataUsingEncoding: s allowLossyConversion: YES]

Or check the length of the returned data. It appears that zero-length data attachments are trimmed somewhere in the mail process.

Chaddy answered 7/5, 2011 at 15:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.