Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- - (IBAction)simple3:(id)sender{
- NSLog(@"The document: %@", theDocument);
- NSString* url = [theDocument absoluteString];
- NSLog(@"The document: %@", url);
- NSString* from = @"Excited Sundsx <mailgun@xvcvx.com>";
- NSString* to = @"sxxxxx@gmail.com";
- NSString* subject = @"Attach From Objective-c App";
- NSString* text = @"Hello World";
- //test
- NSImage *myImage = [[NSImage alloc]initWithContentsOfURL:theDocument];
- if (myImage != nil)
- {
- NSLog(@"Image seem to be ok");
- }else
- NSLog(@"Image seem to be wrong");
- NSData *imageData;
- NSString *image_name;
- NSImage *image;
- //-- Convert string into URL
- NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.mailgun.net/v3/bbbbbbb.me/messages"]];
- NSString *authStr = @"api:key-00000000000000000000";
- NSData *authData = [authStr dataUsingEncoding:NSUTF8StringEncoding];
- NSString *authValue = [NSString stringWithFormat: @"Basic %@",[authData base64EncodedStringWithOptions:0]];
- [request setValue:authValue forHTTPHeaderField:@"Authorization"];
- [request setHTTPMethod:@"POST"];
- NSString *boundary = @"14737809831466499882746641449";
- NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
- [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
- //-- Append data into posr url using following method
- //-- Append data into posr url using following method
- NSMutableData *body = [NSMutableData data];
- [body appendData:[[NSString stringWithFormat:@"rn--%@rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
- [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="%@"rnrn",@"from"] dataUsingEncoding:NSUTF8StringEncoding]];
- [body appendData:[[NSString stringWithFormat:@"%@",from] dataUsingEncoding:NSUTF8StringEncoding]];
- [body appendData:[[NSString stringWithFormat:@"rn--%@rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
- [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="%@"rnrn",@"to"] dataUsingEncoding:NSUTF8StringEncoding]];
- [body appendData:[[NSString stringWithFormat:@"%@",to] dataUsingEncoding:NSUTF8StringEncoding]];
- [body appendData:[[NSString stringWithFormat:@"rn--%@rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
- [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="%@"rnrn",@"subject"] dataUsingEncoding:NSUTF8StringEncoding]];
- [body appendData:[[NSString stringWithFormat:@"%@",subject] dataUsingEncoding:NSUTF8StringEncoding]];
- [body appendData:[[NSString stringWithFormat:@"rn--%@rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
- [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="%@"rnrn",@"text"] dataUsingEncoding:NSUTF8StringEncoding]];
- [body appendData:[[NSString stringWithFormat:@"%@",text] dataUsingEncoding:NSUTF8StringEncoding]];
- // here loop through mutableArray populated from openPal [openPanel URLs]
- lika as pict.
- NSLog(@"ARRAYCOUNT: %lu", (unsigned long)filesArrayPath.count);
- for (int y = 0; y < [filesArrayPath count]; y++) {
- [body appendData:[[NSString stringWithFormat:@"rn--%@rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
- image_name = [filesArrayPath objectAtIndex:y];
- NSData *dataImg = [[NSData alloc]initWithContentsOfURL:[filesArrayPath objectAtIndex:y]];
- NSLog(@"added %i", y+1);
- [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="attachment"; filename="%@"rn",image_name] dataUsingEncoding:NSUTF8StringEncoding]];
- [body appendData:[@"Content-Type: application/octet-streamrnrn" dataUsingEncoding:NSUTF8StringEncoding]];
- [body appendData:[NSData dataWithData:dataImg]];
- }
- [body appendData:[[NSString stringWithFormat:@"rn--%@--rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
- //-- Sending data into server through URL
- [request setHTTPBody:body];
- //-- Getting response form server
- NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
- //-- JSON Parsing with response data
- NSDictionary *result = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil];
- NSLog(@"Result = %@",result);
- }
- curl -s --user 'api:YOUR_API_KEY'
- https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/messages
- -F from='Excited User <YOU@YOUR_DOMAIN_NAME>'
- -F to='foo@example.com'
- -F cc='bar@example.com'
- -F bcc='baz@example.com'
- -F subject='Hello'
- -F text='Testing some Mailgun awesomness!'
- --form-string html='<html>HTML version of the body</html>'
- -F attachment=@files/cartman.jpg
- -F attachment=@files/cartman.png
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement