Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 29 additions & 21 deletions src/AlertMe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ uint16_t smtp_port = 0;
char smtp_server[40];
bool needs_save = false;
bool alert_debug = false;
char* last_error = "";
char last_error[35] = "";

bool stmp_connect_fail = false;

Expand All @@ -40,7 +40,10 @@ void DEBUG_AM(Generic text) {
Serial.println(text);
}
}


#define DEBUG_AMF(f_, ...) if (alert_debug) printf((f_), ##__VA_ARGS__)
#define FV(s) (reinterpret_cast<const __FlashStringHelper*>(s))

AlertMe::AlertMe(){}

// BASE64 -----------------------------------------------------------
Expand Down Expand Up @@ -116,20 +119,20 @@ class Gsender
protected:
Gsender();
private:
const char* _error = nullptr;
const __FlashStringHelper* _error = nullptr;
char* _subject = nullptr;
String _serverResponse;
static Gsender* _instance;
bool AwaitSMTPResponse(WiFiClientSecure &client, const String &resp = "", uint16_t timeOut = 10000);

void setError(const __FlashStringHelper* err);
public:
static Gsender* Instance();
Gsender* Subject(const char* subject);
Gsender* Subject(const String &subject);
bool Send(const String &to, const String &message);
bool TestConnection(char* server, uint16_t port);
String getLastResponse();
const char* getError();
void getError();
};

Gsender* Gsender::_instance = 0;
Expand Down Expand Up @@ -159,7 +162,7 @@ bool Gsender::AwaitSMTPResponse(WiFiClientSecure &client, const String &resp, ui
while (!client.available())
{
if (millis() > (ts + timeOut)) {
_error = "SMTP Response TIMEOUT!";
setError(FV(ErrorSMTPTimeout));
return false;
}
}
Expand All @@ -174,9 +177,14 @@ String Gsender::getLastResponse()
return _serverResponse;
}

const char* Gsender::getError()
{
return _error;
void Gsender::getError()
{
// Copy from PROGMEM to RAM
strncpy_P(last_error, reinterpret_cast<const char*>(_error), sizeof(last_error));
}

void Gsender::setError(const __FlashStringHelper* err) {
_error = err;
}

bool Gsender::Send(const String &to, const String &message)
Expand All @@ -185,18 +193,18 @@ bool Gsender::Send(const String &to, const String &message)
DEBUG_AM("Connecting to :");
DEBUG_AM(smtp_server);
if (!client.connect(smtp_server, smtp_port)) {
_error = "Could not connect to mail server";
setError(FV(ErrorNotConnected));
return false;
}
if (!AwaitSMTPResponse(client, "220")) {
_error = "Connection Error";
setError(FV(ErrorConnection));
return false;
}

DEBUG_AM("HELO friend:");
client.println("HELO friend");
if (!AwaitSMTPResponse(client, "250")) {
_error = "identification error";
setError(FV(ErrorIdentification));
return false;
}

Expand All @@ -215,7 +223,7 @@ bool Gsender::Send(const String &to, const String &message)

client.println(encode64(EMAIL_PASSWORD));
if (!AwaitSMTPResponse(client, "235")) {
_error = "SMTP AUTH error";
setError(FV(ErrorAuthentification));
return false;
}

Expand All @@ -232,7 +240,7 @@ bool Gsender::Send(const String &to, const String &message)
DEBUG_AM("DATA:");
client.println("DATA");
if (!AwaitSMTPResponse(client, "354")) {
_error = "SMTP DATA error";
setError(FV(ErrorSMTPData));
return false;
}

Expand All @@ -250,12 +258,12 @@ bool Gsender::Send(const String &to, const String &message)
client.println(body);
client.println(".");
if (!AwaitSMTPResponse(client, "250")) {
_error = "Sending message error";
setError(FV(ErrorNotSent));
return false;
}
client.println("QUIT");
if (!AwaitSMTPResponse(client, "221")) {
_error = "SMTP QUIT error";
setError(FV(ErrorSMTPQuit));
return false;
}
return true;
Expand All @@ -266,18 +274,18 @@ bool Gsender::TestConnection(char* server, uint16_t port) {
DEBUG_AM("Connecting to :");
DEBUG_AM(server);
if (!client.connect(server, port)) {
_error = "Could not connect to mail server";
setError(FV(ErrorNotConnected));
return false;
}
if (!AwaitSMTPResponse(client, "220")) {
_error = "Connection Error";
setError(FV(ErrorConnection));
return false;
}

DEBUG_AM("HELO friend:");
client.println("HELO friend");
if (!AwaitSMTPResponse(client, "250")) {
_error = "identification error";
setError(FV(ErrorIdentification));
return false;
}

Expand All @@ -298,7 +306,7 @@ bool Gsender::TestConnection(char* server, uint16_t port) {

client.println(encode64(EMAIL_PASSWORD));
if (!AwaitSMTPResponse(client, "235")) {
_error = "SMTP AUTH error";
setError(FV(ErrorAuthentification));
return false;
}

Expand Down Expand Up @@ -458,7 +466,7 @@ const char* AlertMe::send(String subject, String message, String dest) {
return "SENT";
}
else {
strcpy(last_error,gsender->getError());
getError();

DEBUG_AM("Message sending failed. (");
DEBUG_AM(last_error);
Expand Down
10 changes: 10 additions & 0 deletions src/AlertMe.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@
#include <WiFiClientSecure.h>
#include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson

static const char ErrorSendingMessage[] PROGMEM = "Sending message error";
static const char ErrorSMTPQuit[] PROGMEM = "SMTP QUIT error";
static const char ErrorNotConnected[] PROGMEM = "Could not connect to mail server";
static const char ErrorConnection[] PROGMEM = "Connection Error";
static const char ErrorIdentification[] PROGMEM = "Identification error";
static const char ErrorAuthentification[] PROGMEM = "SMTP AUTH error";
static const char ErrorSMTPData[] PROGMEM = "SMTP DATA error";
static const char ErrorNotSent[] PROGMEM = "Sending message error";
static const char ErrorSMTPTimeout[] PROGMEM = "SMTP Response TIMEOUT!";

class AlertMe{
public:
AlertMe();
Expand Down