Guest User

Untitled

a guest
Feb 22nd, 2020
2,570
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. /*
  2. Rui Santos
  3. Complete project details at https://RandomNerdTutorials.com/esp32-send-email-smtp-server-arduino-ide/
  4.  
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files.
  7.  
  8. The above copyright notice and this permission notice shall be included in all
  9. copies or substantial portions of the Software.
  10. */
  11.  
  12. #include "ESP32_MailClient.h"
  13.  
  14. // REPLACE WITH YOUR NETWORK CREDENTIALS
  15. const char* ssid = "REPLACE_WITH_YOUR_SSID";
  16. const char* password = "REPLACE_WITH_YOUR_PASSWORD";
  17.  
  18. // To send Email using Gmail use port 465 (SSL) and SMTP Server smtp.gmail.com
  19. // YOU MUST ENABLE less secure app option https://myaccount.google.com/lesssecureapps?pli=1
  20. #define emailSenderAccount "[email protected]"
  21. #define emailSenderPassword "YOUR_EXAMPLE_EMAIL_PASSWORD"
  22. #define emailRecipient "[email protected]"
  23. #define smtpServer "smtp.gmail.com"
  24. #define smtpServerPort 465
  25. #define emailSubject "ESP32 Test Email"
  26.  
  27. // The Email Sending data object contains config and data to send
  28. SMTPData smtpData;
  29.  
  30. // Callback function to get the Email sending status
  31. void sendCallback(SendStatus info);
  32.  
  33. int Led = 4;
  34. int magnetic = 5;
  35. int val;
  36.  
  37. void setup(){
  38. Serial.begin(115200);
  39. Serial.println();
  40.  
  41. Serial.print("Connecting");
  42.  
  43. WiFi.begin(ssid, password);
  44. while (WiFi.status() != WL_CONNECTED) {
  45. Serial.print(".");
  46. delay(200);
  47. }
  48.  
  49. Serial.println();
  50. Serial.println("WiFi connected.");
  51. Serial.println();
  52. Serial.println("Preparing to send email");
  53. Serial.println();
  54. }
  55. void loop() {
  56. val=digitalRead(magnetic);
  57. if (val==HIGH){
  58. digitalWrite(Led, HIGH);
  59. Serial.println("Motion detected");
  60. sendEmail();
  61. delay(5000);
  62. }
  63. }
  64.  
  65. void sendEmail(){
  66. // Set the SMTP Server Email host, port, account and password
  67. smtpData.setLogin(smtpServer, smtpServerPort, emailSenderAccount, emailSenderPassword);
  68.  
  69. // For library version 1.2.0 and later which STARTTLS protocol was supported,the STARTTLS will be
  70. // enabled automatically when port 587 was used, or enable it manually using setSTARTTLS function.
  71. //smtpData.setSTARTTLS(true);
  72.  
  73. // Set the sender name and Email
  74. smtpData.setSender("ESP32", emailSenderAccount);
  75.  
  76. // Set Email priority or importance High, Normal, Low or 1 to 5 (1 is highest)
  77. smtpData.setPriority("High");
  78.  
  79. // Set the subject
  80. smtpData.setSubject(emailSubject);
  81.  
  82. // Set the message with HTML format
  83. smtpData.setMessage("<div style=\"color:#2f4468;\"><h1>H«Magnetic Reed Switch is HIGH</h1><p>- Sent from ESP32 board</p></div>", true);
  84.  
  85. // Add recipients, you can add more than one recipient
  86. smtpData.addRecipient(emailRecipient);
  87. //smtpData.addRecipient("[email protected]");
  88.  
  89. // Add attach files from SD card or SPIFFS
  90. // Comment the next two lines, if no SPIFFS files created or SD card connected
  91.  
  92. smtpData.setSendCallback(sendCallback);
  93.  
  94. //Start sending Email, can be set callback function to track the status
  95. if (!MailClient.sendMail(smtpData))
  96. Serial.println("Error sending Email, " + MailClient.smtpErrorReason());
  97.  
  98. //Clear all data from Email object to free memory
  99. smtpData.empty();
  100. }
  101.  
  102.  
  103. // Callback function to get the Email sending status
  104. void sendCallback(SendStatus msg) {
  105. // Print the current status
  106. Serial.println(msg.info());
  107.  
  108. // Do something when complete
  109. if (msg.success()) {
  110. Serial.println("----------------");
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment