Here I'd like to explain the version 2 of my library EMailSender, a big evolution respect to version 1, with support for Arduino with w5100, w5200 and w5500 ethernet shield and enc28J60 clone devices, and support for esp32 and esp8266.
Now you can add attachments also, loaded from storage device like SD or SPIFFS. Here Arduino ethernet usage.
Supplies:- Arduino Mega
- enc28J60
- SD card
Arduino, normally, manage network with external device, the standard device like w5100 use Ethernet library the clones ENC28J60 have some libraries to select.
To select your device you must go on EMailSenderKey.h library file and set the correct one
#define DEFAULT_EMAIL_NETWORK_TYPE_ARDUINO NETWORK_ENC28J60 // Default
The library loaded to manage this type of device is UIPEthernet, you can find the library on library manager of the Arduino IDE
or you can change default network type
#define DEFAULT_EMAIL_NETWORK_TYPE_ARDUINO NETWORK_W5100
This is the standard implementation and use Ethernet library.
An important think to consider is that this Ethernet shield not support SSL or TLS, so you must find a provider SMTP that offer an SMTP connection without this type of encription.
I create a topic on forum where you can add the provider you use, that you can find mine also.
Send Simple EmailTo send an email with Arduino you must find a provider that workwithout SSL or TLS, For my solution I use with the SendGrid provider.
I think the usage is very very simple.
So you must set the provider
EMailSender emailSend("YOUR-SENDGRID-API-KEY", "YOUR-SENDGRID-PASSWD", "FROM-EMAIL", "smtp.sendgrid.net", 25);
Than you must create a message and send It
EMailSender::EMailMessage message;<br> message.subject = "Soggetto";
message.message = "Ciao come stai<br>io bene.<br>
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);
Connect an SD Cart to Manage AttachmentsThan to send attachments you must connect an SD card like in the schema, if you need more information about connection refert to this article "How to use SD card with esp8266, esp32 and Arduino".
Send Email With AttachmentsTo send email with attachments you must find a provider that support that functionality, my sendgrid provider not support that and GMX the provider that I used for the test no more support.
But if you find a new provider you can use this code to attach the files.
EMailSender::FileDescriptior fileDescriptor[1];<br> fileDescriptor[0].filename = F("test.txt");
fileDescriptor[0].url = F("/test.txt");
fileDescriptor[0].mime = MIME_TEXT_PLAIN;
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);
The ResultHere the email sended with an esp8266 and GMail provider (to use GMail you must enambe external program).
/*
* 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
*
*/
#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()
{
}
LibraryYou can find the library on GitHub https://github.com/xreef/EMailSender
And you must ask features or report bugs on Forum
Additional documentation here.
Comments
Please log in or sign up to comment.