Sending emails from Arduino using a web server and php

Hello there,

I’m using an arduino UNO and ENC28J60 to send an email. I’m using the UIPEthernet library. I’m able to connect to the server (I create one in 000webhost) but I got an error when I try to do the HTTP request. This is what I get:

Connecting to server…
connected to mail server.
GET/sendemail.php?text=PRUEBA HTTP/1.1
HTTP/1.1 400 Bad Request
Date: Mon, 24 Sep 2018 22:50:25 GMT
Content-Type: text/html
Content-Length: 170
Connection: close
Server: awex
X-Xss-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-Request-ID: 6d21b278fc1dfe563c97ddcd70629b7c

400 Bad Request

400 Bad Request


openresty Host: invernaderounal.000webhostapp.com

And this is my php code:

Sending HTML email using PHP <?php $recipients = array( "xxx@gmail.com", "yyy@gmail.com" // more emails ); $to = implode(',', $recipients); //Receiver email. $subject = "Alerta Invernadero"; $message = $_GET['text']; //Text variable will be the message content $header = "From: Arduino\r\n"; $header .= "Cc:\r\n"; $header .= "MIME-Version: 1.0\r\n"; $header .= "Content-type: text/html\r\n"; $retval = mail ($to,$subject,$message,$header); if( $retval == true ) { echo "Message sent successfully..."; }else { echo "Message could not be sent..."; } ?>

And this is the moment I try to do the HTTP request with arduino:

…int sendAndWaitForReply(const __FlashStringHelper* txtToSend) {
next = millis() + 5000;
client.println(txtToSend);
Serial.println(txtToSend);
if (!waitForReply())
return 0;

return 1;
}

if (!connectToServer())
return 0;

if (!sendAndWaitForReply(F(“GET /sendemail.php?text=PRUEBA HTTP/1.1”)))
return 0;

if (!sendAndWaitForReply(F(“Host: mydomain.000webhostapp.com”)))
return 0;

if (!sendAndWaitForReply(F(“Connection: close”)))
return 0;

I don’t know what is going on. I’d really appreciate any help.

Does your PHP code work when called from a browser? If not, you have a PHP problem, and this is not the PHP forum.

  if (!waitForReply())
    return 0;

  return 1;

Why not just

   return !waitForReply();

?