0

I have some images on array like AppDelegate.imageArray in email composer I want send this array to some one my sample code is

if ([MFMailComposeViewController canSendMail]) {
        MFMailComposeViewController *composeViewController = [[MFMailComposeViewController alloc] initWithNibName:nil bundle:nil];
        [composeViewController setMailComposeDelegate:self];
        [composeViewController setToRecipients:@[@"example@email.com"]];
        [composeViewController setSubject:@"example subject"];
        [composeViewController setMessageBody:appDelegate.imageArray isHTML:NO];
[self presentViewController:composeViewController animated:YES completion:nil];

but I am getting error like cannot initialize a parameter of type NSString with rvalue of type NSMuttableArray

Darshan Kunjadiya
  • 3,323
  • 1
  • 29
  • 31
Ramesh Boosa
  • 198
  • 8
  • Take a look at this post. http://stackoverflow.com/questions/8816196/attach-multiple-image-in-mail-in-iphone – Baby Groot Aug 05 '13 at 13:06
  • how can i return my global array in email body – Ramesh Boosa Aug 05 '13 at 13:06
  • thank u but here i am storing all images in a one array called images array can i directly return that array – Ramesh Boosa Aug 05 '13 at 13:08
  • You need to use `addAttachmentData:mimeType:fileName:`, iterating through your array and adding the files one at a time. – davidf2281 Aug 05 '13 at 13:10
  • No, you can't directly attach an array; you have to do them individually. See http://stackoverflow.com/questions/4302403/how-can-send-a-file-as-attachment-in-objective-c/4302449#4302449 and http://stackoverflow.com/questions/13304371/attaching-a-image-to-an-email-in-an-app-in-ipad-in-ios6 for ways to attach an image to an email. – lnafziger Aug 05 '13 at 13:11

3 Answers3

4
if ([MFMailComposeViewController canSendMail]) {
        MFMailComposeViewController *composeViewController = [[MFMailComposeViewController alloc] initWithNibName:nil bundle:nil];
        [composeViewController setMailComposeDelegate:self];
        [composeViewController setToRecipients:@[@"example@email.com"]];
        [composeViewController setSubject:@"example subject"];
        [composeViewController setMessageBody:@"Images" isHTML:NO];
        for (int i = 0; i < [appDelegate.imageArray count]; i++)
        {
             UIImage *Image = [appDelegate.imageArray objectAtIndex:i];
             NSData *myData = UIImagePNGRepresentation(Image);
             [composeViewController addAttachmentData:myData mimeType:@"image/png"                  fileName:@"Image.png"];
        }

[self presentViewController:composeViewController animated:YES completion:nil];
Baby Groot
  • 4,637
  • 39
  • 52
  • 71
0

MessageBody is simple string, if you want to attach images you can use something like this:

[composeViewController addAttachmentData:imageData mimeType:@"image/jpeg" fileName:fileName];

And files attached by you would represented differently depend on type.

andrew92b
  • 73
  • 5
0

Here Just call this function with your Arr

-(NSMutableString)createImgMailBodyFromArray:(NSMutableArray *)arr{

    //Create a string with HTML formatting for the email body
    NSMutableString *emailBody = [[NSMutableString alloc] initWithString:@"<html><body>"];


    for (int i = 0; i<[arr count]; i++) {
        UIImage *emailImage = [arr objectAtIndex:i];
        //Convert the image into data
        NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(emailImage)];
        //Create a base64 string representation of the data using NSData+Base64
        NSString *base64String = [imageData base64EncodedString];
        //Add the encoded string to the emailBody string
        //Don't forget the "<b>" tags are required, the "<p>" tags are optional
        [emailBody appendString:[NSString stringWithFormat:@"<p><b><img src='data:image/png;base64,%@'></b></p>",base64String]];
    }


    return emailBody;

}

And now just call this way

if ([MFMailComposeViewController canSendMail]) {
        MFMailComposeViewController *composeViewController = [[MFMailComposeViewController alloc] initWithNibName:nil bundle:nil];
        [composeViewController setMailComposeDelegate:self];
        [composeViewController setToRecipients:@[@"example@email.com"]];
        [composeViewController setSubject:@"example subject"];
        [composeViewController setMessageBody:[self createImgMailBodyFromArray:YOUR_ARR] isHTML:NO];
[self presentViewController:composeViewController animated:YES completion:nil];
    }

And "NSData+base64" file here you can find out https://github.com/l4u/NSData-Base64

SachinVsSachin
  • 6,401
  • 3
  • 33
  • 39