//This example send email with attachment using server side //So iPhone will fill form and send proper request to web server using POST method //For test you can use attached sendEmail.php -(NSMutableData *)generateDataFromText:(NSString *)dataText fieldName:(NSString *)fieldName{ NSString *post = [NSString stringWithFormat:@"--AaB03x\r\nContent-Disposition: form-data; name=\"%@\"\r\n\r\n", fieldName]; // Get the post header int ASCII format: NSData *postHeaderData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; // Generate the mutable data variable: NSMutableData *postData = [[NSMutableData alloc] initWithLength:[postHeaderData length] ]; [postData setData:postHeaderData]; NSData *uploadData = [dataText dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; // Add the text: [postData appendData: uploadData]; // Add the closing boundry: [postData appendData: [@"\r\n" dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]]; // Return the post data: return postData; } - (NSData *)generatePostDataForData:(NSData *)uploadData fileName:(NSString *)fileName{ // Generate the post header: NSString *post = [NSString stringWithFormat:@"--AaB03x\r\nContent-Disposition: form-data; name=\"attachment\"; filename=\"%@\"\r\nContent-Type: video/3gpp\r\n\r\n", fileName]; // Get the post header int ASCII format: NSData *postHeaderData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; // Generate the mutable data variable: NSMutableData *postData = [[NSMutableData alloc] initWithLength:[postHeaderData length] ]; [postData setData:postHeaderData]; // Add the image: [postData appendData: uploadData]; // Add the closing boundry: [postData appendData: [@"\r\n--AaB03x--" dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]]; // Return the post data: return postData; } -(void)sendEmailTo:(NSString *)email name:(NSString *)name { NSString *emailBody = @"Just some HTML text" //Fill some text fields NSMutableData *postData = [self generateDataFromText:email fieldName:@"to_email"]; [postData appendData:[self generateDataFromText:@"10000000" fieldName:@"MAX_FILE_SIZE"]]; [postData appendData:[self generateDataFromText:name fieldName:@"to_name"]]; [postData appendData:[self generateDataFromText:email fieldName:@"to_email"]]; [postData appendData:[self generateDataFromText:[[NSUserDefaults standardUserDefaults] stringForKey:@"name_preference"] fieldName:@"from_name"]]; [postData appendData:[self generateDataFromText:[[NSUserDefaults standardUserDefaults] stringForKey:@"email_preference"] fieldName:@"from_email"]]; [postData appendData:[self generateDataFromText:currentEmail.emailSubject fieldName:@"subject"]]; [postData appendData:[self generateDataFromText:emailBody fieldName:@"body"]]; [postData appendData:[self generateDataFromText:email fieldName:@"to_email"]]; //Prepare data for file NSData *dataObj = [NSData dataWithContentsOfFile:@"/link/to/file"]; [postData appendData:[self generatePostDataForData:dataObj fileName:@"fileName"]]; // Setup the request: NSMutableURLRequest *uploadRequest = [[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:kLinkToPostFile] cachePolicy: NSURLRequestReloadIgnoringLocalCacheData timeoutInterval: 30 ] autorelease]; [uploadRequest setHTTPMethod:@"POST"]; [uploadRequest setValue:[NSString stringWithFormat:@"%d", [postData length]] forHTTPHeaderField:@"Content-Length"]; [uploadRequest setValue:@"multipart/form-data; boundary=AaB03x" forHTTPHeaderField:@"Content-Type"]; [uploadRequest setHTTPBody: postData]; NSData *responseData = [NSURLConnection sendSynchronousRequest:uploadRequest returningResponse:nil error:nil]; NSLog([[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding]); }