I’m trying to send an email from my application with a PHP contact form, but the details can’t be sent from the application for some reason. Please tell me what is the problem.. I’ve attached the the php form i’m using at the end.
when I try to send an email using the web-browser and writing: http://www.bergerz.co.il/wp-content/themes/ecobiz/sendemailfromapp.php/?name=MYNAME&subject=SUBJECT&email=yositsa@gmail.com&message=MESSAGE, it’s working perfectly.
-(IBAction)send:(id)sender
{
NSMutableURLRequest * request = [[NSMutableURLRequest alloc] init];
NSString * urlString = [NSString stringWithFormat:@"http://www.bergerz.co.il/wp-content/themes/ecobiz/sendemailfromapp.php"];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"REQUEST"];
NSString * paramDataString = [NSString stringWithFormat:@"name=%@&subject=%@&email=%@&message=%@",[txtName.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[txtSubject.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[txtEmail.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[txtMessage.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData*paramData = [paramDataString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:paramData];
NSURLResponse * response;
NSError * error;
NSData * aData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];//excute of data to the internet
NSString * requestData = [[NSString alloc] initWithData:aData encoding:NSUTF8StringEncoding];
if ([requestData isEqualToString:[NSString stringWithFormat:@"sent"]])
{
lblStatus.hidden = NO;
lblStatus.text = @"Message Sent..";
}
else if ([requestData isEqualToString:[NSString stringWithFormat:@"NO_NAME_ERROR"]])
{
lblStatus.hidden = NO;
lblStatus.text = @"Name is too short";
}
else if ([requestData isEqualToString:[NSString stringWithFormat:@"NO_EMAIL_ERROR"]])
{
lblStatus.hidden = NO;
lblStatus.text = @"Email is too short";
}
else if ([requestData isEqualToString:[NSString stringWithFormat:@"EMAIL_WRONG_ERROR"]])
{
lblStatus.hidden = NO;
lblStatus.text = @"Email wrong error";
}
else if ([requestData isEqualToString:[NSString stringWithFormat:@"NO_MESSAGE_ERROR"]])
{
lblStatus.hidden = NO;
lblStatus.text = @"Message is too short";
}
else if ([requestData isEqualToString:@"NULL"])
{
lblStatus.hidden = NO;
lblStatus.text = @"EMPTY";
}
NSLog(@"request: %@",request);
NSLog(@"requestData: %@",requestData);
NSLog(@"aData: %@",aData);
}
here is the php file: http://www.pastebin.com/BvaiMs04
I’ve edited the message to my latest code, still he isn’t working.
ParamDataString should be
name=%@&subject=%@...without the/?.AND: You have to escape the input strings:
[txtName.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[txtSubject.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] ...or fill everything into a NSDictionary and use
urlEncodedString:Found another bug: HTTP method should be POST, “_REQUEST” does not exist…
[request setHTTPMethod:@"POST"];