初始化提交
This commit is contained in:
11
arduino-cli/libraries/EMailSender-Version-1.x/.project
Normal file
11
arduino-cli/libraries/EMailSender-Version-1.x/.project
Normal file
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>EMailSender</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
262
arduino-cli/libraries/EMailSender-Version-1.x/EMailSender.cpp
Normal file
262
arduino-cli/libraries/EMailSender-Version-1.x/EMailSender.cpp
Normal file
@@ -0,0 +1,262 @@
|
||||
#include "EMailSender.h"
|
||||
|
||||
// BASE64 -----------------------------------------------------------
|
||||
const char PROGMEM b64_alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
"abcdefghijklmnopqrstuvwxyz"
|
||||
"0123456789+/";
|
||||
|
||||
#define encode64(arr) encode64_f(arr,strlen(arr))
|
||||
|
||||
inline void a3_to_a4(unsigned char * a4, unsigned char * a3) {
|
||||
a4[0] = (a3[0] & 0xfc) >> 2;
|
||||
a4[1] = ((a3[0] & 0x03) << 4) + ((a3[1] & 0xf0) >> 4);
|
||||
a4[2] = ((a3[1] & 0x0f) << 2) + ((a3[2] & 0xc0) >> 6);
|
||||
a4[3] = (a3[2] & 0x3f);
|
||||
}
|
||||
|
||||
int base64_encode(char *output, char *input, int inputLen) {
|
||||
int i = 0, j = 0;
|
||||
int encLen = 0;
|
||||
unsigned char a3[3];
|
||||
unsigned char a4[4];
|
||||
|
||||
while (inputLen--) {
|
||||
a3[i++] = *(input++);
|
||||
if (i == 3) {
|
||||
a3_to_a4(a4, a3);
|
||||
|
||||
for (i = 0; i < 4; i++) {
|
||||
output[encLen++] = pgm_read_byte(&b64_alphabet[a4[i]]);
|
||||
}
|
||||
|
||||
i = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (i) {
|
||||
for (j = i; j < 3; j++) {
|
||||
a3[j] = '\0';
|
||||
}
|
||||
|
||||
a3_to_a4(a4, a3);
|
||||
|
||||
for (j = 0; j < i + 1; j++) {
|
||||
output[encLen++] = pgm_read_byte(&b64_alphabet[a4[j]]);
|
||||
}
|
||||
|
||||
while ((i++ < 3)) {
|
||||
output[encLen++] = '=';
|
||||
}
|
||||
}
|
||||
output[encLen] = '\0';
|
||||
return encLen;
|
||||
}
|
||||
|
||||
int base64_enc_length(int plainLen) {
|
||||
int n = plainLen;
|
||||
return (n + 2 - ((n + 2) % 3)) / 3 * 4;
|
||||
}
|
||||
|
||||
const char* encode64_f(char* input, uint8_t len) {
|
||||
// encoding
|
||||
|
||||
DEBUG_PRINTLN(F("Encoding"));
|
||||
|
||||
DEBUG_PRINTLN(input);
|
||||
DEBUG_PRINTLN(len);
|
||||
|
||||
//int encodedLen =
|
||||
base64_enc_length(len);
|
||||
static char encoded[256];
|
||||
// note input is consumed in this step: it will be empty afterwards
|
||||
base64_encode(encoded, input, len);
|
||||
return encoded;
|
||||
}
|
||||
|
||||
// END BASE64 ---------------------------------------------------------
|
||||
|
||||
EMailSender::EMailSender(const char* email_login, const char* email_password, const char* email_from, // @suppress("Class members should be properly initialized")
|
||||
const char* smtp_server, uint16_t smtp_port, bool isSecure) {
|
||||
this->setEMailLogin(email_login);
|
||||
this->setEMailFrom(email_from);
|
||||
this->setEMailPassword(email_password);
|
||||
this->setSMTPServer(smtp_server);
|
||||
this->setSMTPPort(smtp_port);
|
||||
|
||||
this->isSecure = isSecure;
|
||||
}
|
||||
|
||||
EMailSender::EMailSender(const char* email_login, const char* email_password, const char* email_from, bool isSecure) { // @suppress("Class members should be properly initialized")
|
||||
this->setEMailLogin(email_login);
|
||||
this->setEMailFrom(email_from);
|
||||
this->setEMailPassword(email_password);
|
||||
|
||||
this->isSecure = isSecure;
|
||||
}
|
||||
|
||||
EMailSender::EMailSender(const char* email_login, const char* email_password, bool isSecure){ // @suppress("Class members should be properly initialized")
|
||||
this->setEMailLogin(email_login);
|
||||
this->setEMailFrom(email_login);
|
||||
this->setEMailPassword(email_password);
|
||||
|
||||
this->isSecure = isSecure;
|
||||
}
|
||||
|
||||
void EMailSender::setSMTPPort(uint16_t smtp_port){
|
||||
this->smtp_port = smtp_port;
|
||||
};
|
||||
void EMailSender::setSMTPServer(const char* smtp_server){
|
||||
delete [] this->smtp_server;
|
||||
this->smtp_server = new char[strlen(smtp_server)+1];
|
||||
strcpy(this->smtp_server, smtp_server);
|
||||
};
|
||||
|
||||
void EMailSender::setEMailLogin(const char* email_login){
|
||||
delete [] this->email_login;
|
||||
this->email_login = new char[strlen(email_login)+1];
|
||||
strcpy(this->email_login, email_login);
|
||||
};
|
||||
void EMailSender::setEMailFrom(const char* email_from){
|
||||
delete [] this->email_from;
|
||||
this->email_from = new char[strlen(email_from)+1];
|
||||
strcpy(this->email_from, email_from);
|
||||
};
|
||||
void EMailSender::setEMailPassword(const char* email_password){
|
||||
delete [] this->email_password;
|
||||
this->email_password = new char[strlen(email_password)+1];
|
||||
strcpy(this->email_password, email_password);
|
||||
};
|
||||
|
||||
void EMailSender::setIsSecure(bool isSecure) {
|
||||
this->isSecure = isSecure;
|
||||
}
|
||||
|
||||
EMailSender::Response EMailSender::awaitSMTPResponse(WiFiClientSecure &client,
|
||||
const char* resp, const char* respDesc, uint16_t timeOut) {
|
||||
EMailSender::Response response;
|
||||
uint32_t ts = millis();
|
||||
while (!client.available()) {
|
||||
if (millis() > (ts + timeOut)) {
|
||||
response.code = F("1");
|
||||
response.desc = F("SMTP Response TIMEOUT!");
|
||||
response.status = false;
|
||||
return response;
|
||||
}
|
||||
}
|
||||
_serverResponce = client.readStringUntil('\n');
|
||||
|
||||
DEBUG_PRINTLN(_serverResponce);
|
||||
if (resp && _serverResponce.indexOf(resp) == -1){
|
||||
response.code = resp;
|
||||
response.desc = respDesc;
|
||||
response.status = false;
|
||||
return response;
|
||||
}
|
||||
|
||||
response.status = true;
|
||||
return response;
|
||||
}
|
||||
|
||||
EMailSender::Response EMailSender::send(const char* to, EMailMessage &email)
|
||||
{
|
||||
WiFiClientSecure client;
|
||||
|
||||
DEBUG_PRINT(F("Insecure client:"));
|
||||
DEBUG_PRINTLN(this->isSecure);
|
||||
|
||||
#ifndef ARDUINO_ESP8266_RELEASE_2_4_2
|
||||
if (this->isSecure == false){
|
||||
client.setInsecure();
|
||||
bool mfln = client.probeMaxFragmentLength(this->smtp_server, this->smtp_port, 512);
|
||||
|
||||
DEBUG_PRINT("MFLN supported: ");
|
||||
DEBUG_PRINTLN(mfln?"yes":"no");
|
||||
|
||||
if (mfln) {
|
||||
client.setBufferSizes(512, 512);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
EMailSender::Response response;
|
||||
|
||||
if(!client.connect(this->smtp_server, this->smtp_port)) {
|
||||
response.desc = F("Could not connect to mail server");
|
||||
response.code = F("2");
|
||||
response.status = false;
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
response = awaitSMTPResponse(client, "220", "Connection Error");
|
||||
if (!response.status) return response;
|
||||
|
||||
|
||||
|
||||
DEBUG_PRINTLN(F("HELO friend:"));
|
||||
client.println(F("HELO friend"));
|
||||
|
||||
response = awaitSMTPResponse(client, "250", "Identification error");
|
||||
if (!response.status) return response;
|
||||
|
||||
|
||||
DEBUG_PRINTLN(F("AUTH LOGIN:"));
|
||||
client.println(F("AUTH LOGIN"));
|
||||
awaitSMTPResponse(client);
|
||||
|
||||
DEBUG_PRINTLN(encode64(this->email_login));
|
||||
client.println(encode64(this->email_login));
|
||||
awaitSMTPResponse(client);
|
||||
|
||||
DEBUG_PRINTLN(encode64(this->email_password));
|
||||
client.println(encode64(this->email_password));
|
||||
|
||||
response = awaitSMTPResponse(client, "235", "SMTP AUTH error");
|
||||
if (!response.status) return response;
|
||||
|
||||
String mailFrom = "MAIL FROM: <" + String(this->email_from) + '>';
|
||||
DEBUG_PRINTLN(mailFrom);
|
||||
client.println(mailFrom);
|
||||
awaitSMTPResponse(client);
|
||||
|
||||
String rcpt = "RCPT TO: <" + String(to) + '>';
|
||||
|
||||
DEBUG_PRINTLN(rcpt);
|
||||
client.println(rcpt);
|
||||
awaitSMTPResponse(client);
|
||||
|
||||
DEBUG_PRINTLN(F("DATA:"));
|
||||
client.println(F("DATA"));
|
||||
|
||||
response = awaitSMTPResponse(client, "354", "SMTP DATA error");
|
||||
if (!response.status) return response;
|
||||
|
||||
client.println("From: <" + String(this->email_from) + '>');
|
||||
client.println("To: <" + String(to) + '>');
|
||||
|
||||
client.print(F("Subject: "));
|
||||
client.println(email.subject);
|
||||
|
||||
client.println(F("Mime-Version: 1.0"));
|
||||
client.println(F("Content-Type: text/html; charset=\"UTF-8\""));
|
||||
client.println(F("Content-Transfer-Encoding: 7bit"));
|
||||
client.println();
|
||||
String body = "<!DOCTYPE html><html lang=\"en\">" + String(email.message) + "</html>";
|
||||
client.println(body);
|
||||
client.println(".");
|
||||
|
||||
response = awaitSMTPResponse(client, "250", "Sending message error");
|
||||
if (!response.status) return response;
|
||||
|
||||
client.println(F("QUIT"));
|
||||
|
||||
response = awaitSMTPResponse(client, "221", "SMTP QUIT error");
|
||||
if (!response.status) return response;
|
||||
|
||||
response.status = true;
|
||||
response.code = F("0");
|
||||
response.desc = F("Message sent!");
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
76
arduino-cli/libraries/EMailSender-Version-1.x/EMailSender.h
Normal file
76
arduino-cli/libraries/EMailSender-Version-1.x/EMailSender.h
Normal file
@@ -0,0 +1,76 @@
|
||||
/** \mainpage EMailSender library
|
||||
*
|
||||
* MIT license
|
||||
* written by Renzo Mischianti
|
||||
*/
|
||||
|
||||
#ifndef EMailSender_h
|
||||
#define EMailSender_h
|
||||
|
||||
#include <WiFiClientSecure.h>
|
||||
|
||||
#if ARDUINO >= 100
|
||||
#include "Arduino.h"
|
||||
#else
|
||||
#include "WProgram.h"
|
||||
#endif
|
||||
|
||||
// Uncomment if you use esp8266 core <= 2.4.2
|
||||
// #define ARDUINO_ESP8266_RELEASE_2_4_2
|
||||
|
||||
// Uncomment to enable printing out nice debug messages.
|
||||
// #define EMAIL_SENDER_DEBUG
|
||||
|
||||
// Define where debug output will be printed.
|
||||
#define DEBUG_PRINTER Serial
|
||||
|
||||
// Setup debug printing macros.
|
||||
#ifdef EMAIL_SENDER_DEBUG
|
||||
#define DEBUG_PRINT(...) { DEBUG_PRINTER.print(__VA_ARGS__); }
|
||||
#define DEBUG_PRINTLN(...) { DEBUG_PRINTER.println(__VA_ARGS__); }
|
||||
#else
|
||||
#define DEBUG_PRINT(...) {}
|
||||
#define DEBUG_PRINTLN(...) {}
|
||||
#endif
|
||||
|
||||
class EMailSender {
|
||||
public:
|
||||
EMailSender(const char* email_login, const char* email_password, const char* email_from, const char* smtp_server, uint16_t smtp_port, bool isSecure = false);
|
||||
EMailSender(const char* email_login, const char* email_password, const char* email_from, bool isSecure = false);
|
||||
EMailSender(const char* email_login, const char* email_password, bool isSecure = false);
|
||||
|
||||
typedef struct {
|
||||
String subject;
|
||||
String message;
|
||||
} EMailMessage;
|
||||
|
||||
typedef struct {
|
||||
String code;
|
||||
String desc;
|
||||
bool status = false;
|
||||
} Response;
|
||||
|
||||
void setSMTPPort(uint16_t smtp_port);
|
||||
void setSMTPServer(const char* smtp_server);
|
||||
void setEMailLogin(const char* email_login);
|
||||
void setEMailFrom(const char* email_from);
|
||||
void setEMailPassword(const char* email_password);
|
||||
|
||||
EMailSender::Response send(const char* to, EMailMessage &email);
|
||||
|
||||
void setIsSecure(bool isSecure = false);
|
||||
|
||||
private:
|
||||
uint16_t smtp_port = 465;
|
||||
char* smtp_server = strdup("smtp.gmail.com");
|
||||
char* email_login = 0;
|
||||
char* email_from = 0;
|
||||
char* email_password = 0;
|
||||
|
||||
bool isSecure = false;
|
||||
|
||||
String _serverResponce;
|
||||
Response awaitSMTPResponse(WiFiClientSecure &client, const char* resp = "", const char* respDesc = "", uint16_t timeOut = 10000);
|
||||
};
|
||||
|
||||
#endif
|
||||
21
arduino-cli/libraries/EMailSender-Version-1.x/LICENSE
Normal file
21
arduino-cli/libraries/EMailSender-Version-1.x/LICENSE
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2017 Renzo Mischianti
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
66
arduino-cli/libraries/EMailSender-Version-1.x/README.md
Normal file
66
arduino-cli/libraries/EMailSender-Version-1.x/README.md
Normal file
@@ -0,0 +1,66 @@
|
||||
<div>
|
||||
<a href="https://www.mischianti.org/forums/forum/mischiantis-libraries/emailsender-send-email-with-attachments/"><img
|
||||
src="https://github.com/xreef/LoRa_E32_Series_Library/raw/master/resources/buttonSupportForumEnglish.png" alt="Support forum EMailSender English"
|
||||
align="right"></a>
|
||||
</div>
|
||||
<div>
|
||||
<a href="https://www.mischianti.org/it/forums/forum/le-librerie-di-mischianti/emailsender-invio-di-email-con-allegati/"><img
|
||||
src="https://github.com/xreef/LoRa_E32_Series_Library/raw/master/resources/buttonSupportForumItaliano.png" alt="Forum supporto EMailSender italiano"
|
||||
align="right"></a>
|
||||
</div>
|
||||
|
||||
# Library to send EMail via esp8266.
|
||||
|
||||
### [Updated tutorial on my site](https://www.mischianti.org/2019/09/10/send-email-with-esp8266-and-arduino/)
|
||||
|
||||
## Tutorial:
|
||||
|
||||
To download. click the DOWNLOADS button in the top right corner, rename the uncompressed folder EMailSender. Check that the EMailSender folder contains `EMailSender\\.cpp` and `EMailSender.h`. Place the DHT library folder your `<arduinosketchfolder>/libraries/` folder. You may need to create the libraries subfolder if its your first library. Restart the IDE.
|
||||
|
||||
# Reef complete EMailSender library to send EMail.
|
||||
I try to rationalize a famous library like Gsender.
|
||||
|
||||
Constructor:
|
||||
Default value is quite simple and use GMail as smtp server.
|
||||
```cpp
|
||||
EMailSender emailSend("smtp.account@gmail.com", "password");
|
||||
```
|
||||
|
||||
If you want use onother provider you can use more complex (but simple) contructor
|
||||
```cpp
|
||||
EMailSender(const char* email_login, const char* email_password, const char* email_from, const char* smtp_server, uint16_t smtp_port);
|
||||
|
||||
```
|
||||
|
||||
You must connect to WIFI :P.
|
||||
|
||||
Create a message with the structure EMailMessage
|
||||
```cpp
|
||||
EMailSender::EMailMessage message;
|
||||
message.subject = "Subject";
|
||||
message.message = "Hi, How are you<br>Fine.";
|
||||
```
|
||||
|
||||
Send message:
|
||||
```cpp
|
||||
EMailSender::Response resp = emailSend.send("account_to_send@gmail.com", message);
|
||||
```
|
||||
|
||||
Then check the response:
|
||||
```cpp
|
||||
Serial.println("Sending status: ");
|
||||
Serial.println(resp.code);
|
||||
Serial.println(resp.desc);
|
||||
Serial.println(resp.status);
|
||||
```
|
||||
|
||||
Example output:
|
||||
|
||||
```cpp
|
||||
Connection: ESTABLISHED
|
||||
Got IP address: 192.168.1.104
|
||||
Sending status:
|
||||
1
|
||||
0
|
||||
Message sent!
|
||||
```
|
||||
@@ -0,0 +1,82 @@
|
||||
#include "Arduino.h"
|
||||
#include <EMailSender.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
|
||||
uint8_t connection_state = 0;
|
||||
uint16_t reconnect_interval = 10000;
|
||||
|
||||
EMailSender emailSend("smtp.account@gmail.com", "password");
|
||||
|
||||
uint8_t WiFiConnect(const char* nSSID = nullptr, const char* nPassword = nullptr)
|
||||
{
|
||||
static uint16_t attempt = 0;
|
||||
Serial.print("Connecting to ");
|
||||
if(nSSID) {
|
||||
WiFi.begin(nSSID, nPassword);
|
||||
Serial.println(nSSID);
|
||||
}
|
||||
|
||||
uint8_t i = 0;
|
||||
while(WiFi.status()!= WL_CONNECTED && i++ < 50)
|
||||
{
|
||||
delay(200);
|
||||
Serial.print(".");
|
||||
}
|
||||
++attempt;
|
||||
Serial.println("");
|
||||
if(i == 51) {
|
||||
Serial.print("Connection: TIMEOUT on attempt: ");
|
||||
Serial.println(attempt);
|
||||
if(attempt % 2 == 0)
|
||||
Serial.println("Check if access point available or SSID and Password\r\n");
|
||||
return false;
|
||||
}
|
||||
Serial.println("Connection: ESTABLISHED");
|
||||
Serial.print("Got IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
return true;
|
||||
}
|
||||
|
||||
void Awaits()
|
||||
{
|
||||
uint32_t ts = millis();
|
||||
while(!connection_state)
|
||||
{
|
||||
delay(50);
|
||||
if(millis() > (ts + reconnect_interval) && !connection_state){
|
||||
connection_state = WiFiConnect();
|
||||
ts = millis();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//The setup function is called once at startup of the sketch
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
|
||||
const char* ssid = "ssid of your AP";
|
||||
const char* password = "password of your AP";
|
||||
|
||||
connection_state = WiFiConnect(ssid, password);
|
||||
if(!connection_state) // if not connected to WIFI
|
||||
Awaits(); // constantly trying to connect
|
||||
|
||||
EMailSender::EMailMessage message;
|
||||
message.subject = "Soggetto";
|
||||
message.message = "Ciao come stai<br>io bene.";
|
||||
|
||||
EMailSender::Response resp = emailSend.send("account_to_send@gmail.com", message);
|
||||
|
||||
Serial.println("Sending status: ");
|
||||
|
||||
Serial.println(resp.status);
|
||||
Serial.println(resp.code);
|
||||
Serial.println(resp.desc);
|
||||
}
|
||||
|
||||
// The loop function is called in an endless loop
|
||||
void loop()
|
||||
{
|
||||
//Add your repeated code here
|
||||
}
|
||||
21
arduino-cli/libraries/EMailSender-Version-1.x/keywords.txt
Normal file
21
arduino-cli/libraries/EMailSender-Version-1.x/keywords.txt
Normal file
@@ -0,0 +1,21 @@
|
||||
###########################################
|
||||
# Syntax Coloring Map For EMailSender
|
||||
###########################################
|
||||
|
||||
###########################################
|
||||
# Datatypes (KEYWORD1)
|
||||
###########################################
|
||||
|
||||
EMailSender KEYWORD1
|
||||
|
||||
###########################################
|
||||
# Methods and Functions (KEYWORD2)
|
||||
###########################################
|
||||
|
||||
setSMTPPort KEYWORD2
|
||||
setSMTPServer KEYWORD2
|
||||
setEMailLogin KEYWORD2
|
||||
setEMailFrom KEYWORD2
|
||||
setEMailPassword KEYWORD2
|
||||
|
||||
send KEYWORD2
|
||||
Reference in New Issue
Block a user