15 Feb 2009, 6:49pm

leave a comment

Sending emails on the iPhone with attachments

Sending an XML document from the iPhone is a non-trivial exercise. While Apple can add attachments (e.g. the Photo application), us poor developers do not have this luxury.

To send you will need to firstly encode and escape your data using this code, where yourString is your string. This code is useful for escaping any data sent via HTTP URLs. Thanks to Firefly for the code (he provides a complete sample of how to launch the Mail app with your custom message which I have also used).

NSString *reserved = @”:/?#[]@!$&’()*+,;=”;
return [NSMakeCollectable(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)yourString, NULL, (CFStringRef)reserved, kCFStringEncodingUTF8)) autorelease];

Once encoded, you will probably need to send it using a POST HTTP request as the data may exceed the limits of a GET HTTP request.

Doing that is a little more involved, here’s my code based off these two useful samples

NSString *reserved = @”:/?#[]@!$&’()*+,;=”;
NSString* escapedData = [NSMakeCollectable(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)data, NULL, (CFStringRef)reserved, kCFStringEncodingUTF8)) autorelease];

NSString* httpBodyString=[[NSMutableString alloc] initWithFormat:@”to=%@&DATA=%@”, emailAddress.text, escapedData];

NSString* urlString=[[NSMutableString alloc] initWithString:@”http://domain/script.php”];
NSURL* url=[[NSURL alloc] initWithString:urlString];
[urlString release];

NSMutableData *reqData =[[httpBodyString dataUsingEncoding:NSUTF8StringEncoding] retain];
NSMutableURLRequest *urlRequest=[NSMutableURLRequest requestWithURL:url];
[url release];

[urlRequest setHTTPMethod:@"POST"];
[urlRequest setValue:[NSString stringWithFormat:@"%i", [reqData length]] forHTTPHeaderField:@”Content-Length”];
[urlRequest setHTTPBody:reqData];
[httpBodyString release];

[webView loadRequest:urlRequest];

The last line I am particularly proud of. Sure you can do NSURLConnection *connectionResponse = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self]; but then you have to handle the results problematically and NSURLConnection returns raw binary data which is a bit annoying.

So what I do is simply load it into a UIWebView so I can view the results (or errors) from my PHP script. In the final version I’ll just hide that web-view or even keep it for user feedback. The UIWebView handles all the delegate methods. Even if you do want to handle it in code – I suggest you use this as a way to debug your PHP script.

Almost done – now we need to actually send the email!

This PHP script is a very good example of how to send email with attachments, very easy to tweak to accept your $_POST data (and not load it from a file), and change the attachment type to XML.

And that’s it. I couldn’t find all this info in the one place – as you can see by all the link, so here it is!

*name

*e-mail

web site

leave a comment


 

free winrar download

free winrar download

winrar download free

winrar download free

windows 7 key generator

windows 7 key generator

windows 7 activation crack

windows7 activation crack

free winzip

free winzip

free winrar

free winrar

winzip free download

winzip free download

winrar free download

winrar free download

windows 7 crack

windows 7 crack

winzip free download full version

winzip free download full version

winzip activation code

winzip activation code

windows 7 product key

windows 7 product key
\n