Skip to content

Commit c6ce44f

Browse files
committed
Fix network and SSL client issues.
1 parent 6e8a71e commit c6ce44f

32 files changed

+751
-1489
lines changed

README.md

Lines changed: 5 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -755,9 +755,9 @@ This library supports external netwoking devices e.g. WiFi modules, Ethernet mod
755755

756756
Since v3.4.0, the Arduino Clients can be used with this library without additional external SSL Client required.
757757

758-
No additional setup needed, only pass the Arduino Client to the function `setClient` or pass the TinyGSMClient and TinyGSM modem to the function `setGSMClient`.
758+
No additional setup needed, only pass the Arduino Client to the function `setClient` or pass the TinyGSMClient and TinyGSM modem to the function `setGSMClient` or pass the Ethernet client and mac address to the function `setEthernetClient`.
759759

760-
Two callback functions required (except for using `setGSMClient`) for network connection (with disconnection) and sending connecting status back to the Mail Client.
760+
Two callback functions are required (except for `setGSMClient` and `setEthernetClient`) for network connection (with disconnection) and sending connecting status back to the Mail Client.
761761

762762
If device has on-board WiFi and supports native (SDK) Ethernet, these two native networks will be auto detectd and used.
763763

@@ -982,68 +982,18 @@ The below example will use ESP32 and W5500 and Ethernet client library to connec
982982
#define WIZNET_SCLK_PIN 18 // Connect W5500 SCLK pin to GPIO 18 of ESP32
983983
984984
985-
EthernetClient eth_client;
986-
987985
uint8_t Eth_MAC[] = {0x02, 0xF0, 0x0D, 0xBE, 0xEF, 0x01};
988986
989-
SMTPSession smtp;
987+
SMTPSession smtp;
990988
991-
Session_Config config;
989+
EthernetClient eth_client;
992990
993-
// Callback function to get the Email sending status
994991
void smtpCallback(SMTP_Status status);
995992
996-
void ResetEthernet()
997-
{
998-
Serial.println("Resetting WIZnet W5500 Ethernet Board... ");
999-
pinMode(WIZNET_RESET_PIN, OUTPUT);
1000-
digitalWrite(WIZNET_RESET_PIN, HIGH);
1001-
delay(200);
1002-
digitalWrite(WIZNET_RESET_PIN, LOW);
1003-
delay(50);
1004-
digitalWrite(WIZNET_RESET_PIN, HIGH);
1005-
delay(200);
1006-
}
1007-
1008-
void networkConnection()
1009-
{
1010-
1011-
Ethernet.init(WIZNET_CS_PIN);
1012-
1013-
ResetEthernet();
1014-
1015-
Serial.println("Starting Ethernet connection...");
1016-
Ethernet.begin(Eth_MAC);
1017-
1018-
unsigned long to = millis();
1019-
1020-
while (Ethernet.linkStatus() == LinkOFF || millis() - to < 2000)
1021-
{
1022-
delay(100);
1023-
}
1024-
1025-
if (Ethernet.linkStatus() == LinkON)
1026-
{
1027-
Serial.print("Connected with IP ");
1028-
Serial.println(Ethernet.localIP());
1029-
}
1030-
else
1031-
{
1032-
Serial.println("Can't connect");
1033-
}
1034-
}
1035-
1036-
void networkStatusRequestCallback()
1037-
{
1038-
smtp.setNetworkStatus(Ethernet.linkStatus() == LinkON);
1039-
}
1040-
1041993
void setup()
1042994
{
1043995
Serial.begin(115200);
1044996
1045-
networkConnection();
1046-
1047997
config.server.host_name = "smtp.gmail.com"; //for gmail.com
1048998
config.server.port = 587; // requires connection upgrade via STARTTLS
1049999
config.login.email = "your Email address"; //set to empty for no SMTP Authentication
@@ -1066,12 +1016,7 @@ void setup()
10661016
// Set the message content
10671017
message.text.content = "This is simple plain text message";
10681018
1069-
// Set the callback function for connection upgrade
1070-
smtp.networkStatusRequestCallback(networkStatusRequestCallback);
1071-
1072-
smtp.networkConnectionRequestCallback(networkConnection);
1073-
1074-
smtp.setClient(&eth_client);
1019+
smtp.setEthernetClient(&eth_client, Eth_MAC, WIZNET_CS_PIN, WIZNET_RESET_PIN);
10751020
10761021
// Set debug option
10771022
smtp.debug(1);

examples/SMTP/External_Client/EthernetClient/EthernetClient.ino renamed to examples/SMTP/External_Client/EthernetClient/ESP32/ESP32.ino

Lines changed: 16 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,22 @@
66
* Github: https://github.com/mobizt/ESP-Mail-Client
77
*
88
* Copyright (c) 2023 mobizt
9-
*/
9+
*/
1010

1111
/**
1212
* This example shows how to send Email using EthernetClient.
1313
*
1414
* This example used ESP32 and WIZnet W5500 Ethernet module.
1515
*
16-
* For ESP32 and LAN8720 see examples/SMTP/Ethernet/ESP32/Send_Text.ino
17-
*
18-
* ESP32 Arduino SDK native Ethernet using ETH.h is currently support Ethernet PHY chips
19-
*
16+
* For ESP32 and LAN8720 see examples/SMTP/Ethernet/ESP32/Send_Text.ino.
17+
*
18+
* ESP32 Arduino SDK native Ethernet using ETH.h is currently support Ethernet PHY chips included the following
2019
* LAN8720, TLK101, RTL8201, DP83848, DM9051, KSZ8041 and KSZ8081.
21-
*
22-
* For ESP8266, the native Ethernet is currently supported ENC28J60, W5100 and W5500.
23-
*
24-
* You do not need to set external Client with native Ethernet support PHY/MAC chips.
25-
*
2620
*
2721
*/
2822

2923
/** Note for library update from v2.x.x to v3.x.x.
30-
*
24+
*
3125
* Struct data names changed
3226
*
3327
* "ESP_Mail_Session" changes to "Session_Config"
@@ -69,57 +63,21 @@ const int analog_pin = 34;
6963

7064
uint8_t Eth_MAC[] = {0x02, 0xF0, 0x0D, 0xBE, 0xEF, 0x01};
7165

66+
/**
67+
IPAddress localIP(192, 168, 1, 104);
68+
IPAddress subnet(255, 255, 0, 0);
69+
IPAddress gateway(192, 168, 1, 1);
70+
IPAddress dnsServer(8, 8, 8, 8);
71+
bool optional = false; // Use this static IP only no DHCP
72+
ESP_Mail_StaticIP staIP(localIP, subnet, gateway, dnsServer, optional);
73+
*/
74+
7275
SMTPSession smtp;
7376

7477
EthernetClient eth_client;
7578

7679
void smtpCallback(SMTP_Status status);
7780

78-
void ResetEthernet()
79-
{
80-
Serial.println("Resetting WIZnet W5500 Ethernet Board... ");
81-
pinMode(WIZNET_RESET_PIN, OUTPUT);
82-
digitalWrite(WIZNET_RESET_PIN, HIGH);
83-
delay(200);
84-
digitalWrite(WIZNET_RESET_PIN, LOW);
85-
delay(50);
86-
digitalWrite(WIZNET_RESET_PIN, HIGH);
87-
delay(200);
88-
}
89-
90-
void networkConnection()
91-
{
92-
93-
Ethernet.init(WIZNET_CS_PIN);
94-
95-
ResetEthernet();
96-
97-
Serial.println("Starting Ethernet connection...");
98-
Ethernet.begin(Eth_MAC);
99-
100-
unsigned long to = millis();
101-
102-
while (Ethernet.linkStatus() == LinkOFF || millis() - to < 2000)
103-
{
104-
delay(100);
105-
}
106-
107-
if (Ethernet.linkStatus() == LinkON)
108-
{
109-
Serial.print("Connected with IP ");
110-
Serial.println(Ethernet.localIP());
111-
}
112-
else
113-
{
114-
Serial.println("Can't connect");
115-
}
116-
}
117-
118-
void networkStatusRequestCallback()
119-
{
120-
smtp.setNetworkStatus(Ethernet.linkStatus() == LinkON);
121-
}
122-
12381
void sendEmail()
12482
{
12583

@@ -142,11 +100,8 @@ void sendEmail()
142100

143101
message.text.content = "This is simple plain text message";
144102

145-
smtp.setClient(&eth_client);
146-
147-
smtp.networkConnectionRequestCallback(networkConnection);
148-
149-
smtp.networkStatusRequestCallback(networkStatusRequestCallback);
103+
/* Assign the pointer to global defined Ethernet Client object */
104+
smtp.setEthernetClient(&eth_client, Eth_MAC, WIZNET_CS_PIN, WIZNET_RESET_PIN); // The staIP can be assigned to the fifth param
150105

151106
if (!smtp.connect(&config))
152107
{
@@ -172,17 +127,6 @@ void setup()
172127

173128
Serial.println();
174129

175-
networkConnection();
176-
177-
/*
178-
For internal NTP client
179-
For times east of the Prime Meridian use 0-12
180-
For times west of the Prime Meridian add 12 to the offset.
181-
Ex. American/Denver GMT would be -6. 6 + 12 = 18
182-
See https://en.wikipedia.org/wiki/Time_zone for a list of the GMT/UTC timezone offsets
183-
*/
184-
MailClient.setUDPClient(&udp_client, 0 /* GMT offset */);
185-
186130
smtp.debug(1);
187131

188132
smtp.callback(smtpCallback);

0 commit comments

Comments
 (0)