MFMailComposeViewController in iphone app - iphone

hii every one
can we use " MFMailComposeViewController " with out using " presentModalViewController" , i mean i need to send email with out navigating to that mail composer page
i did a test project with following code
- (IBAction)buttonPressed {
arrRecipients = [[NSMutableArray alloc]init];
[arrRecipients addObject:#"xxxxxxx#gmail.com"];
MFMailComposeViewController *mailController = [[MFMailComposeViewController alloc] init];
mailController.mailComposeDelegate = self;
[mailController setSubject:#"Ravikiran test mail"];
[mailController setToRecipients:arrRecipients];
[mailController setMessageBody:#"this is my test app" isHTML:YES];
[self presentModalViewController:mailController animated:YES];
[mailController release];
}
it is sending mail but it is navigating to mail composer page & then its sending but i need to send mail just on click of the button

No, you cannot use MFMailComposeViewController to send an email without user interaction. There are probably 3rd parties libraries that let you do this, but the iOS SDK does not allow you to.
Also, you're leaking the arrRecipients array.

Best way for your needs is to have a web service which sends the data to an email id.
its always to good to inform the user in some way before sending the mail.
Thanks!

Related

How to send a message in iPhone devices?

Currently i am working in iPhone application, Using MFMessageComposeViewController to develop this application and its working fine.
But i want, message compose screen don't show on the screen, then the Message send programmatically ,
How to do this? it is possible? please help me
I tried this:
MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];
if([MFMessageComposeViewController canSendText])
{
controller.body = #"Hi";
controller.recipients = [NSArray arrayWithObjects:#"12345678", #"87654321", nil];
controller.messageComposeDelegate = self;
[self presentModalViewController:controller animated:YES];
}
I've tried to do this once but but it wont be accepted by app store. Apple wont let you send message/email without user knowledge about it.
You can use a sms:[target phone number] URL to open the SMS application, but there are no indications on how to prefill a SMS body with text (see this post on Apple Developer Forums).
You can't use the Message Framework to send a message without the user knowing about it. One common solution is to send the message details to a web service running on your server and send the message from the server. As long as you're not trying to be sneaky, I don't think there's anything wrong with doing that.

Attachments using email uri scheme in iOS

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.

Sending email through iPhone application

I'd like to setup a button in the iPhone app I'm making to send an email with the results of some operations performed earlier in the app. I've read other people comment on here that you can use other frameworks but, frankly, I can't find one that actually works. I've heard MailCore is good. Has anyone been able to send an email seamlessly through an iPhone app?
Note: I do not want to exit the app to send the email.
Answer From this Question: How can I send mail from an iPhone application
On iPhone OS 3.0 and later you should use the MFMailComposeViewController class, and the MFMailComposeViewControllerDelegate protocol, that that tucked away in the MessageUI framework. Note you must link the Message UI Framework and import its headers.
#import <MessageUI/MFMailComposeViewController.h>
First to send a message:
MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setSubject:#"My Subject"];
[controller setMessageBody:#"Hello there." isHTML:NO];
if (controller) [self presentModalViewController:controller animated:YES];
[controller release];
Then the user does the work and you get the delegate callback in time:
- (void)mailComposeController:(MFMailComposeViewController*)controller
didFinishWithResult:(MFMailComposeResult)result
error:(NSError*)error;{
if (result == MFMailComposeResultSent) {
NSLog(#"It's away!");
}
[self dismissModalViewControllerAnimated:YES];
}
In this case, your best bet is to use a service like Amazon Simple Email Service (SES). It will enable you to send the email without ever leaving the application (unlike the other solutions listed). Amazon AWS has an iOS SDK which can utilize the SES service. This service is super simple to use, and the cost is extremely low.
http://aws.amazon.com/sdkforios/
MFMailComposeViewController *mailComposer =[[MFMailComposeViewController alloc] init];
mailComposer.mailComposeDelegate = self;
NSString *emailBody = #"Write email body text here........";
[mailComposer setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:mailComposer animated:YES];
[mailComposer release];

Email body not displayed after sending from iPhone via code

I have couple of iPhone apps that sends an email via code.
I've noticed quite a number of emails coming in from customers where everything is populated except for the body message (i always see their signature though).
I sent a test message, using the same app from my iPhone device, and i get the message (a simple "this is a test" message).
Wondering if there are any coding that needs to occur to handle different version of iOS, or perhaps different iPhone device hardwares?
Anybody noticed this before?
I would consider the possibility that the users are accidentally pressing send on a blank email. Perhaps to eliminate this as a possibility, guard against blank messages in code, then if it still happens you'll know it's a real issue.
set all delegates properly
and use this
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
NSString *sub= [NSString stringWithFormat:#"The Black Sheep App: %# Specials",[[BarSplArr objectAtIndex:0]objectForKey:#"barname"]];
NSString *message=[NSString stringWithFormat:#"Check out these specials for %# on %#, %# \n\n %#",[[BarSplArr objectAtIndex:0]objectForKey:#"barname"],day,date,[[BarSplArr objectAtIndex:0]objectForKey:#"drinkspecials"] ];
[controller setSubject:sub];
[controller setMessageBody:message isHTML:NO];
[self presentModalViewController:controller animated:YES];
[controller release];

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.