feat: 全量同步 254 个常用的 Arduino 扩展库文件

This commit is contained in:
yczpf2019
2026-01-24 16:05:38 +08:00
parent c665ba662b
commit 397b9a23a3
6878 changed files with 2732224 additions and 1 deletions

View 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>

View File

@@ -0,0 +1,578 @@
#include "EMailSender.h"
//#include <SPIFFS.h>
//#include <LittleFS.h>
//#define SD SPIFFS
// 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) {
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) { // @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){ // @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(EMAIL_NETWORK_CLASS &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 +String(" (") + _serverResponce + String(")");
response.status = false;
return response;
}
response.status = true;
return response;
}
static const char cb64[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
void encodeblock(unsigned char in[3],unsigned char out[4],int len) {
out[0]=cb64[in[0]>>2]; out[1]=cb64[((in[0]&0x03)<<4)|((in[1]&0xF0)>>4)];
out[2]=(unsigned char) (len>1 ? cb64[((in[1]&0x0F)<<2)|((in[2]&0xC0)>>6)] : '=');
out[3]=(unsigned char) (len>2 ? cb64[in[2]&0x3F] : '=');
}
#if (defined(STORAGE_SPIFFS_ENABLED) && defined(FS_NO_GLOBALS))
void encode(fs::File *file, EMAIL_NETWORK_CLASS *client) {
unsigned char in[3],out[4];
int i,len,blocksout=0;
while (file->available()!=0) {
len=0;
for (i=0;i<3;i++){
in[i]=(unsigned char) file->read();
if (file->available()!=0) len++;
else in[i]=0;
}
if (len){
encodeblock(in,out,len);
// for(i=0;i<4;i++) client->write(out[i]);
client->write(out, 4);
blocksout++; }
if (blocksout>=19||file->available()==0){
if (blocksout) {
client->print("\r\n");
}
blocksout=0;
}
}
}
#endif
#if (defined(STORAGE_SD_ENABLED) || (defined(STORAGE_SPIFFS_ENABLED) && !defined(FS_NO_GLOBALS)))
void encode(File *file, EMAIL_NETWORK_CLASS *client) {
unsigned char in[3],out[4];
int i,len,blocksout=0;
while (file->available()!=0) {
len=0;
for (i=0;i<3;i++){
in[i]=(unsigned char) file->read();
if (file->available()!=0) len++;
else in[i]=0;
}
if (len){
encodeblock(in,out,len);
// for(i=0;i<4;i++) client->write(out[i]);
client->write(out, 4);
blocksout++; }
if (blocksout>=19||file->available()==0){
if (blocksout) {
client->print("\r\n");
}
blocksout=0;
}
}
}
#endif
EMailSender::Response EMailSender::send(const char* to, EMailMessage &email, Attachments attachments){
DEBUG_PRINT(F("ONLY ONE RECIPIENT"));
const char* arrEmail[] = {to};
return send(arrEmail, 1, email, attachments);
}
EMailSender::Response EMailSender::send(const char* to[], byte sizeOfTo, EMailMessage &email, Attachments attachments) {
return send(to, sizeOfTo, 0, email, attachments);
}
EMailSender::Response EMailSender::send(const char* to[], byte sizeOfTo, byte sizeOfCc, EMailMessage &email, Attachments attachments)
{
return send(to, sizeOfTo, sizeOfCc, 0, email, attachments);
}
EMailSender::Response EMailSender::send(const char* to[], byte sizeOfTo, byte sizeOfCc,byte sizeOfCCn, EMailMessage &email, Attachments attachments)
{
EMAIL_NETWORK_CLASS client;
// SSLClient client(base_client, TAs, (size_t)TAs_NUM, A5);
DEBUG_PRINT(F("Insecure client:"));
DEBUG_PRINTLN(this->isSecure);
#if (EMAIL_NETWORK_TYPE == NETWORK_ESP8266 || EMAIL_NETWORK_TYPE == NETWORK_ESP8266_242)
#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
#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;
String helo = "HELO "+String(publicIPDescriptor)+": ";
DEBUG_PRINTLN(helo);
client.println(helo);
response = awaitSMTPResponse(client, "250", "Identification error");
if (!response.status) return response;
if (useAuth){
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;
}
DEBUG_PRINT(F("MAIL FROM: <"));
DEBUG_PRINT(this->email_from);
DEBUG_PRINTLN(F(">"));
client.print(F("MAIL FROM: <"));
client.print(this->email_from);
client.println(F(">"));
awaitSMTPResponse(client);
// String rcpt = "RCPT TO: <" + String(to) + '>';
//
// DEBUG_PRINTLN(rcpt);
// client.println(rcpt);
int cont;
for (cont=0;cont<(sizeOfTo+sizeOfCc+sizeOfCCn);cont++){
DEBUG_PRINT(F("RCPT TO: <"));
DEBUG_PRINT(to[cont]);
DEBUG_PRINTLN(F(">"));
client.print(F("RCPT TO: <"));
client.print(to[cont]);
client.println(F(">"));
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.print(F("From: <"));
client.print(this->email_from);
client.println(F(">"));
// client.println("To: <" + String(to) + '>');
client.print(F("To: "));
for (cont=0;cont<sizeOfTo;cont++){
client.print(F("<"));
client.print(to[cont]);
client.print(">");
if (cont!=sizeOfTo-1){
client.print(",");
}
}
client.println();
if (sizeOfCc>0){
client.print(F("Cc: "));
for (;cont<sizeOfTo+sizeOfCc;cont++){
client.print(F("<"));
client.print(to[cont]);
client.print(">");
if (cont!=sizeOfCc-1){
client.print(",");
}
}
client.println();
}
if (sizeOfCCn>0){
client.print(F("CCn: "));
for (;cont<sizeOfTo+sizeOfCc+sizeOfCCn;cont++){
client.print(F("<"));
client.print(to[cont]);
client.print(">");
if (cont!=sizeOfCCn-1){
client.print(",");
}
}
client.println();
}
client.print(F("Subject: "));
client.println(email.subject);
// client.println(F("Mime-Version: 1.0"));
client.println(F("MIME-Version: 1.0"));
client.println(F("Content-Type: Multipart/mixed; boundary=frontier"));
client.println(F("--frontier"));
client.print(F("Content-Type: "));
client.print(email.mime);
client.println(F("; charset=\"UTF-8\""));
// client.println(F("Content-Type: text/html; charset=\"UTF-8\""));
client.println(F("Content-Transfer-Encoding: 7bit"));
client.println();
if (email.mime==F("text/html")){
// String body = "<!DOCTYPE html><html lang=\"en\">" + String(email.message) + "</html>";
client.print(F("<!DOCTYPE html><html lang=\"en\">"));
client.print(email.message);
client.println(F("</html>"));
// client.println(body);
}else{
client.println(email.message);
}
client.println();
#ifdef STORAGE_SPIFFS_ENABLED
bool spiffsActive = false;
#endif
#ifdef STORAGE_SD_ENABLED
bool sdActive = false;
#endif
#if defined(ENABLE_ATTACHMENTS) && (defined(STORAGE_SD_ENABLED) || defined(STORAGE_SPIFFS_ENABLED))
// if ((sizeof(attachs) / sizeof(attachs[0]))>0){
if (sizeof(attachments)>0 && attachments.number>0){
DEBUG_PRINT(F("Array: "));
// for (int i = 0; i<(sizeof(attachs) / sizeof(attachs[0])); i++){
for (int i = 0; i<attachments.number; i++){
uint8_t tBuf[64];
DEBUG_PRINTLN(attachments.fileDescriptor[i].filename);
client.println(F("--frontier"));
client.print(F("Content-Type: "));
client.print(attachments.fileDescriptor[i].mime);
client.println(F("; charset=\"UTF-8\""));
if (attachments.fileDescriptor[i].encode64){
client.println(F("Content-Transfer-Encoding: base64"));
}
client.print(F("Content-Disposition: attachment; filename="));
client.print(attachments.fileDescriptor[i].filename);
client.println("\n");
int clientCount = 0;
#ifdef STORAGE_SPIFFS_ENABLED
if (attachments.fileDescriptor[i].storageType==EMAIL_STORAGE_TYPE_SPIFFS){
#ifdef OPEN_CLOSE_SPIFFS
if(!SPIFFS.begin()){
EMailSender::Response response;
response.code = F("500");
response.desc = F("Error on startup SPIFFS filesystem!");
response.status = false;
return response;
}
spiffsActive = true;
#endif
fs::File myFile = SPIFFS.open(attachments.fileDescriptor[i].url, "r");
if(myFile) {
if (attachments.fileDescriptor[i].encode64){
encode(&myFile, &client);
}else{
while(myFile.available()) {
clientCount = myFile.read(tBuf,64);
client.write((byte*)tBuf,clientCount);
}
}
myFile.close();
client.println();
}
else {
EMailSender::Response response;
response.code = F("404");
response.desc = "Error opening attachments file "+attachments.fileDescriptor[i].url;
response.status = false;
return response;
}
}
#endif
#ifdef STORAGE_SD_ENABLED
if (attachments.fileDescriptor[i].storageType==EMAIL_STORAGE_TYPE_SD){
// File myFile = SD.open(attachments.fileDescriptor[i].url, "r");
// if(myFile) {
// while(myFile.available()) {
// clientCount = myFile.read(tBuf,64);
// client.write((byte*)tBuf,clientCount);
// }
// myFile.close();
// }
// else {
// EMailSender::Response response;
// response.code = "404";
// response.desc = "Error opening attachments file "+attachments.fileDescriptor[i].url;
// response.status = false;
// return response;
// }
#ifdef OPEN_CLOSE_SD
DEBUG_PRINTLN(F("SD Check"));
if(!SD.begin(4)){
response.code = F("500");
response.desc = F("Error on startup SD filesystem!");
response.status = false;
return response;
}
sdActive = true;
#endif
DEBUG_PRINTLN(F("Open file: "));
File myFile = SD.open(attachments.fileDescriptor[i].url.c_str());
if(myFile) {
myFile.seek(0);
DEBUG_PRINTLN(F("OK"));
if (attachments.fileDescriptor[i].encode64){
DEBUG_PRINTLN(F("BASE 64"));
encode(&myFile, &client);
}else{
DEBUG_PRINTLN(F("NORMAL"));
while(myFile.available()) {
clientCount = myFile.read(tBuf,64);
client.write((byte*)tBuf,clientCount);
}
}
myFile.close();
client.println();
}
else {
response.code = F("404");
response.desc = "Error opening attachments file "+attachments.fileDescriptor[i].url;
response.status = false;
return response;
}
}
#endif
}
client.println();
client.println(F("--frontier--"));
#ifdef STORAGE_SD_ENABLED
#ifdef OPEN_CLOSE_SD
if (sdActive){
DEBUG_PRINTLN(F("SD end"));
#ifndef ARDUINO_ESP8266_RELEASE_2_4_2
SD.end();
#endif
DEBUG_PRINTLN(F("SD end 2"));
}
#endif
#endif
#ifdef STORAGE_SPIFFS_ENABLED
#ifdef OPEN_CLOSE_SPIFFS
if (spiffsActive){
SPIFFS.end();
}
#endif
#endif
}
#endif
DEBUG_PRINTLN(F("Message end"));
client.println(F("."));
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;
}

View File

@@ -0,0 +1,299 @@
/*
* EMail Sender Arduino, esp8266 and esp32 library to send email
*
* AUTHOR: Renzo Mischianti
* VERSION: 2.0.0
*
* https://www.mischianti.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2017 Renzo Mischianti www.mischianti.org All right reserved.
*
* You may copy, alter and reuse this code in any way you like, but please leave
* reference to www.mischianti.org in your comments if you redistribute this code.
*
* 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.
*/
#ifndef EMailSender_h
#define EMailSender_h
#include "EMailSenderKey.h"
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#if(NETWORK_ESP8266_242 == DEFAULT_EMAIL_NETWORK_TYPE_ESP8266)
#define ARDUINO_ESP8266_RELEASE_2_4_2
#define DEFAULT_EMAIL_NETWORK_TYPE_ESP8266 NETWORK_ESP8266
#endif
//
//#if(NETWORK_ESP8266_SD == DEFAULT_EMAIL_NETWORK_TYPE_ESP8266)
// #define ESP8266_GT_2_4_2_SD_STORAGE_SELECTED
// #define DEFAULT_EMAIL_NETWORK_TYPE_ESP8266 NETWORK_ESP8266
//#endif
#if !defined(EMAIL_NETWORK_TYPE)
// select Network type based
#if defined(ESP8266) || defined(ESP31B)
//#define STORAGE_SPIFFS_ENABLED
// #if(NETWORK_ESP8266_SD == DEFAULT_EMAIL_NETWORK_TYPE_ESP8266)
// #define STORAGE_SD_ENABLED_ONLY
// #define EMAIL_NETWORK_TYPE NETWORK_ESP8266
// #elif(NETWORK_ESP8266_SPIFFS == DEFAULT_EMAIL_NETWORK_TYPE_ESP8266)
// #define STORAGE_SPIFFS_ENABLED_ONLY
// #define EMAIL_NETWORK_TYPE NETWORK_ESP8266
// #else
#define EMAIL_NETWORK_TYPE DEFAULT_EMAIL_NETWORK_TYPE_ESP8266
// #endif
#elif defined(ESP32)
#define EMAIL_NETWORK_TYPE DEFAULT_EMAIL_NETWORK_TYPE_ESP32
#else
#define EMAIL_NETWORK_TYPE DEFAULT_EMAIL_NETWORK_TYPE_ARDUINO
// #define STORAGE_SD_ENABLED
#endif
#endif
#if defined(ESP8266) || defined(ESP31B)
#ifndef STORAGE_SPIFFS_ENABLED_ONLY
#define STORAGE_SD_ENABLED
#endif
#ifndef STORAGE_SD_ENABLED_ONLY
#define STORAGE_SPIFFS_ENABLED
#endif
#elif defined(ESP32)
#ifndef STORAGE_SPIFFS_ENABLED_ONLY
#define STORAGE_SD_ENABLED
#endif
#ifndef STORAGE_SD_ENABLED_ONLY
#define STORAGE_SPIFFS_ENABLED
#endif
#else
#define STORAGE_SD_ENABLED
#endif
// Includes and defined based on Network Type
#if(EMAIL_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
// Note:
// No SSL/WSS support for client in Async mode
// TLS lib need a sync interface!
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#elif defined(ESP32)
#include <WiFi.h>
#include <WiFiClientSecure.h>
#define EMAIL_NETWORK_CLASS WiFiClient
#define EMAIL_NETWORK_SSL_CLASS WiFiClientSecure
#define EMAIL_NETWORK_SERVER_CLASS WiFiServer
#elif defined(ESP31B)
#include <ESP31BWiFi.h>
#else
#error "network type ESP8266 ASYNC only possible on the ESP mcu!"
#endif
//
//#include <ESPAsyncTCP.h>
//#include <ESPAsyncTCPbuffer.h>
//#define EMAIL_NETWORK_CLASS AsyncTCPbuffer
//#define EMAIL_NETWORK_SERVER_CLASS AsyncServer
#elif(EMAIL_NETWORK_TYPE == NETWORK_ESP8266 || EMAIL_NETWORK_TYPE == NETWORK_ESP8266_242)
#if !defined(ESP8266) && !defined(ESP31B)
#error "network type ESP8266 only possible on the ESP mcu!"
#endif
#ifdef ESP8266
#include <ESP8266WiFi.h>
#else
#include <ESP31BWiFi.h>
#endif
#define EMAIL_NETWORK_CLASS WiFiClient
#define EMAIL_NETWORK_SSL_CLASS WiFiClientSecure
#define EMAIL_NETWORK_SERVER_CLASS WiFiServer
#elif(EMAIL_NETWORK_TYPE == NETWORK_W5100)
#ifdef STM32_DEVICE
#define EMAIL_NETWORK_CLASS TCPClient
#define EMAIL_NETWORK_SERVER_CLASS TCPServer
#else
#include <Ethernet.h>
#include <SPI.h>
#define EMAIL_NETWORK_CLASS EthernetClient
#define EMAIL_NETWORK_SERVER_CLASS EthernetServer
#endif
#elif(EMAIL_NETWORK_TYPE == NETWORK_ENC28J60)
#include <UIPEthernet.h>
#define EMAIL_NETWORK_CLASS UIPClient
#define EMAIL_NETWORK_SERVER_CLASS UIPServer
//#include <UIPEthernet.h>
//UIPClient base_client;
//SSLClient client(base_client, TAs, (size_t)TAs_NUM, A5);
//
//#define EMAIL_NETWORK_CLASS SSLClient
//#define EMAIL_NETWORK_SERVER_CLASS UIPServer
#elif(EMAIL_NETWORK_TYPE == NETWORK_ESP32)
#include <WiFi.h>
#include <WiFiClientSecure.h>
#define EMAIL_NETWORK_CLASS WiFiClient
#define EMAIL_NETWORK_SSL_CLASS WiFiClientSecure
#define EMAIL_NETWORK_SERVER_CLASS WiFiServer
#elif(EMAIL_NETWORK_TYPE == NETWORK_ESP32_ETH)
#include <ETH.h>
#define EMAIL_NETWORK_CLASS WiFiClient
#define EMAIL_NETWORK_SERVER_CLASS WiFiServer
#else
#error "no network type selected!"
#endif
#ifdef ENABLE_ATTACHMENTS
#ifdef STORAGE_SPIFFS_ENABLED
#if defined(ESP32)
// #define FS_NO_GLOBALS
#include <SPIFFS.h>
#else
#ifdef ARDUINO_ESP8266_RELEASE_2_4_2
#define FS_NO_GLOBALS
#endif
#include "FS.h"
#endif
#endif
#ifdef STORAGE_SD_ENABLED
#include <SPI.h>
#include <SD.h>
#endif
#endif
#ifdef EMAIL_NETWORK_SSL_CLASS
#define EMAIL_NETWORK_CLASS EMAIL_NETWORK_SSL_CLASS
#endif
#define OPEN_CLOSE_SPIFFS
#define OPEN_CLOSE_SD
// 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);
EMailSender(const char* email_login, const char* email_password, const char* email_from);
EMailSender(const char* email_login, const char* email_password);
enum StorageType {
EMAIL_STORAGE_TYPE_SPIFFS,
EMAIL_STORAGE_TYPE_SD
};
#define MIME_TEXT_HTML F("text/html")
#define MIME_TEXT_PLAIN F("text/plain")
#define MIME_IMAGE_JPG F("image/jpg")
#define MIME_IMAGE_PNG F("image/png")
typedef struct {
String mime = "text/html";
String subject;
String message;
} EMailMessage;
typedef struct {
StorageType storageType = EMAIL_STORAGE_TYPE_SD;
String mime;
bool encode64 = false;
String filename;
String url;
} FileDescriptior;
typedef struct {
byte number;
FileDescriptior *fileDescriptor;
} Attachments;
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, Attachments att = {0, 0});
EMailSender::Response send(const char* to[], byte sizeOfTo, EMailMessage &email, Attachments att = {0, 0});
EMailSender::Response send(const char* to[], byte sizeOfTo, byte sizeOfCc, EMailMessage &email, Attachments att = {0, 0});
EMailSender::Response send(const char* to[], byte sizeOfTo, byte sizeOfCc, byte sizeOfCCn, EMailMessage &email, Attachments att = {0, 0});
void setIsSecure(bool isSecure = false);
void setUseAuth(bool useAuth = true) {
this->useAuth = useAuth;
}
void setPublicIpDescriptor(const char *publicIpDescriptor = "mischianti") {
publicIPDescriptor = publicIpDescriptor;
}
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;
const char* publicIPDescriptor = "mischianti";
bool isSecure = false;
bool useAuth = true;
String _serverResponce;
Response awaitSMTPResponse(EMAIL_NETWORK_CLASS &client, const char* resp = "", const char* respDesc = "", uint16_t timeOut = 10000);
};
#endif

View File

@@ -0,0 +1,33 @@
#ifndef EMailSenderKey_h
#define EMailSenderKey_h
// Uncomment if you use esp8266 core <= 2.4.2
//#define ARDUINO_ESP8266_RELEASE_2_4_2
#define ENABLE_ATTACHMENTS
// Uncomment to enable printing out nice debug messages.
//#define EMAIL_SENDER_DEBUG
// Define where debug output will be printed.
#define DEBUG_PRINTER Serial
#define NETWORK_ESP8266_ASYNC (0)
#define NETWORK_ESP8266 (1)
#define NETWORK_ESP8266_242 (6)
#define NETWORK_W5100 (2)
#define NETWORK_ENC28J60 (3)
#define NETWORK_ESP32 (4)
#define NETWORK_ESP32_ETH (5)
#ifndef DEFAULT_EMAIL_NETWORK_TYPE_ESP8266
#define DEFAULT_EMAIL_NETWORK_TYPE_ESP8266 NETWORK_ESP8266
#endif
#ifndef DEFAULT_EMAIL_NETWORK_TYPE_ESP32
#define DEFAULT_EMAIL_NETWORK_TYPE_ESP32 NETWORK_ESP32
#endif
#ifndef DEFAULT_EMAIL_NETWORK_TYPE_ARDUINO
#define DEFAULT_EMAIL_NETWORK_TYPE_ARDUINO NETWORK_ENC28J60
#endif
#endif

View 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.

View File

@@ -0,0 +1,119 @@
<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 with attachments
### Arduino (support W5100 like must be set, and ENC28J60 via UIPEthernet), esp8266 (SPIFFS and SD) (core <=2.4.2 must be set) and esp32 (SPIFFS and SD).
### Complete english tutorial
#### [Send email with attachments (EMailSender v2.x library): Arduino Ethernet](https://www.mischianti.org/category/my-libraries/emailsender-send-email-with-attachments/)
#### [Send email with attachments (EMailSender v2.x library): esp32 and esp8266](https://www.mischianti.org/category/my-libraries/emailsender-send-email-with-attachments/)
### Tutorial completo in italiano
### [Inviare email con allegati (libreria v2.x): Arduino Ethernet](https://www.mischianti.org/it/category/le-mie-librerie/emailsender-inviare-email-con-allegati/)
### [Inviare email con allegati (libreria v2.x): esp32 e esp8266](https://www.mischianti.org/it/category/le-mie-librerie/emailsender-inviare-email-con-allegati/)
## Installation 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 EMailSender library folder your `<arduinosketchfolder>/libraries/` folder.
You may need to create the libraries subfolder if its your first library.
Restart the IDE.
# EMailSender library to send EMail.
With this library you can send email with attach:
Arduino
Network supported
- w5100 like shield with Ethernet library
- enc28J60 with UIPLibrary
Storage support
- SD
esp8266
you must pay attention, older core from 2.4.2 must be activated
Storage supported
- SD
- SPIFFS
esp32
Storage supported
- SD
- SPIFFS
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.";
```
Create array of attachments
```cpp
// Two file
EMailSender::FileDescriptior fileDescriptor[2];
fileDescriptor[1].filename = F("test.txt");
fileDescriptor[1].url = F("/test.txt");
fileDescriptor[1].storageType = EMailSender::EMAIL_STORAGE_TYPE_SD;
fileDescriptor[0].filename = F("logo.jpg");
fileDescriptor[0].url = F("/logo.jpg");
fileDescriptor[0].mime = "image/jpg";
fileDescriptor[0].encode64 = true;
fileDescriptor[0].storageType = EMailSender::EMAIL_STORAGE_TYPE_SD;
EMailSender::Attachments attachs = {2, fileDescriptor};
```
Send message:
```cpp
EMailSender::Response resp = emailSend.send("account_to_send@gmail.com", message, attachs);
```
Then check the response:
```cpp
Serial.println("Sending status: ");
Serial.println(resp.code);
Serial.println(resp.desc);
Serial.println(resp.status);
```
From version 2.1.1 new features distribution list to send CC and CCn email.
Example output:
```cpp
Connection: ESTABLISHED
Got IP address: 192.168.1.104
Sending status:
1
0
Message sent!
```

View File

@@ -0,0 +1,161 @@
/*
* EMailSender library for Arduino, esp8266 and esp32
* Arduino Mega and UIPEthernet send example with attach
* this example is not tested for all, I can't find a provider
* that manage attach without SSL and TLS
*
* Pay attention you must set in the library
* #define DEFAULT_EMAIL_NETWORK_TYPE_ARDUINO NETWORK_ENC28J60
* for UIPEthernet
*
* #define DEFAULT_EMAIL_NETWORK_TYPE_ARDUINO NETWORK_W5100
* for standard Ethernet
*
* The base64 encoding of the image is slow, so be patient
*
*
* https://www.mischianti.org/category/my-libraries/emailsender-send-email-with-attachments/
*
*/
#include "Arduino.h"
#include <SPI.h>
#include <UIPEthernet.h>
#include <SD.h>
#include <EMailSender.h>
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// 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 emailSend("<LOGIN>", "<PASSWD>", "<EMAIL-FROM>", "<SMTP-SERVER>", "<SMTP-SERVER-PORT>");
void printDirectory(File dir, int numTabs);
//The setup function is called once at startup of the sketch
void setup()
{
Serial.begin(115200);
delay(2000);
Serial.println("Starting!");
Serial.print("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
File root = SD.open("/");
printDirectory(root, 0);
Serial.println("done!");
// File myFile = SD.open("/TEST.TXT", "r");
// if(myFile) {
// myFile.seek(0);
// DEBUG_PRINTLN(F("OK"));
// myFile.close();
// }
// else {
// DEBUG_PRINTLN(F("KO"));
// }
//
//
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
while(1);
}
Serial.print("IP address ");
Serial.println(Ethernet.localIP());
EMailSender::EMailMessage message;
message.subject = "Soggetto";
message.message = "Ciao come stai<br>io bene.<br>www.mischianti.org";
message.mime = MIME_TEXT_PLAIN;
// Two file
// EMailSender::FileDescriptior fileDescriptor[2];
// fileDescriptor[1].filename = F("test.txt");
// fileDescriptor[1].url = F("/test.txt");
// fileDescriptor[1].storageType = EMailSender::EMAIL_STORAGE_TYPE_SD;
//
// fileDescriptor[0].filename = F("logo.jpg");
// fileDescriptor[0].url = F("/logo.jpg");
// fileDescriptor[0].mime = "image/jpg";
// fileDescriptor[0].encode64 = true;
// fileDescriptor[0].storageType = EMailSender::EMAIL_STORAGE_TYPE_SD;
//
// EMailSender::Attachments attachs = {2, fileDescriptor};
// One file
EMailSender::FileDescriptior fileDescriptor[1];
fileDescriptor[0].filename = F("test.txt");
fileDescriptor[0].url = F("/test.txt");
fileDescriptor[0].mime = MIME_TEXT_PLAIN;
fileDescriptor[0].storageType = EMailSender::EMAIL_STORAGE_TYPE_SD;
// Pay attention base64 encoding is quite slow
// EMailSender::FileDescriptior fileDescriptor[2];
// fileDescriptor[0].filename = F("logo.jpg");
// fileDescriptor[0].url = F("/logo.jpg");
// fileDescriptor[0].mime = "image/jpg";
// fileDescriptor[0].encode64 = false;
// fileDescriptor[0].storageType = EMailSender::EMAIL_STORAGE_TYPE_SD;
EMailSender::Attachments attachs = {1, fileDescriptor};
EMailSender::Response resp = emailSend.send("email_to_receive@gmail.com", message, attachs);
Serial.println("Sending status: ");
Serial.println(resp.status);
Serial.println(resp.code);
Serial.println(resp.desc);
File root2 = SD.open("/");
printDirectory(root2, 0);
Serial.println("done!");
SD.end();
}
void loop()
{
}
void printDirectory(File dir, int numTabs) {
while (true) {
File entry = dir.openNextFile();
if (! entry) {
// no more files
break;
}
for (uint8_t i = 0; i < numTabs; i++) {
Serial.print('\t');
}
Serial.print(entry.name());
if (entry.isDirectory()) {
Serial.println("/");
printDirectory(entry, numTabs + 1);
} else {
// files have sizes, directories do not
Serial.print("\t\t");
Serial.println(entry.size(), DEC);
}
entry.close();
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

View File

@@ -0,0 +1,2 @@
Prova prova prova
www.mischianti.org

View File

@@ -0,0 +1,64 @@
/*
* EMailSender library for Arduino, esp8266 and esp32
* Arduino Mega and UIPEthernet send example with Sendgrid provider
*
* Pay attention you must set in the library
* #define DEFAULT_EMAIL_NETWORK_TYPE_ARDUINO NETWORK_ENC28J60
* for UIPEthernet
*
* #define DEFAULT_EMAIL_NETWORK_TYPE_ARDUINO NETWORK_W5100
* for standard Ethernet
*
*
* https://www.mischianti.org/category/my-libraries/emailsender-send-email-with-attachments/
*
*/
#include "Arduino.h"
#include <SPI.h>
#include <UIPEthernet.h>
#include <EMailSender.h>
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
EMailSender emailSend("<YOUR-SENDGRID-API-KEY>", "<YOUR-SENDGRID-PASSWD>", "<FROM-EMAIL>", "smtp.sendgrid.net", 25);
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(115200);
// while (!Serial) {}
delay(2000);
Serial.println("Starting!");
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
while(1);
}
Serial.print("IP address ");
Serial.println(Ethernet.localIP());
EMailSender::EMailMessage message;
message.subject = "Soggetto";
message.message = "Ciao come stai<br>io bene.<br>www.mischianti.org";
EMailSender::Response resp = emailSend.send("email_to_receive@gmail.com", message);
Serial.println("Sending status: ");
Serial.println(resp.status);
Serial.println(resp.code);
Serial.println(resp.desc);
}
void loop()
{
}

View File

@@ -0,0 +1,98 @@
/*
* EMailSender library for Arduino, esp8266 and esp32
* Simple esp32 Gmail send to a distribution list example
*
* https://www.mischianti.org/category/my-libraries/emailsender-send-email-with-attachments/
*
*/
#include "Arduino.h"
#include <EMailSender.h>
#include <WiFi.h>
uint8_t connection_state = 0;
uint16_t reconnect_interval = 10000;
EMailSender emailSend("<YOUR-SMTP>", "<YOUR-SMTP-PASSWD>");
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();
}
}
}
void setup()
{
Serial.begin(115200);
const char* ssid = "<YOUR-SSID>";
const char* password = "<YOUR-PASSWD>";
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.<br>www.mischianti.org";
// Send to 3 different email
const char* arrayOfEmail[] = {"<FIRST>@gmail.com", "<SECOND>@yahoo.com", "<THIRD>@hotmail.com"};
EMailSender::Response resp = emailSend.send(arrayOfEmail, 3, message);
// // Send to 3 different email, 2 in C and 1 in CC
// const char* arrayOfEmail[] = {"<FIRST>@gmail.com", "<SECOND>@yahoo.com", "<THIRD>@hotmail.com"};
// EMailSender::Response resp = emailSend.send(arrayOfEmail, 2, 1, message);
//
// // Send to 3 different email first to C second to CC and third to CCn
// const char* arrayOfEmail[] = {"<FIRST>@gmail.com", "<SECOND>@yahoo.com", "<THIRD>@hotmail.com"};
// EMailSender::Response resp = emailSend.send(arrayOfEmail, 3, message);
Serial.println("Sending status: ");
Serial.println(resp.status);
Serial.println(resp.code);
Serial.println(resp.desc);
}
void loop()
{
}

View File

@@ -0,0 +1,120 @@
/*
* EMailSender library for Arduino, esp8266 and esp32
* esp32 Gmail send example with 2 attach loaded in SPIFFS
*
* The base64 encoding of the image is slow, so be patient
*
* https://www.mischianti.org/category/my-libraries/emailsender-send-email-with-attachments/
*
*/
#include "Arduino.h"
#include <EMailSender.h>
#include <WiFi.h>
#include <SPIFFS.h>
const char* ssid = "<YOUR-SSID>";
const char* password = "<YOUR-PASSWD>";
uint8_t connection_state = 0;
uint16_t reconnect_interval = 10000;
EMailSender emailSend("account_gmail@gmail.com", "<YOUR-GMAIL-PASSWD>");
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();
}
}
}
void setup()
{
Serial.begin(115200);
if(!SPIFFS.begin()){
Serial.println("An Error has occurred while mounting SPIFFS");
return;
}
Serial.println("ReadDir");
File dir = SPIFFS.open("/");
File file = dir.openNextFile();
while (file) {
Serial.print(file.name());
Serial.println(file.size());
file = dir.openNextFile();
}
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.<br>www.mischianti.org";
EMailSender::FileDescriptior fileDescriptor[2];
fileDescriptor[1].filename = F("test.txt");
fileDescriptor[1].url = F("/test.txt");
fileDescriptor[1].storageType = EMailSender::EMAIL_STORAGE_TYPE_SPIFFS;
fileDescriptor[0].filename = F("logo.jpg");
fileDescriptor[0].url = F("/logo.jpg");
fileDescriptor[0].mime = "image/jpg";
fileDescriptor[0].encode64 = true;
fileDescriptor[0].storageType = EMailSender::EMAIL_STORAGE_TYPE_SPIFFS;
EMailSender::Attachments attachs = {2, fileDescriptor};
EMailSender::Response resp = emailSend.send("email_to_receive@gmail.com", message, attachs);
Serial.println("Sending status: ");
Serial.println(resp.status);
Serial.println(resp.code);
Serial.println(resp.desc);
}
void loop()
{
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

View File

@@ -0,0 +1,2 @@
Prova prova prova
www.mischianti.org

View File

@@ -0,0 +1,88 @@
/*
* EMailSender library for Arduino, esp8266 and esp32
* Simple esp32 Gmail send example
*
* https://www.mischianti.org/category/my-libraries/emailsender-send-email-with-attachments/
*
*/
#include "Arduino.h"
#include <EMailSender.h>
#include <WiFi.h>
const char* ssid = "<YOUR-SSID>";
const char* password = "<YOUR-PASSWD>";
uint8_t connection_state = 0;
uint16_t reconnect_interval = 10000;
EMailSender emailSend("account_login@gmail.com", "<YOUR-GMAIL-PASSWD>");
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();
}
}
}
void setup()
{
Serial.begin(115200);
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.<br>www.mischianti.org";
EMailSender::Response resp = emailSend.send("receive_email@gmail.com", message);
Serial.println("Sending status: ");
Serial.println(resp.status);
Serial.println(resp.code);
Serial.println(resp.desc);
}
void loop()
{
}

View File

@@ -0,0 +1,117 @@
/*
* EMailSender library for Arduino, esp8266 and esp32
* esp8266 Gmail send example with 2 attach loaded in SPIFFS
*
* The base64 encoding of the image is slow, so be patient
*
* https://www.mischianti.org/category/my-libraries/emailsender-send-email-with-attachments/
*
*/
#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();
}
}
}
void setup()
{
Serial.begin(115200);
const char* ssid = "<YOUR_SSID>";
const char* password = "<YOUR_PASSWD>";
if(!SPIFFS.begin()){
Serial.println("An Error has occurred while mounting SPIFFS");
return;
}
Serial.println("ReadDir");
Dir dir = SPIFFS.openDir("/");
while (dir.next()) {
Serial.print(dir.fileName());
if(dir.fileSize()) {
File f = dir.openFile("r");
Serial.println(f.size());
}
}
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.<br>www.mischianti.org";
EMailSender::FileDescriptior fileDescriptor[2];
fileDescriptor[1].filename = F("test.txt");
fileDescriptor[1].url = F("/test.txt");
fileDescriptor[1].storageType = EMailSender::EMAIL_STORAGE_TYPE_SPIFFS;
fileDescriptor[0].filename = F("logo.jpg");
fileDescriptor[0].url = F("/logo.jpg");
fileDescriptor[0].mime = "image/jpg";
fileDescriptor[0].encode64 = true;
fileDescriptor[0].storageType = EMailSender::EMAIL_STORAGE_TYPE_SPIFFS;
EMailSender::Attachments attachs = {2, fileDescriptor};
EMailSender::Response resp = emailSend.send("<receipe@gmail.com>", message, attachs);
Serial.println("Sending status: ");
Serial.println(resp.status);
Serial.println(resp.code);
Serial.println(resp.desc);
}
void loop()
{
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

View File

@@ -0,0 +1,2 @@
Prova prova prova
www.mischianti.org

View File

@@ -0,0 +1,88 @@
/*
* EMailSender library for Arduino, esp8266 and esp32
* Simple esp8266 Gmail send example
*
* https://www.mischianti.org/category/my-libraries/emailsender-send-email-with-attachments/
*
*/
#include "Arduino.h"
#include <EMailSender.h>
#include <ESP8266WiFi.h>
const char* ssid = "<YOUR-SSID>";
const char* password = "<YOUR-PASSWD>";
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();
}
}
}
void setup()
{
Serial.begin(115200);
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.<br>www.mischianti.org";
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);
}
void loop()
{
}

View File

@@ -0,0 +1,24 @@
###########################################
# Syntax Coloring Map For EMailSender
###########################################
###########################################
# Datatypes (KEYWORD1)
###########################################
EMailSender KEYWORD1
###########################################
# Methods and Functions (KEYWORD2)
###########################################
setSMTPPort KEYWORD2
setSMTPServer KEYWORD2
setEMailLogin KEYWORD2
setEMailFrom KEYWORD2
setEMailPassword KEYWORD2
setIsSecure KEYWORD2
setUseAuth KEYWORD2
setPublicIpDescriptor KEYWORD2
send KEYWORD2

View File

@@ -0,0 +1,11 @@
name=EMailSender
version=2.1.1
author=Renzo Mischianti <renzo.mischianti@gmail.com>
maintainer=Renzo Mischianti <renzo.mischianti@gmail.com>
sentence=EMail Arduino, esp8266 and esp32 Library.
paragraph=Library to send EMail with attachments via Arduino (support W5100 like, and ENC28J60 via UIPEthernet), esp8266 (SPIFFS and SD) (core <=2.4.2 must be set and >2.4.2) and esp32 (SPIFFS and SD).
category=Communication
url=https://www.mischianti.org/category/my-libraries/emailsender-send-email-with-attachments/
repository=https://github.com/xreef/EMailSender.git
architectures=*
includes=EMailSender.h