Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Rui Santos
- Complete project details at https://RandomNerdTutorials.com/esp32-send-email-smtp-server-arduino-ide/
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files.
- The above copyright notice and this permission notice shall be included in all
- copies or substantial portions of the Software.
- */
- #include "ESP32_MailClient.h"
- // REPLACE WITH YOUR NETWORK CREDENTIALS
- const char* ssid = "REPLACE_WITH_YOUR_SSID";
- const char* password = "REPLACE_WITH_YOUR_PASSWORD";
- // To send Email using Gmail use port 465 (SSL) and SMTP Server smtp.gmail.com
- // YOU MUST ENABLE less secure app option https://myaccount.google.com/lesssecureapps?pli=1
- #define emailSenderAccount "[email protected]"
- #define emailSenderPassword "YOUR_EXAMPLE_EMAIL_PASSWORD"
- #define emailRecipient "[email protected]"
- #define smtpServer "smtp.gmail.com"
- #define smtpServerPort 465
- #define emailSubject "ESP32 Test Email"
- // The Email Sending data object contains config and data to send
- SMTPData smtpData;
- // Callback function to get the Email sending status
- void sendCallback(SendStatus info);
- int Led = 4;
- int magnetic = 5;
- int val;
- void setup(){
- Serial.begin(115200);
- Serial.println();
- Serial.print("Connecting");
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- Serial.print(".");
- delay(200);
- }
- Serial.println();
- Serial.println("WiFi connected.");
- Serial.println();
- Serial.println("Preparing to send email");
- Serial.println();
- }
- void loop() {
- val=digitalRead(magnetic);
- if (val==HIGH){
- digitalWrite(Led, HIGH);
- Serial.println("Motion detected");
- sendEmail();
- delay(5000);
- }
- }
- void sendEmail(){
- // Set the SMTP Server Email host, port, account and password
- smtpData.setLogin(smtpServer, smtpServerPort, emailSenderAccount, emailSenderPassword);
- // For library version 1.2.0 and later which STARTTLS protocol was supported,the STARTTLS will be
- // enabled automatically when port 587 was used, or enable it manually using setSTARTTLS function.
- //smtpData.setSTARTTLS(true);
- // Set the sender name and Email
- smtpData.setSender("ESP32", emailSenderAccount);
- // Set Email priority or importance High, Normal, Low or 1 to 5 (1 is highest)
- smtpData.setPriority("High");
- // Set the subject
- smtpData.setSubject(emailSubject);
- // Set the message with HTML format
- smtpData.setMessage("<div style=\"color:#2f4468;\"><h1>H«Magnetic Reed Switch is HIGH</h1><p>- Sent from ESP32 board</p></div>", true);
- // Add recipients, you can add more than one recipient
- smtpData.addRecipient(emailRecipient);
- //smtpData.addRecipient("[email protected]");
- // Add attach files from SD card or SPIFFS
- // Comment the next two lines, if no SPIFFS files created or SD card connected
- smtpData.setSendCallback(sendCallback);
- //Start sending Email, can be set callback function to track the status
- if (!MailClient.sendMail(smtpData))
- Serial.println("Error sending Email, " + MailClient.smtpErrorReason());
- //Clear all data from Email object to free memory
- smtpData.empty();
- }
- // Callback function to get the Email sending status
- void sendCallback(SendStatus msg) {
- // Print the current status
- Serial.println(msg.info());
- // Do something when complete
- if (msg.success()) {
- Serial.println("----------------");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment