Send email with esp8266 and Arduino

Spread the love
  • 7
  •  
  •  
  • 3
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    10
    Shares

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.

Esp8266 and Arduino EMailSender library

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 smtp
  • email_password: password account smtp
  • email_from: email of sender
  • smtp_server: server SMTP
  • smtp_port: SMTP port
  • isSecure = 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 in Gmail

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:

  1. Sign in to Gmail
  2. Click here to access Less Secure App Access in My Account.
  3. Next to “Allow less secure apps: OFF,” select the toggle switch to turn ON.

This setting may not be available for:

For G-Suite users: Enabling less secure apps to access accounts

  1. Sign in to your Google Admin console (Sign in using an administrator account, not your current account.
  2. Click Security > Basic settings.
  3. Under Less secure apps, select Go to settings for less secure apps.
  4. 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.

  1. Sign in to your Google Admin console.Sign in using an administrator account
  2. Click Security > Basic settings.
  3. Under Less secure apps, select Go to settings for less secure apps.
  4. 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.

Thanks

GitHub library


Spread the love
  • 7
  •  
  •  
  • 3
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    10
    Shares

You may also like...

18 Responses

  1. Bulchai Nirattisai says:

    Thank you very much.
    This is so simple and very useful for tons of IoT projects.

  2. Alexandru Goman says:

    Hello sir, let me just start by saying thank you and how amazing this project is, but i have a problem. I can’t compile the code, it gives me this error.

    Arduino: 1.8.10 (Mac OS X), Board: “Arduino/Genuino Uno”

    Multiple libraries were found for “EMailSender.h”
    Used: /Users/alex/Documents/Arduino/libraries/EMailSender-master
    In file included from /Users/alex/Documents/Arduino/libraries/EMailSender-master/examples/EMailSenderTest/EMailSenderTest.ino:2:0:
    /Users/alex/Documents/Arduino/libraries/EMailSender-master/EMailSender.h:10:10: fatal error: WiFiClientSecure.h: No such file or directory
    #include
    ^~~~~~~~~~~~~~~~~~~~
    compilation terminated.
    exit status 1
    Error compiling for board Arduino/Genuino Uno.

    What do you think the cause may be? And what solution do you think there is. Thank you.

  3. alan says:

    This is great, thanks for sharing
    I would also like to use it with an esp32 but it does not compile, would it be possible/easy to convert?

    • Hi Alan,
      I try with an esp32 and It’s work correctly.
      But I check that in VSCode and Eclipse there is a bug in the plugin that give an error on compiling time.
      Try with ArduinoIDE and post your error.
      Thanks Renzo

  4. peter says:

    I’m still trying to make this work!! Do I need to edit the .cpp and .h files?

    I Keep getting this error below? Can you help at all??

    Arduino: 1.8.9 (Mac OS X), Board: “LOLIN(WEMOS) D1 R2 & mini, 80 MHz, Flash, 4M (1M SPIFFS), v2 Lower Memory, Disabled, None, Only Sketch, 921600”

    /Users/wilmsworld/Documents/Arduino/libraries/EMailSender-master2/EMailSender.cpp: In member function ‘EMailSender::Response EMailSender::send(const char*, EMailSender::EMailMessage&)’:
    /Users/wilmsworld/Documents/Arduino/libraries/EMailSender-master2/EMailSender.cpp:169:10: error: base operand of ‘->’ has non-pointer type ‘axTLS::WiFiClientSecure’
    client->setInsecure();
    ^
    /Users/wilmsworld/Documents/Arduino/libraries/EMailSender-master2/EMailSender.cpp:170:22: error: base operand of ‘->’ has non-pointer type ‘axTLS::WiFiClientSecure’
    bool mfln = client->probeMaxFragmentLength(host.c_str(), port, 512);
    ^
    /Users/wilmsworld/Documents/Arduino/libraries/EMailSender-master2/EMailSender.cpp:170:47: error: ‘host’ was not declared in this scope
    bool mfln = client->probeMaxFragmentLength(host.c_str(), port, 512);
    ^
    /Users/wilmsworld/Documents/Arduino/libraries/EMailSender-master2/EMailSender.cpp:170:61: error: ‘port’ was not declared in this scope
    bool mfln = client->probeMaxFragmentLength(host.c_str(), port, 512);
    ^
    /Users/wilmsworld/Documents/Arduino/libraries/EMailSender-master2/EMailSender.cpp:175:11: error: base operand of ‘->’ has non-pointer type ‘axTLS::WiFiClientSecure’
    client->setBufferSizes(512, 512);
    ^
    exit status 1
    Error compiling for board LOLIN(WEMOS) D1 R2 & mini.

    This report would have more information with
    “Show verbose output during compilation”
    option enabled in File -> Preferences.

    • Hi,
      you probably have a 2.4.2 esp8266 core, you must decomment
      //#define ARDUINO_ESP8266_RELEASE_2_4_2
      And tell me if you resolve your issue.

      Bye Renzo

      • peter says:

        Ok so I decommented in the .h file, but I still have errors:-

        /Users/wilmsworld/Documents/Arduino/libraries/EMailSender-master2/EMailSender.cpp: In member function ‘EMailSender::Response EMailSender::send(const char*, EMailSender::EMailMessage&)’:
        EMailSender.cpp:169:10: error: base operand of ‘->’ has non-pointer type ‘axTLS::WiFiClientSecure’
        client->setInsecure();
        ^
        EMailSender.cpp:170:22: error: base operand of ‘->’ has non-pointer type ‘axTLS::WiFiClientSecure’
        bool mfln = client->probeMaxFragmentLength(host.c_str(), port, 512);
        ^
        EMailSender.cpp:170:47: error: ‘host’ was not declared in this scope
        bool mfln = client->probeMaxFragmentLength(host.c_str(), port, 512);
        ^
        EMailSender.cpp:170:61: error: ‘port’ was not declared in this scope
        bool mfln = client->probeMaxFragmentLength(host.c_str(), port, 512);
        ^
        EMailSender.cpp:175:11: error: base operand of ‘->’ has non-pointer type ‘axTLS::WiFiClientSecure’
        client->setBufferSizes(512, 512);
        ^
        exit status 1
        base operand of ‘->’ has non-pointer type ‘axTLS::WiFiClientSecure’

        any ideas??

        • Azz
          I check the Iesterday commit and I see that there is an error, and I commit an updated version now.
          But if you decomment the define that part of code is skipped

          #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_PRINTLN(“MFLN supported: %s\n”, mfln ? “yes” : “no”);

          if (mfln) {
          client.setBufferSizes(512, 512);
          }
          }
          #endif

          Try to download last version, decomment define
          #define ARDUINO_ESP8266_RELEASE_2_4_2

          and rebuild with pay attention if the EmailSender library is updated.

          Please send me the result, Thanks Renzo

          • peter says:

            Thats great it works perfectly now! Many thanks for your work and your fast replies to my questions!!

  5. benate100 says:

    Bonjour, Monsieur,
    c’était pour voir s’il y avait un moyen d’envoyer des variables de notre programme ainsi que du texte en html.

  6. benate100 says:

    Hello again, I come back to you because when I want to send a mail, I get the error “Could not connect to mail server”. Do you have an idea where this can come from?

    my settup:
    uint16_t smtp_port = 465;
    char* smtp_server = strdup(“smtp.gmail.com”);

Leave a Reply

Your email address will not be published. Required fields are marked *