• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

ESP32 Send Emails using an SMTP Server: HTML, Text and Attachments (Arduino IDE)

Q&A Forum › Category: ESP32 › ESP32 Send Emails using an SMTP Server: HTML, Text and Attachments (Arduino IDE)
0 Vote Up Vote Down
Ashish Garg asked 5 years ago

I want to send an email when my Magnetic switch is HIGH. Magnetic Switch is connected to ESP32. So in which part of the code I should add this?

<
int Led = 4;
int magnetic = 5;
int val;
val=digitalRead(magnetic);
if (val==HIGH)
{
digitalWrite(Led, HIGH);
Serial.println("Motion detected");
delay(1000);
}
>

It might be a simple question but I am new in Arduino, and ESP32, so please help me.

I am following codes from this link https://randomnerdtutorials.com/esp32-send-email-smtp-server-arduino-ide/#comment-429759

Question Tags: ESP32, SMTP
7 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 5 years ago

Hi.

Move your variable definitions before the setup().

Move the other lines of code to the loop().

After detecting motion add the lines to send an email. I recommend creating a function that contains all the code to send the email.

I also recommend increasing the delay time a bit to prevent getting multiple emails.

Here’s an example (don’t forget to modify with your own details: sender email, recipient email and network credentials):
https://pastebin.com/wU73Nvew

I hope this helps.

Regards,
Sara

0 Vote Up Vote Down
Ashish Garg answered 5 years ago

Thanks a lot, mam. But after running this I am getting error.  I added my email and password and checked couple of times and even used a different one.

Start your code here17:57:31.363 -> Connecting.
17:57:31.676 -> WiFi connected.
17:57:31.676 ->
17:57:31.676 -> Preparing to send email
17:57:31.676 ->
17:57:34.249 -> Motion detected
17:57:34.249 -> Connecting to SMTP server...
17:57:38.271 -> Identification...
17:57:38.338 -> Authentication...
17:57:38.372 -> Sign in...
17:57:38.471 -> Error, login password is not valid
17:57:38.471 -> Error sending Email, login password is not valid
17:57:45.988 -> Motion detected
17:57:45.988 -> Connecting to SMTP server...
17:57:49.993 -> Identification...
17:57:50.026 -> Authentication...
17:57:50.059 -> Sign in...
17:57:50.159 -> Error, login password is not valid
17:57:50.159 -> Error sending Email, login password is not valid
18:00:53.765 -> Motion detected
18:00:53.765 -> Connecting to SMTP server...
18:00:57.799 -> Identification...
18:00:57.866 -> Authentication...
18:00:57.899 -> Sign in...
18:00:58.000 -> Error, login password is not valid
18:00:58.000 -> Error sending Email, login password is not valid
18:01:17.997 -> Motion detected
18:01:17.997 -> Connecting to SMTP server...
18:01:22.356 -> Identification...
18:01:22.425 -> Authentication...
18:01:22.460 -> Sign in...
18:01:22.632 -> Error, login password is not valid
18:01:22.632 -> Error sending Email, login password is not valid
18:01:27.617 -> Motion detected
18:01:27.617 -> Connecting to SMTP server...
18:01:31.568 -> Identification...
18:01:31.603 -> Authentication...
18:01:31.638 -> Sign in...
18:01:31.740 -> Error, login password is not valid
18:01:31.740 -> Error sending Email, login password is not valid
0 Vote Up Vote Down
Sara Santos Staff answered 5 years ago

Hi.
The error says that password or email is not correct.
What email provider are you using? It may have different SMTP server settings. Did you check that?
Regards,
Sara

0 Vote Up Vote Down
Ashish Garg answered 5 years ago

Mam, I am using Gmail.
Thanks 

0 Vote Up Vote Down
Sara Santos Staff answered 5 years ago

Hi again.
If you’re using gmail, you don’t need to change the SMTP server settings on our example.
The error you’re getting says that the password is not valid. The password is for the email sender account, not for the receiver email account.
Without further information I can’t understand what might be wrong :/
Regards,
Sara

0 Vote Up Vote Down
Ashish Garg answered 5 years ago

Sorry for the late response. I solved my problem. Thanks a lot, Mam. I really appreciate your help.

0 Vote Up Vote Down
Sara Santos Staff answered 5 years ago

Hi.
That’s great.
I’ll close this question.
If you need further help, you just need to open a new question in our forum.
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025
  • [eBook Updated] Learn ESP32 with Arduino IDE eBook – Version 3.2 April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.