Attachments using email uri scheme in iOS - iphone

I am implementing an iphone application (iOS 4.2) from where I would like to trigger the email client to send messages with attachments. I could effectively use uri schemes in combination with the class NSURL in order to trigger the email app but I was wondering whether it is possible to attach images. I have tried with mailto:whoever#wherever.org?subject=sthg&body=sthgelse&attachment=/path/to/file but the attachments are not included. I know iphone applications are sandboxed therefore it is possible that the email utility were not able to access the path to my image since it is located in my application bundle. On the other hand I was considering to administer my images with the photo manager. (1) Is there a way to include attachments in this way? (2) If so, is it possible to reference images either from my app or from the photo client? I could not find any attachments argument in the mailto RFC but maybe Apple has provided some way to achieve this.
Thanks in advance for your help,
Luis

MFMailComposeViewController will be able to do that, some example of usage belows:
remember to add MessageUI.framework
MFMailComposeViewController *email = [[MFMailComposeViewController alloc] init];
email.mailComposeDelegate = self;
[email setSubject:#"Whatever"];
// Set up recipients
NSArray recipients = [NSArray arrayWithObject:#"whoever#wherever.org"];
[email setToRecipients:recipients];
// Attach an image to the email
UIImage *attachment = ...;
NSData *data = UIImagePNGRepresentation(attachment);
[email addAttachmentData:myData mimeType:#"image/png" fileName:#"ok.png"];
// Fill out the email body text
NSString *emailBody = #"test mail";
[email setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
[email release];

Instead of using the mailto: URL scheme, you should use the MFMailComposeViewController which allows you to add attachments. It also has the added benefit that using will not leave your app.

If one does not have account MFMailComposeViewController simply crashes.
Yes, you can call canSendMail first with the result NO(!), what next?
The answer is - use 'mailto:'. It'll popup dialog to create account.

Related

How to link the text field with my email in my app?

How can I send the information that the user inset in text field UITextField to my email?
and how can I let the information page shows once, only at the first time the user enters the app?
What you're asking is vague, but to take the data entered into a textfield and put it in a string just use:
NSString *theEmailBody = theTextField.text;
From there you can send an email using the standard Apple email framework:
MFMailComposeViewController *mail =[[MFMailComposeViewController alloc] init];
NSString *theEmailbody = theTextField.text;;
[mail setMessageBody:theEmailbody isHTML:NO];
or by making your own mail server and sending the HTTP request there.
You can use the following code in you function to send email.
MFMailComposeViewController *mail =[[MFMailComposeViewController alloc] init];
NSString *emailbody = textField.text;;
[mail setMessageBody:emailbody isHTML:NO];
Apple has sample code to show you how to set up in app email (linked below)
After you've done this, you can easily change (for example) the body of your email to be the contents of your textField by adding this where appropriate.
NSString *emailBody = myTextField.text;
Then additionally, if you wish for the information page to only be visible the first time the app is launched, look into NSUserDefaults. This will allow you to store and retrieve a value to let the app know wether or not to load your information page.
https://developer.apple.com/library/ios/#samplecode/MailComposer/Introduction/Intro.html#//apple_ref/doc/uid/DTS40008865

MFMailComposeViewController adds null Content-Id

This is my first post to the group so please be kind :)
I am sending a couple of audio attachments from my app as follows:
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
picker.modalPresentationStyle = UIModalPresentationCurrentContext;
[picker setSubject:#"Test message"];
NSArray *toRecipients = [NSArray arrayWithObject:#"foo#bar.com"];
[picker setToRecipients:toRecipients];
// multiple attachments are made as follows
NSData *myData = [NSData dataWithContentsOfFile:filePath];
[picker addAttachmentData:myData mimeType:#"audio/x-caf" fileName:fileName];
[picker setMessageBody:#"test message" isHTML:NO];
[controller presentModalViewController:picker animated:YES];
A mailComposeController method takes care of dismissing the modal view.
Unfortunately, when the message is delivered the mime headers for the attachments land up having the Content-Id set to null. Here is what it looks like:
Content-Type: audio/x-caf; name="audio_1.caf"
Content-Transfer-Encoding: base64
Content-Id: <(null)>
Content-Disposition: attachment; filename="audio_1.caf"
If I manually delete the Content-Id field and reload the message in my Mac Mail program (Mail Version 3.6) the attachments show up correctly. In other words, I don't really need/want theContent-Id to be inserted in the mime header.
Also, its worth mentioning that for debugging purposes if I set the mime type to "image/jpeg" the mime header does get a valid Content-Id string. All my attachments show up in Mail. As is to be expected, in that case the attachments don't open correctly on the Mac or iPhone.
Any help would be greatly appreciated.
thanks,
apurva
According to this post, you cannot manually set the Content-ID
How to add a UIImage in MailComposer Sheet of MFMailComposeViewController
OK. Figured it out -- more by accident than anything else.
The null content ids seem to be inserted by the mail server (not my app or MFMailComposeViewController).
Initially I was testing the send with a personal yahoo account. Thats when I ran into the problem. When I switched to my work account the problem disappeared.
thanks,
apurva

Easy way to open the Mail application with an pre-defined message subject and body?

In my app the user generates text content. I want to enable the user to launch the Mail application, which then should contain a specified subject and message body. Like: You write a poem in my app and then want to send it to your new girlfriend. So you tap a mail icon and the Mail app opens, containing already an subject and message body with your poem inside.
Someone said there is a kind of URL mechanism for that?
You can use MFMailComposeViewController for sending the mail.
-(void)OpenMail{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"my new poem"];
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
[picker release];
}
emailBody is string which contains the poem.
All the best.
Apple URL Scheme Reference: Mail Links lists all your options.
Or you use MFMailComposeViewController.

Class to manage e-mail from iPhone

I'm working on an iPhone app that offers the user the opportunity to send an e-mail in 3 different places in the app, and for 3 different purposes.
Rather than put the same code for showing the e-mail composer in 3 different view controllers, shouldn't I develop a separate E-mail class, create an instance, and then set properties such as To, CC, BCC, Body, HTML_Or_Not, and so on?
Also, if I create an instance of such a class, and it brings up the e-mail composer, is it OK to release the class even before the e-mail composer has left the screen?
My advice, It's so easy to use the built in mail picker class, just stick with it, you can create a function to setup and show the picker, and use that when you need to like so:
- (void)showMailPicker {
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
picker.navigationBar.barStyle = UIBarStyleBlack;
[picker setToRecipients: ...];
[picker setSubject:#"Title"];
// Fill out the email body text
NSString *emailBody = #"email Body...";
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
[picker release];
}
}
Yes, it's safe to release the picker once you have presented it, and remember that once you present the picker, you can't change the email addresses, subject, body etc...
My aproach would me to encapsulate the whole process of creating, initialising, displaying, dismissing and handling all of the aspects of the mail composer in a class, like a "mail manager" or something.
Then you can create and instance and set the properties you need, and then call "show mail composer" or something to that effect.
I wouldn't recommend releasing this manager class if it will be dealing with the dismissal and handling of the result of the mail composer (ie, handling an error when sending) until after it has finished what it's doing. If you release it too early, you may not have any logical way to dismiss the mail composer or gracefully handle it's results etc.

MFMailComposeViewController attachment file size limit

I'm using MFMailComposeViewController to send a file. Everything works fine with files under 15mb. Anything over, and the file simply doesn't get attached to the MFMailComposeViewController view. It's not that the email server isn't accepting, its that it never gets attached in the first place.
Does anyone have any ideas if there's a way to resolve that?
I know many email services can't handle attachments over 5 or 10mb, but other services allow you much larger file size.
MFMailComposeViewController *mail = [[[MFMailComposeViewController alloc] init] autorelease];
mail.mailComposeDelegate = self;
NSString* path = [NSString stringWithFormat:#"%#/%#/%#", NSHomeDirectory(), #"Documents", fileName];
NSData *data = [NSData dataWithContentsOfMappedFile:path]; //also tried dataWithContentsOfFile with same results
[mail addAttachmentData:data mimeType:#"audio/x-caf" fileName:[NSString stringWithFormat:#"%#.caf", label]];
[appDelegate.tabBarController presentModalViewController:mail animated:YES];
Loading a 15 MB anything into RAM on a pre-2009 iPhone or iPod is going to really push the limits of your process's available memory, so it's not surprising there's a cap. I wouldn't try to convince the compose view to accept a larger attachment; instead, I'd suggest you transfer the file to some external server and either e-mail a link to it or send the mail from there.
Currently it has a 21mb limit (relevant to iOS 15). But I could sent at least 24mb when pressed Try. 25mb couldn't be attached and MFMailComposeViewController was closed by system at all.