2

I am using MFMailComposeViewController for sending the Email,

With this code I am able to get the Image, But image comes after the Text Detail, I wan't First Image and after Text,

Here is my code,

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

    if ([MFMailComposeViewController canSendMail]) {
        //Setting up the Subject, recipients, and message body.
        [mail setToRecipients:[NSArray arrayWithObjects:Emailid,nil]];
        [mail setSubject:@"Receipt"];

        NSData *photoData = UIImageJPEGRepresentation([UIImage imageNamed:@"Gift.png"], 1);
        [mail addAttachmentData:photoData mimeType:@"image/jpg" fileName:[NSString stringWithFormat:@"photo.png"]];

        [mail setMessageBody:@"Receipt" isHTML:NO];
        NSString *emailBody;




        emailBody = [NSString stringWithFormat:@
                     "<br>Purchase Amount:            </br> "      "$  %@"
                     "<br>Amount Received:        </br> "      "$  %@"
                     "<br>Change:    </br> "      "$  %@"
                     ,AmountData,txtAmount.text,ChangeAmount.text];  


        [mail setMessageBody:emailBody isHTML:YES];

Please any body suggest me How can i do that?

Janak Nirmal
  • 22,706
  • 18
  • 63
  • 99
Anki
  • 589
  • 2
  • 13
  • 22

1 Answers1

6

as of my knowledge this is the default behavior of the mfmailcomposeviewcontroller instead of just adding image as attachment you can do as below to show image first just to create body part

(void)createEmail {


NSMutableString *emailBody = [[[NSMutableString alloc] initWithString:@"<html><body>"] retain];
   [emailBody appendString:@"<p>Some email body text can go here</p>"];
   UIImage *emailImage = [UIImage imageNamed:@"myImageName.png"];
   NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(emailImage)];

   NSString *base64String = [imageData base64EncodedString];
  [emailBody appendString:[NSString stringWithFormat:@"<p><b><img src='data:image/png;base64,%@'></b></p>",base64String]];
  [emailBody appendString:@"</body></html>"];
    NSLog(@"%@",emailBody);

 //mail composer window
    MFMailComposeViewController *emailDialog = [[MFMailComposeViewController alloc] init];
    emailDialog.mailComposeDelegate = self;
    [emailDialog setSubject:@"My Inline Image Document"];
    [emailDialog setMessageBody:emailBody isHTML:YES];

    [self presentModalViewController:emailDialog animated:YES];
    [emailDialog release];
    [emailBody release];
}

this is shown here

The iOSDev
  • 5,237
  • 7
  • 41
  • 78
  • Thank's Alok, I just added my base64String code in my aEmail body and its working Fine. – Anki Apr 09 '12 at 05:37
  • With this imageData base64EncodedString, i able to show the image in iPhone mail detail but not able to get the Image in Gmail Account, How can i solve that problem, Any suggession? – Anki Apr 11 '12 at 14:40
  • actually i dont have much knowledge about it too – The iOSDev Apr 12 '12 at 07:05
  • i think i got one answer just check [this](http://stackoverflow.com/questions/1527351/how-to-add-an-uiimage-in-mailcomposer-sheet-of-mfmailcomposeviewcontroller-in-ip) out the first accepted answers discription – The iOSDev Apr 12 '12 at 07:09
  • @TheiOSDev Thanks for the answer. Magic line ---> NSString *base64String = [imageData base64EncodedString]; – iPhoneDeveloper Apr 04 '18 at 14:15