2013-03-21 109 views
0

我有一個應用程序,人們可以填寫一些數據並將其發送到我的電子郵件。郵件發送正常,但郵件正文爲空。通過服務器發送電子郵件

我爲連接方法:

-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex { 

    if(!(buttonIndex == [actionSheet cancelButtonIndex])) { 

     NSString *post = nil; 
     post = [[NSString alloc] initWithFormat:@"Naam spel = %@ Uitleg= %@", spelnaam.text ,Uitleg.text]; 
     NSLog(@"dit wordt verstuurd %@", post); //works fine! 

     NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
     NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; 
     NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
     [request setURL:[NSURL URLWithString:@"http://www.myserver.nl/theemailfile.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 ]; 

     [post release]; 
     [self displayAlert]; 
    } 
} 

我的PHP文件:

<?php 
$message = $_REQUEST['message'] ; 
mail("[email protected]", "Test Message", $message); 
?> 
+0

您需要隔離,這是否是您的客戶端應用程序,或者是否這是你的服務器。你可以控制兩個,是嗎?首先,在做'NSURLConnection'之前確保「'postData'」不是null(例如,在Xcode中設置斷點)。 – 2013-03-21 11:21:39

+0

你爲什麼不使用內置郵件API發送郵件? – 2013-03-21 11:23:46

+0

使用內置郵件Api,用戶必須啓用電子郵件帳戶 – 2013-03-21 11:56:42

回答

2

在你的HTTP請求,你說你要發送的請求的主體的信息類型爲application/x-www-form-urlencoded,但事實並非如此。它似乎只是自由形式的文本。你應該符合the format described in the HTML specification。你發送的消息,看起來像這樣:

Naam spel = test Uitleg= test 

您需要發送這樣的事情:

message=This+is+a+test&other=test 
+0

郵件是包含naam和uitleg的字符串'post'。 – 2013-03-21 11:53:39

+0

得到它的工作表示感謝! – 2013-03-21 12:36:35