sendmail esp8266 mail attachments, array attachments

This commit is contained in:
gemu2015 2020-09-23 12:52:54 +02:00
parent 80d0730de9
commit ee7b64692b
3 changed files with 211 additions and 18 deletions

View File

@ -654,7 +654,7 @@ bool ESP32_MailClient::readMail(IMAPData &imapData)
{
_sdOk = sdTest();
if (_sdOk)
if (!SD.exists(imapData._savePath.c_str()))
if (!imapData.fsp->exists(imapData._savePath.c_str()))
createDirs(imapData._savePath);
}
else if (imapData._storageType == MailClientStorageType::SPIFFS)
@ -1788,6 +1788,7 @@ bool ESP32_MailClient::sendMail(SMTPData &smtpData)
else
{
/*
if (!_sdOk)
{
if (smtpData._storageType == MailClientStorageType::SD)
@ -1798,13 +1799,18 @@ bool ESP32_MailClient::sendMail(SMTPData &smtpData)
if (!_sdOk)
continue;
*/
bool file_existed = false;
/*
if (smtpData._storageType == MailClientStorageType::SD)
file_existed = SD.exists(smtpData._attach._filename[i].c_str());
else if (smtpData._storageType == MailClientStorageType::SPIFFS)
file_existed = SPIFFS.exists(smtpData._attach._filename[i].c_str());
else if (smtpData._storageType == MailClientStorageType::FFat)
file_existed = FFat.exists(smtpData._attach._filename[i].c_str());
*/
file_existed = smtpData.fsp->exists(smtpData._attach._filename[i].c_str());
if (file_existed)
{
smtpData._cbData._info = smtpData._attach._filename[i];
@ -1820,10 +1826,15 @@ bool ESP32_MailClient::sendMail(SMTPData &smtpData)
smtpData._net->getStreamPtr()->print(buf.c_str());
File file;
/*
if (smtpData._storageType == MailClientStorageType::SD)
file = SD.open(smtpData._attach._filename[i].c_str(), FILE_READ);
else if (smtpData._storageType == MailClientStorageType::SPIFFS)
file = SPIFFS.open(smtpData._attach._filename[i].c_str(), FILE_READ);
else if (smtpData._storageType == MailClientStorageType::FFat)
file = FFat.open(smtpData._attach._filename[i].c_str(), FILE_READ);
*/
file = smtpData.fsp->open(smtpData._attach._filename[i].c_str(), FILE_READ);
send_base64_encode_mime_file(smtpData._net->getStreamPtr(), file);
smtpData._net->getStreamPtr()->print(ESP32_MAIL_STR_34);
@ -2036,6 +2047,7 @@ bool ESP32_MailClient::sdBegin(uint8_t sck, uint8_t miso, uint8_t mosi, uint8_t
_mosi = mosi;
_ss = ss;
_sdConfigSet = true;
SPI.begin(_sck, _miso, _mosi, _ss);
return SD.begin(_ss, SPI);
}
@ -2331,7 +2343,7 @@ bool ESP32_MailClient::waitIMAPResponse(IMAPData &imapData, uint8_t imapCommandT
delete[] midx;
if (imapData._storageType == MailClientStorageType::SD)
if (!SD.exists(filepath.c_str()))
if (!imapData.fsp->exists(filepath.c_str()))
createDirs(filepath);
if (!imapData._headerSaved)
@ -2353,7 +2365,7 @@ bool ESP32_MailClient::waitIMAPResponse(IMAPData &imapData, uint8_t imapCommandT
}
if (imapData._storageType == MailClientStorageType::SD)
file = SD.open(filepath.c_str(), FILE_WRITE);
file = imapData.fsp->open(filepath.c_str(), FILE_WRITE);
else if (imapData._storageType == MailClientStorageType::SPIFFS)
file = SPIFFS.open(filepath.c_str(), FILE_WRITE);
}
@ -2922,7 +2934,7 @@ bool ESP32_MailClient::waitIMAPResponse(IMAPData &imapData, uint8_t imapCommandT
delete[] midx;
if (imapData._storageType == MailClientStorageType::SD)
if (!SD.exists(filepath.c_str()))
if (!imapData.fsp->exists(filepath.c_str()))
createDirs(filepath);
if (!imapData._headerSaved)
@ -2944,7 +2956,7 @@ bool ESP32_MailClient::waitIMAPResponse(IMAPData &imapData, uint8_t imapCommandT
}
if (imapData._storageType == MailClientStorageType::SD)
file = SD.open(filepath.c_str(), FILE_WRITE);
file = imapData.fsp->open(filepath.c_str(), FILE_WRITE);
else if (imapData._storageType == MailClientStorageType::SPIFFS)
file = SPIFFS.open(filepath.c_str(), FILE_WRITE);
}
@ -3003,7 +3015,7 @@ bool ESP32_MailClient::waitIMAPResponse(IMAPData &imapData, uint8_t imapCommandT
delete[] midx;
if (imapData._storageType == MailClientStorageType::SD)
if (!SD.exists(filepath.c_str()))
if (!imapData.fsp->exists(filepath.c_str()))
createDirs(filepath);
filepath += ESP32_MAIL_STR_202;
@ -3011,7 +3023,7 @@ bool ESP32_MailClient::waitIMAPResponse(IMAPData &imapData, uint8_t imapCommandT
filepath += imapData._messageDataInfo[mailIndex][messageDataIndex]._filename;
if (imapData._storageType == MailClientStorageType::SD)
file = SD.open(filepath.c_str(), FILE_WRITE);
file = imapData.fsp->open(filepath.c_str(), FILE_WRITE);
else if (imapData._storageType == MailClientStorageType::SPIFFS)
file = SPIFFS.open(filepath.c_str(), FILE_WRITE);
}
@ -3167,7 +3179,7 @@ bool ESP32_MailClient::waitIMAPResponse(IMAPData &imapData, uint8_t imapCommandT
{
if (imapData._storageType == MailClientStorageType::SD)
file = SD.open(hpath.c_str(), FILE_WRITE);
file = imapData.fsp->open(hpath.c_str(), FILE_WRITE);
else if (imapData._storageType == MailClientStorageType::SPIFFS)
file = SPIFFS.open(hpath.c_str(), FILE_WRITE);
@ -3714,6 +3726,17 @@ void IMAPData::setFetchUID(const String &fetchUID)
void IMAPData::setFileStorageType(uint8_t storageType)
{
_storageType = storageType;
switch (storageType) {
case MailClientStorageType::SPIFFS:
fsp = &SPIFFS;
break;
case MailClientStorageType::SD:
fsp = &SD;
break;
case MailClientStorageType::FFat:
fsp = &FFat;
break;
}
}
void IMAPData::setDownloadAttachment(bool download)
@ -4733,6 +4756,17 @@ void SMTPData::removeAttachFile(uint8_t index)
void SMTPData::setFileStorageType(uint8_t storageType)
{
_storageType = storageType;
switch (storageType) {
case MailClientStorageType::SPIFFS:
fsp = &SPIFFS;
break;
case MailClientStorageType::SD:
fsp = &SD;
break;
case MailClientStorageType::FFat:
fsp = &FFat;
break;
}
}
void SMTPData::clearAttachData()

View File

@ -48,6 +48,7 @@
#include "RFC2047.h"
#include "ESP32MailHTTPClient.h"
#include "ESP32TimeHelper.h"
#include <FFat.h>
#define FORMAT_SPIFFS_IF_FAILED true
@ -90,6 +91,7 @@ struct MailClientStorageType
{
static const uint8_t SPIFFS = 0;
static const uint8_t SD = 1;
static const uint8_t FFat = 2;
};
static const char ESP32_MAIL_STR_1[] PROGMEM = "Content-Type: multipart/mixed; boundary=\"";
@ -1302,6 +1304,7 @@ private:
std::string _host = "";
uint16_t _port = 993;
uint8_t _storageType = 1;
FS *fsp = &SD;
bool _unseen = false;
std::string _loginEmail = "";
std::string _loginPassword = "";
@ -1859,7 +1862,7 @@ protected:
string _host = "";
uint16_t _port = 0;
uint8_t _storageType = 1;
FS *fsp = &SD;
string _fromName = "";
string _senderEmail = "";
string _subject = "";

View File

@ -330,6 +330,12 @@ String buffer;
if (!buffer.startsWith(F("354"))) {
goto exit;
}
buffer = F("MIME-Version: 1.0\r\n");
client->print(buffer);
buffer = F("Content-Type: Multipart/mixed; boundary=frontier\r\n");
client->print(buffer);
buffer = F("From: ");
buffer += from;
client->println(buffer);
@ -350,6 +356,7 @@ String buffer;
AddLog_P2(LOG_LEVEL_INFO, PSTR("%s"),buffer.c_str());
#endif
#ifdef USE_SCRIPT
if (*msg=='*' && *(msg+1)==0) {
g_client=client;
@ -378,9 +385,98 @@ exit:
}
void xsend_message_txt(char *msg) {
g_client->println(msg);
}
#ifdef DEBUG_EMAIL_PORT
AddLog_P2(LOG_LEVEL_INFO, PSTR("%s"),msg);
#endif
#if defined(USE_SCRIPT_FATFS) && defined(USE_SCRIPT)
if (*msg=='@') {
msg++;
attach_File(msg);
} else if (*msg=='&') {
msg++;
attach_Array(msg);
} else {
g_client->print(F("--frontier\r\n"));
g_client->print(F("Content-Type: text/plain\r\n\r\n"));
g_client->println(msg);
g_client->print(F("\r\n--frontier\r\n"));
}
#else
g_client->print(F("--frontier\r\n"));
g_client->print(F("Content-Type: text/plain\r\n\r\n"));
g_client->println(msg);
g_client->print(F("\r\n--frontier\r\n"));
#endif
}
#if defined(USE_SCRIPT_FATFS) && defined(USE_SCRIPT)
#include <LittleFS.h>
extern FS *fsp;
void attach_File(char *path) {
g_client->print(F("--frontier\r\n"));
g_client->print(F("Content-Type: text/plain\r\n"));
char buff[64];
char *cp = path;
while (*cp=='/') cp++;
File file = fsp->open(path, "r");
if (file) {
sprintf_P(buff,PSTR("Content-Disposition: attachment; filename=\"%s\"\r\n\r\n"), cp);
g_client->write(buff);
uint16_t flen = file.size();
uint8_t fbuff[64];
uint16_t blen = sizeof(fbuff);
while (flen>0) {
file.read(fbuff, blen);
flen -= blen;
g_client->write(fbuff, blen);
if (flen<blen) blen = flen;
}
file.close();
} else {
g_client->print(F("\r\n\r\nfile not found!\r\n"));
}
g_client->print(F("\r\n--frontier\r\n"));
}
float *get_array_by_name(char *name, uint16_t *alen);
void flt2char(float num, char *nbuff);
void attach_Array(char *aname) {
float *array = 0;
uint16_t alen;
array = get_array_by_name(aname, &alen);
g_client->print(F("--frontier\r\n"));
g_client->print(F("Content-Type: text/plain\r\n"));
if (array && alen) {
#ifdef DEBUG_EMAIL_PORT
AddLog_P2(LOG_LEVEL_INFO, PSTR("array found %d"),alen);
#endif
char buff[64];
sprintf_P(buff,PSTR("Content-Disposition: attachment; filename=\"%s.txt\"\r\n\r\n"), aname);
g_client->write(buff);
float *fp=array;
for (uint32_t cnt = 0; cnt<alen; cnt++) {
// export array as tab gelimited text
char nbuff[16];
flt2char(*fp++, nbuff);
if (cnt < (alen - 1)) {
strcat(nbuff,"\t");
} else {
strcat(nbuff,"\n");
}
g_client->write(nbuff, strlen(nbuff));
}
} else {
g_client->print(F("\r\n\r\narray not found!\r\n"));
}
g_client->print(F("\r\n--frontier\r\n"));
}
#endif // defined(USE_SCRIPT_FATFS) && defined(USE_SCRIPT)
#else
/*
* Created by K. Suwatchai (Mobizt)
@ -413,6 +509,9 @@ void script_send_email_body(void(*func)(char *));
#define xPSTR(a) a
//The Email Sending data object contains config and data to send
SMTPData smtpData;
#define MAX_ATTCHMENTS 8
char *attachments[MAX_ATTCHMENTS];
uint8_t num_attachments;
//Callback function to get the Email sending status
//void sendCallback(SendStatus info);
@ -435,7 +534,7 @@ uint16_t SendMail(char *buffer) {
// return if not enough memory
uint32_t mem=ESP.getFreeHeap();
AddLog_P2(LOG_LEVEL_INFO, PSTR("heap: %d"),mem);
//AddLog_P2(LOG_LEVEL_INFO, PSTR("heap: %d"),mem);
if (mem<SEND_MAIL32_MINRAM) {
return 4;
}
@ -463,6 +562,13 @@ uint16_t SendMail(char *buffer) {
cmd=endcmd+1;
for (uint32_t cnt=0; cnt<MAX_ATTCHMENTS; cnt++) {
attachments[cnt]=0;
}
num_attachments=0;
#ifdef DEBUG_EMAIL_PORT
AddLog_P2(LOG_LEVEL_INFO, PSTR("mailsize: %d"),blen);
#endif
@ -540,9 +646,10 @@ uint16_t SendMail(char *buffer) {
}
#endif
#ifdef DEBUG_EMAIL_PORT
#ifdef DEBUG_EMAIL_PORT
AddLog_P2(LOG_LEVEL_INFO, PSTR("%s - %s - %s - %s"),from,to,subject,cmd);
#endif
#endif
smtpData.setDebug(true);
@ -598,9 +705,16 @@ uint16_t SendMail(char *buffer) {
//Set the storage types to read the attach files (SD is default)
//smtpData.setFileStorageType(MailClientStorageType::SPIFFS);
#if defined (USE_SCRIPT) && defined(USE_SCRIPT_FATFS)
#ifdef USE_SCRIPT_FATFS
#if USE_SCRIPT_FATFS<0
smtpData.setFileStorageType(MailClientStorageType::FFat);
#else
smtpData.setFileStorageType(MailClientStorageType::SD);
#endif
#endif
//smtpData.setSendCallback(sendCallback);
//Start sending Email, can be set callback function to track the status
@ -613,17 +727,59 @@ uint16_t SendMail(char *buffer) {
}
//Clear all data from Email object to free memory
smtpData.empty();
for (uint32_t cnt=0; cnt<MAX_ATTCHMENTS; cnt++) {
if (attachments[cnt]) {
free(attachments[cnt]);
attachments[cnt]=0;
}
}
exit:
if (oparams) free(oparams);
return status;
}
float *get_array_by_name(char *name, uint16_t *alen);
void flt2char(float num, char *nbuff);
void attach_Array(char *aname) {
float *array = 0;
uint16_t alen;
String ttstr = "";
array = get_array_by_name(aname, &alen);
if (array && alen) {
float *fp=array;
for (uint32_t cnt = 0; cnt<alen; cnt++) {
// export array as tab gelimited text
char nbuff[16];
flt2char(*fp++, nbuff);
if (cnt < (alen - 1)) {
strcat(nbuff,"\t");
} else {
strcat(nbuff,"\n");
}
ttstr += nbuff;
}
if (num_attachments<MAX_ATTCHMENTS) {
attachments[num_attachments] = (char*)malloc(ttstr.length()+1);
strcpy(attachments[num_attachments],ttstr.c_str());
char name[32];
sprintf(name,"%s.txt",aname);
smtpData.addAttachData(name, "text/plain",(uint8_t*)attachments[num_attachments],ttstr.length());
num_attachments++;
}
} else {
//g_client->print(F("\r\n\r\narray not found!\r\n"));
}
}
void send_message_txt(char *txt) {
if (*txt=='&') {
if (*txt=='@') {
txt++;
smtpData.addAttachFile(txt);
} else if (*txt=='&') {
txt++;
attach_Array(txt);
} else if (*txt=='$') {
txt++;
#if defined(ESP32) && defined(USE_WEBCAM)