tags:

views:

516

answers:

3

Hello,

I'd like a user to be able to enter a string into a textfield, press done, and have the data emailed to my inbox, but I have no idea how to go about doing this. Could somebody describe the basic steps I need to look into, things I need to learn, recommend tutorials, sample code, etc. I would like to avoid opening the mail app if possible.

Thanks.

+3  A: 

Look in the apple documentation for UI Message framework at http://developer.apple.com/iphone/library/documentation/MessageUI/Reference/MessageUI_Framework_Reference/MessageUI_Framework_Reference.pdf, they hasve MFMailComposeViewController etc, I never used them, they provide UIs to write emails, I dont know if you can behind the scenes make it send an email (maybe its possible), youll have to look through the docs and figure it out. If it doesnt do it for you, you can always make an SMPT protocol and send the emails yourself...(you should be able to find something of use in the UI Message framework though i would think)

Daniel
This does look like the right method, assuming that you aren't just sending a canned message that the user doesn't need to edit. It also has the advantage that sending the email does not force the user to exit your application.
Amagrammer
+2  A: 

If you want the email to come from the user's own personal account then you have to either:

  1. Use the built-in MFMailComposeViewController (which puts up a mail editing page) or
  2. Use an SMTP client library like http://code.google.com/p/skpsmtpmessage/. However, in this case, you'll have to get SMTP account information from the user (i.e. userid, email address, password).

If all you want is the body of the text box and you don't care about the return email address you can set up a simple mail re-sender web service (there's tons of PHP/Ruby/Python code out there for sending email from a script). Then behind the scenes your iPhone app fires off an HTTP request to your web-server who then formats it as a mail message and forwards it to you.

This is probably the most user-friendly since it requires minimal input from the user, but you may want to put up safeguards (like using SSL and/or authentication) to restrict access to the web-service URL from only your app.

Ramin
A: 

Something like this should work:

NSString *s = @"mailto:[email protected]?subject=iPhone%20Question&body=Where%20is%20mine?";
NSURL *url = [[NSURL alloc] initWithString: s];
[[UIApplication sharedApplication] openURL: url];

That's about right, I think. The %20's are for embedded spaces -- if you put real values in place of the ones I have hard-coded above, you will want to do text-replaces for spaces and other problematic characters in the URL. I'm no expert, perhaps someone else can point to a guide.

Note that the application is exited if the openURL call succeeds, so you don't really have to worry about cleaning up things like "url" above.

Alternatively, that MFMailComposeViewController referenced above might be the right way to go -- I'm going to look into it.

Amagrammer