Advertisement
Guest User

Untitled

a guest
Jun 27th, 2015
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. - (IBAction)simple3:(id)sender{
  2.  
  3. NSLog(@"The document: %@", theDocument);
  4.  
  5.  
  6. NSString* url = [theDocument absoluteString];
  7. NSLog(@"The document: %@", url);
  8.  
  9.  
  10. NSString* from = @"Excited Sundsx <mailgun@xvcvx.com>";
  11. NSString* to = @"sxxxxx@gmail.com";
  12. NSString* subject = @"Attach From Objective-c App";
  13. NSString* text = @"Hello World";
  14.  
  15. //test
  16. NSImage *myImage = [[NSImage alloc]initWithContentsOfURL:theDocument];
  17. if (myImage != nil)
  18. {
  19. NSLog(@"Image seem to be ok");
  20. }else
  21. NSLog(@"Image seem to be wrong");
  22.  
  23. NSData *imageData;
  24. NSString *image_name;
  25. NSImage *image;
  26.  
  27. //-- Convert string into URL
  28. NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.mailgun.net/v3/bbbbbbb.me/messages"]];
  29. NSString *authStr = @"api:key-00000000000000000000";
  30. NSData *authData = [authStr dataUsingEncoding:NSUTF8StringEncoding];
  31.  
  32. NSString *authValue = [NSString stringWithFormat: @"Basic %@",[authData base64EncodedStringWithOptions:0]];
  33. [request setValue:authValue forHTTPHeaderField:@"Authorization"];
  34.  
  35. [request setHTTPMethod:@"POST"];
  36.  
  37. NSString *boundary = @"14737809831466499882746641449";
  38. NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
  39. [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
  40.  
  41. //-- Append data into posr url using following method
  42. //-- Append data into posr url using following method
  43. NSMutableData *body = [NSMutableData data];
  44.  
  45. [body appendData:[[NSString stringWithFormat:@"rn--%@rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  46. [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="%@"rnrn",@"from"] dataUsingEncoding:NSUTF8StringEncoding]];
  47. [body appendData:[[NSString stringWithFormat:@"%@",from] dataUsingEncoding:NSUTF8StringEncoding]];
  48.  
  49. [body appendData:[[NSString stringWithFormat:@"rn--%@rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  50. [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="%@"rnrn",@"to"] dataUsingEncoding:NSUTF8StringEncoding]];
  51. [body appendData:[[NSString stringWithFormat:@"%@",to] dataUsingEncoding:NSUTF8StringEncoding]];
  52.  
  53. [body appendData:[[NSString stringWithFormat:@"rn--%@rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  54. [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="%@"rnrn",@"subject"] dataUsingEncoding:NSUTF8StringEncoding]];
  55. [body appendData:[[NSString stringWithFormat:@"%@",subject] dataUsingEncoding:NSUTF8StringEncoding]];
  56.  
  57. [body appendData:[[NSString stringWithFormat:@"rn--%@rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  58. [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="%@"rnrn",@"text"] dataUsingEncoding:NSUTF8StringEncoding]];
  59. [body appendData:[[NSString stringWithFormat:@"%@",text] dataUsingEncoding:NSUTF8StringEncoding]];
  60.  
  61.  
  62. // here loop through mutableArray populated from openPal [openPanel URLs]
  63. lika as pict.
  64.  
  65. NSLog(@"ARRAYCOUNT: %lu", (unsigned long)filesArrayPath.count);
  66. for (int y = 0; y < [filesArrayPath count]; y++) {
  67. [body appendData:[[NSString stringWithFormat:@"rn--%@rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  68. image_name = [filesArrayPath objectAtIndex:y];
  69. NSData *dataImg = [[NSData alloc]initWithContentsOfURL:[filesArrayPath objectAtIndex:y]];
  70. NSLog(@"added %i", y+1);
  71.  
  72. [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="attachment"; filename="%@"rn",image_name] dataUsingEncoding:NSUTF8StringEncoding]];
  73. [body appendData:[@"Content-Type: application/octet-streamrnrn" dataUsingEncoding:NSUTF8StringEncoding]];
  74. [body appendData:[NSData dataWithData:dataImg]];
  75. }
  76.  
  77. [body appendData:[[NSString stringWithFormat:@"rn--%@--rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  78.  
  79. //-- Sending data into server through URL
  80. [request setHTTPBody:body];
  81.  
  82. //-- Getting response form server
  83. NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
  84.  
  85. //-- JSON Parsing with response data
  86. NSDictionary *result = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil];
  87. NSLog(@"Result = %@",result);
  88.  
  89.  
  90. }
  91.  
  92. curl -s --user 'api:YOUR_API_KEY'
  93. https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/messages
  94. -F from='Excited User <YOU@YOUR_DOMAIN_NAME>'
  95. -F to='foo@example.com'
  96. -F cc='bar@example.com'
  97. -F bcc='baz@example.com'
  98. -F subject='Hello'
  99. -F text='Testing some Mailgun awesomness!'
  100. --form-string html='<html>HTML version of the body</html>'
  101. -F attachment=@files/cartman.jpg
  102. -F attachment=@files/cartman.png
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement