Vikings
Vikings

Reputation: 2527

iPhone In App Email

I have my application sending an email, but is there a way to put a photo in my email above the text i'm sending. Kind of like a header to display my logo.

Upvotes: 1

Views: 948

Answers (3)

theChrisKent
theChrisKent

Reputation: 15099

When you attach an image using the MFMailComposeViewController it is always displayed at the bottom of the message (under your text, but above the signature) and this cannot be changed in the current version of the framework.

It is possible, however, to encode your image data into base64 and place this directly in the HTML body of your app. I won't include the code here (you can easily google it) because this is tricky and problematic since not all readers will interpret this correctly.

If this is a header image that will be the same for all emails you can put this on a server somewhere and then include an <img> tag in your HTML email body that references this file.

If this is a dynamic image you can have your app upload it to one of the many image hosting sites, retrieve the url and again include it as the src of an <img> tag in your HTML email body.

Upvotes: 1

FreeAsInBeer
FreeAsInBeer

Reputation: 12979

// Action for submenu Email Button
- (IBAction)emailButtonPressed {
[delegate playSound:@"Click_16"];

if (connectionStatus == YES) 
{

    if (maxCounter) 
    {
        NSString *filename = (NSString *)[self currentImageObject:kSerialKey AtIndex:imageCounter];

        NSString* documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

            // set up image data for email
        NSString *imageFile = [documentsDirectory stringByAppendingPathComponent:filename];
        NSData *imageData = [NSData dataWithContentsOfFile:imageFile];

            // set up mail view controller for message
        MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
        if (controller != nil) 
        {
            controller.mailComposeDelegate = self;
            [controller setSubject:@"Email Subject"];
            [controller setMessageBody:@"Check out this picture" isHTML:NO];
            [controller addAttachmentData:imageData mimeType:@"image/png" fileName:filename];
            [self presentModalViewController:controller animated:YES];

        }

        [controller release];

    }

    else 
        [self genericAlert:@"There are no pictures to email."];

}

else
    [self genericAlert:@"You are not connected to the internet.  Please connect and try again."];

}

// email delegate method to dismiss window
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
    [self becomeFirstResponder];
    [self dismissModalViewControllerAnimated:YES];
}

Upvotes: 2

James Bedford
James Bedford

Reputation: 28982

You should use the - (void)addAttachmentData:(NSData*)attachment mimeType:(NSString*)mimeType fileName:(NSString*)filename method from the MFMailComposeViewController class.

Here's a great example of this that shows taking an image from the camera and then sending it an an email message!

Upvotes: 0

Related Questions