Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS JSON sending an email from Mandrill

I have an IOS application that I would like to send an email via Mandrill. I have tried to implement this, but its not working and Ive confused myself.

When pressing the button to send an email from the IOS application I log this error message:

{"status":"error","code":-1,"name":"ValidationError","message":"You must specify a key value"}

My code is:

    NSString *post = [NSString stringWithFormat:@"{\"key\": \"abcdefg123456\", \"raw_message\": \"From: [email protected]\nTo: [email protected]\nSubject: Some Subject\n\nSome content.}"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"https://mandrillapp.com/api/1.0/messages/send-raw.json"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
    NSLog(@"Post: %@", post);

NSURLResponse *response;
NSData *POSTReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
NSString *theReply = [[NSString alloc] initWithBytes:[POSTReply bytes] length:[POSTReply length] encoding: NSASCIIStringEncoding];
NSLog(@"Reply: %@", theReply);

Please let me know where I am going wrong.

like image 201
Steve Avatar asked Jul 13 '14 17:07

Steve


1 Answers

It looks you forgot the \" after "content.".

Try to write your "post" variable as follow:

NSString *post = [NSString stringWithFormat:@"{\"key\": \"abcdefg123456\", \"raw_message\": \"From: [email protected]\nTo: [email protected]\nSubject: Some Subject\n\nSome content.\"}"];

I hope it helps.

like image 106
scollaco Avatar answered Oct 22 '22 09:10

scollaco