Reputation: 1619
My application is able to send the input of several textfields to a PHP script on my server using the POST function which sends the message to my email. The problem is, some characters won't get 'translated'. For example: ü -> ü.
I have already checked my HTTP Header but it's still UTF-8.
I also use this code to convert the characters:
[textFieldName.text stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversation:YES];
What am I doing wrong? Even the PHP file has this code:
function mail_utf8($to,$subject = '(No subject)',$message = '',$header = ''){
$header_ = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/plain; charset=UTF-8' . "\r\n";
if(mail($to, '=?UTF-8?B?'.base64_encode($subject).'?=', $message, $header_ . $header)) return true;
else return false;
}
EDIT:
NSString *post = nil;
post = [[NSString alloc] initWithFormat:@"vorname=%@&name=%@&plz=%@&ort=%@&email=%@&geburtsdatum=%@&eventdatum=%@",
[textFieldVorname.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[textFieldName.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[textFieldPLZ.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[textFieldOrt.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[textFieldEmail.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[textFieldGeburtsdatum.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[textFieldEventdatum.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://www.url.com/document-send.php"]];
[request setHTTPMethod:@"POST"];
[request setValue: postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
[NSURLConnection connectionWithRequest:request delegate:self];
Upvotes: 0
Views: 1746
Reputation: 1619
Okay, I found the error:
Like Peter Hosey said: the error was in decoding the utf-8 character set to ISO 8859-1 in php. Now this is the working code:
mail($email_to,$email_subject,utf8_decode($email_data),$email_header);
Thank you guys for helping me :)
Upvotes: 0
Reputation: 6560
I fixed a similar problem POSTing JSON data by explicitly adding charset=utf-8
to my Content-Type
header. It may apply to your case, as well.
Try:
[request setValue:@"application/x-www-form-urlencoded;charset=utf-8" forHTTPHeaderField:@"Content-Type"];
Upvotes: 2
Reputation: 2115
The method stringByAddingPercentEscapesUsingEncoding
returns a new NSString object. You need to catch the return value and use it in place of the raw value of the field in the post data.
NSString *encodedText = [ textField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding ] ;
NSData *postData = [ encodedText dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES ] ;
Upvotes: 1