I finally upgrade this library with support for Arduino (Ethernet, UIPEthernet and SD as storage), esp32 (SD and SPIFFS) and esp8266 (SD and SPIFFS), with the possibility of attaching files.
Refer to this article for the version 2.x of the library “Send email with attachments (v2.x library)“.
A simple method to notify a problem is to use email, It’s very usefully and simple to share, so I created a library to do It. I get some code from GSender a library that It’s quite simple, but not so complete and configurable.
This library use the SMTP:
The Simple Mail Transfer Protocol (SMTP) is a communication protocol for electronic mail transmission. As an Internet standard, SMTP was first defined in 1982 by RFC 821, and updated in 2008 by RFC 5321 to Extended SMTP additions, which is the protocol variety in widespread use today. Mail servers and other message transfer agents use SMTP to send and receive mail messages. Proprietary systems such as Microsoft Exchange and IBM Notes and webmail systems such as Outlook.com, Gmail and Yahoo! Mail may use non-standard protocols internally, but all use SMTP when sending to or receiving email from outside their own systems. SMTP servers commonly use the Transmission Control Protocol on port number 25. (cit WiKi)
For the examples I use a gmail dedicated account, I create a new one account because you must reduce security to use It with an external program.
Backward compatibility
From esp8266 core 2.4.2 and next version upper and equal than 2.5.2 something are changed and to support old versione 2.4.2 I add a define to allow you to work and that core.
So if you have a esp8266 core 2.4.2 or less you must decomment this line
// Uncomment if you use esp8266 core <= 2.4.2
#define ARDUINO_ESP8266_RELEASE_2_4_2
Library
You can find my library here.
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 /libraries/ folder.
You may need to create the libraries subfolder if its your first library.
Restart the IDE.
Usage
Constructor: Default value is quite simple and use GMail as smtp server.
EMailSender emailSend("smtp.account@gmail.com", "password");
If you want use onother provider you can use more complex (but simple) contructor
EMailSender(const char* email_login, const char* email_password, 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, const char* email_from, const char* smtp_server, uint16_t smtp_port, bool isSecure = false);
email_login
: login to account smtpemail_password
: password account smtpemail_from
: email of sendersmtp_server
: server SMTPsmtp_port
: SMTP portisSecure = false
: if false BearSSL allows for ‘insecure’ TLS connections
You must connect to WIFI :P.
Create a message with the structure EMailMessage
EMailSender::EMailMessage message;
message.subject = "Subject";
message.message = "Hi, How are you<br>Fine.";
Send message:
EMailSender::Response resp = emailSend.send("account_to_send@gmail.com", message);
Then check the response:
Serial.println("Sending status: ");
Serial.println(resp.code);
Serial.println(resp.desc);
Serial.println(resp.status);
Example output:
Connection: ESTABLISHED
Got IP address: 192.168.1.104
Sending status:
1
0
Message sent!
GMail enable
Allow less secure apps to access your Gmail account
Google may block sign-in attempts from some apps or devices that do not use modern security standards. Since these apps and devices are easier to break into, blocking them helps keep your account safe.
To disable this security feature:
- Sign in to Gmail
- Click here to access Less Secure App Access in My Account.
- Next to “Allow less secure apps: OFF,” select the toggle switch to turn ON.
This setting may not be available for:
- Accounts with 2-Step Verification enabled: Such accounts require an application-specific password for less secure apps access.
- G Suite users: This setting is hidden if your administrator has locked less secure app account access.
For G-Suite users: Enabling less secure apps to access accounts
- Sign in to your Google Admin console (Sign in using an administrator account, not your current account.
- Click Security > Basic settings.
- Under Less secure apps, select Go to settings for less secure apps.
- In the subwindow, select the Allow users to manage their access to less secure apps radio button.
Once you’ve set Allow users to manage their access to less secure apps to on, affected users within the selected group or Organizational Unit will be able to toggle access for less secure apps on or off themselves.
G-Suite admins: Enforcing access to less secure apps for all users
Use this setting when you want to ensure that access by a less secure app is available to all for a limited time, such as for upgrades.
- Sign in to your Google Admin console.Sign in using an administrator account
- Click Security > Basic settings.
- Under Less secure apps, select Go to settings for less secure apps.
- In the subwindow, select the Enforce access to less secure apps for all users radio button.
Once you’ve set Enforce access to less secure apps for all users to on, affected users within the selected group or Organizational Unit will not be able to toggle access for less secure apps off themselves. You will have to set the setting back to Allow users to manage their access to less secure apps to allow them to toggle access for less secure apps on or off themselves.