Guest User

Untitled

a guest
Jun 25th, 2018
535
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. const char* ssid = "SSID"; // Enter the SSID of your WiFi Network.
  3. const char* password = "PASSWORD";// Enter the Password of your WiFi Network.
  4. char server[] = "mail.smtp2go.com"; // The SMTP Server
  5.  
  6. WiFiClient espClient;
  7. void setup()
  8. {
  9. Serial.begin(115200);
  10. delay(10);
  11. Serial.println("");
  12. Serial.println("");
  13. Serial.print("Connecting To: ");
  14. Serial.println(ssid);
  15. WiFi.begin(ssid, password);
  16.  
  17. while (WiFi.status() != WL_CONNECTED)
  18. {
  19. delay(500);
  20. Serial.print("*");
  21. }
  22. Serial.println("");
  23. Serial.println("WiFi Connected.");
  24. Serial.print("IP address: ");
  25. Serial.println(WiFi.localIP());
  26. byte ret = sendEmail();
  27. }
  28.  
  29. void loop()
  30. {
  31.  
  32. }
  33.  
  34. byte sendEmail()
  35. {
  36. if (espClient.connect(server, 2525) == 1)
  37. {
  38. Serial.println(F("connected"));
  39. }
  40. else
  41. {
  42. Serial.println(F("connection failed"));
  43. return 0;
  44. }
  45. if (!emailResp())
  46. return 0;
  47. //
  48. Serial.println(F("Sending EHLO"));
  49. espClient.println("EHLO www.example.com");
  50. if (!emailResp())
  51. return 0;
  52. //
  53. /*Serial.println(F("Sending TTLS"));
  54. espClient.println("STARTTLS");
  55. if (!emailResp())
  56. return 0;*/
  57. //
  58. Serial.println(F("Sending auth login"));
  59. espClient.println("AUTH LOGIN");
  60. if (!emailResp())
  61. return 0;
  62. //
  63. Serial.println(F("Sending User"));
  64. // Change this to your base64, ASCII encoded username
  65. /*
  66. For example, the email address test@gmail.com would be encoded as dGVzdEBnbWFpbC5jb20=
  67. */
  68. espClient.println("dGVzdEBnbWFpbC5jb20="); //base64, ASCII encoded Username
  69. if (!emailResp())
  70. return 0;
  71. //
  72. Serial.println(F("Sending Password"));
  73. // change to your base64, ASCII encoded password
  74. /*
  75. For example, if your password is "testpassword" (excluding the quotes),
  76. it would be encoded as dGVzdHBhc3N3b3Jk
  77. */
  78. espClient.println("dGVzdHBhc3N3b3Jk");//base64, ASCII encoded Password
  79. if (!emailResp())
  80. return 0;
  81. //
  82. Serial.println(F("Sending From"));
  83. // change to sender email address
  84. espClient.println(F("MAIL From: sender@gmail.com"));
  85. if (!emailResp())
  86. return 0;
  87. // change to recipient address
  88. Serial.println(F("Sending To"));
  89. espClient.println(F("RCPT To: receiver@gmail.com"));
  90. if (!emailResp())
  91. return 0;
  92. //
  93. Serial.println(F("Sending DATA"));
  94. espClient.println(F("DATA"));
  95. if (!emailResp())
  96. return 0;
  97. Serial.println(F("Sending email"));
  98. // change to recipient address
  99. espClient.println(F("To: receiver@gmail.com"));
  100. // change to your address
  101. espClient.println(F("From: sender@gmail.com"));
  102. espClient.println(F("Subject: ESP8266 test e-mail\r\n"));
  103. espClient.println(F("This is is a test e-mail sent from ESP8266.\n"));
  104. espClient.println(F("Second line of the test e-mail."));
  105. espClient.println(F("Third line of the test e-mail."));
  106. //
  107. espClient.println(F("."));
  108. if (!emailResp())
  109. return 0;
  110. //
  111. Serial.println(F("Sending QUIT"));
  112. espClient.println(F("QUIT"));
  113. if (!emailResp())
  114. return 0;
  115. //
  116. espClient.stop();
  117. Serial.println(F("disconnected"));
  118. return 1;
  119. }
  120.  
  121. byte emailResp()
  122. {
  123. byte responseCode;
  124. byte readByte;
  125. int loopCount = 0;
  126.  
  127. while (!espClient.available())
  128. {
  129. delay(1);
  130. loopCount++;
  131. // Wait for 20 seconds and if nothing is received, stop.
  132. if (loopCount > 20000)
  133. {
  134. espClient.stop();
  135. Serial.println(F("\r\nTimeout"));
  136. return 0;
  137. }
  138. }
  139.  
  140. responseCode = espClient.peek();
  141. while (espClient.available())
  142. {
  143. readByte = espClient.read();
  144. Serial.write(readByte);
  145. }
  146.  
  147. if (responseCode >= '4')
  148. {
  149. // efail();
  150. return 0;
  151. }
  152. return 1;
  153. }
Add Comment
Please, Sign In to add comment