0

I'm trying to manually create a mini mail module with just the user's email address, subject and content of the email. It just serves the purpose for a fast customer support for my app. I also want to hide my email address since i'm just using my gmail account instead of my company's email account. This is to ensure good and timely service since gmail is known for it.

The problem is when i'm sending the 'email' content to my PHP server using:

- (void)sendMail{
    NSString *rawStr = [NSString stringWithFormat:@"http://www.somedomain.com/iosuser.php?id=%@__%@__%@__%@",[self getMacAddress],emailAdd.text,subject.text,content.text;];

    NSLog(@"%@",rawStr);
    NSURL *url = [NSURL URLWithString:rawStr];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    NSURLResponse *response;
    NSError *err;
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
    NSLog(@"responseData: %@", responseData);
}

The NSLog for rawStr works fine, displaying all the contents for the url, email address, subject and the content:

http://www.somedomain.com/iosuser.php?id=C8:2A:14:22:92:E2__test@test.com__SubjectTest__ContentTestContentTestContentTestContentTestContentTestContentTestContentTestContentTestContentTestContentTest

But the NSLog for responseData returns: responseData: (null) so nothing is actually sent.

The emailAdd and subject are UITextField input while the content is from UITextView input. I wonder if UITextView is the problem. Really appreciate any pointers and apologize if this is a foolish issue.

Note:I tried to use the NSLog output of rawStr on my browser to manually test the script and it works.

Joe Shamuraq
  • 1,245
  • 3
  • 18
  • 32

3 Answers3

1

I think UITextView is not the problem. Because you are getting correct log via NSLog(@"%@",rawStr);. You are not getting the proper response from the server, I'm not 100% sure about that. So please check the below links:

-[NSURLRequest sendSynchronousRequest:returningResponse:error:] getting back HTTP headers

error handling with NSURLConnection sendSynchronousRequest

Community
  • 1
  • 1
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • How do i change the synchronous to asynchronous connection? – Joe Shamuraq Jun 15 '12 at 19:23
  • @TeamStar in your question also you are using "sendSynchronousRequest" , is it ? Just for clarification – Midhun MP Jun 15 '12 at 19:29
  • Based on the link u gave me, i noticed that the issue comes from sendSynchronousRequest so i thought by changing it to Asynchronous might work... – Joe Shamuraq Jun 15 '12 at 19:31
  • Please check this link. Please refer the "Loading data Asynchronously" section https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsurlconnection_Class/Reference/Reference.html – Midhun MP Jun 15 '12 at 19:32
1

I think that your problem is the @ sign in the URL (which isn't allowed unless specifying the username I believe)

you should escape all your strings before putting them in the url with

NSString *rawStr = [NSString stringWithFormat:@"http://www.somedomain.com/iosuser.php?id=%@__%@__%@__%@",
                    [[self getMacAddress] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                    [emailAdd.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                    [subject.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                    [content.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
cobbal
  • 69,903
  • 20
  • 143
  • 156
0

It actually falls to the very basic of URL syntax that iOS adopts... No Spaces. I replaced all spaces with underscore using

rawStr = [rawStr stringByReplacingOccurrencesOfString:@" " withString:@"_"];

For the sake of better handling, i changed from Synchronous to Asynchronous.

Joe Shamuraq
  • 1,245
  • 3
  • 18
  • 32
  • This (and other errors that will crop up eventually) would be handled by using `stringByAddingPercentEscapesUsingEncoding:`, which doesn't lose the information that just converting spaces to underscores loses. – cobbal Jun 15 '12 at 21:04