I wanted to send an email via a websever and php. Therefore I created a connection to my webserver (000webhoster). Nearly everything works fine.
My main problem is, that I don't receive anything. I am not even sure, if the PHP code is even executed.
This is my current swift code:
func postToServer (){
print("button pressed")
var url: NSURL = NSURL(string: "http://www.bl1nd3d.herobo.com/email.php")!
var request: NSMutableURLRequest = NSMutableURLRequest(URL:url)
var bodyData = "data=something"
request.HTTPMethod = "POST"
request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding)
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler:{ response, data, error in
if let HTTPResponse = response as? NSHTTPURLResponse {
let statusCode = HTTPResponse.statusCode
if statusCode == 200 {
print("nice")
// TODO : Sent successfully popup
}
else {
print(statusCode)
}
}
})
}
This works fine ^^ .. I always receive the statuscode of 200. (If you wanted to try this you need to enable the insecure AppTransport)
And this is my PHP code.. I believe this doesn't work that properly..
<?php
$postVar = $_POST['data'];
if (!empty($postVar)) {
echo $postVar;
// email , subject, message
mail("[email protected]","Agent was booked","The POST was set and button was pressed" . $postVar);
}
echo "doesn't work that good " . $postVar;
?>
I checked, it should be possible with my webhoster, to send emails..
But it doesn't work.. Did I forget something?
Well something must have gone really bad wrong... My whole dir was deleted while trying this.. :O
The directory /public_html does not exist or could not be selected, so the directory / is shown instead.
well it probably didn't work that well.. My account was blocked...
Did you check the spam folder? Standard mail() function is not a great solution to send emails. Your mail provider can block email's sent from mail() or it just couldn't work at all. You need to send mail using smtp. Here's the example
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With