Skip to content

Commit

Permalink
Fix network and SSL client issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
mobizt committed Sep 11, 2023
1 parent 6e8a71e commit c6ce44f
Show file tree
Hide file tree
Showing 32 changed files with 751 additions and 1,489 deletions.
65 changes: 5 additions & 60 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -755,9 +755,9 @@ This library supports external netwoking devices e.g. WiFi modules, Ethernet mod

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

No additional setup needed, only pass the Arduino Client to the function `setClient` or pass the TinyGSMClient and TinyGSM modem to the function `setGSMClient`.
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`.

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

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

Expand Down Expand Up @@ -982,68 +982,18 @@ The below example will use ESP32 and W5500 and Ethernet client library to connec
#define WIZNET_SCLK_PIN 18 // Connect W5500 SCLK pin to GPIO 18 of ESP32
EthernetClient eth_client;
uint8_t Eth_MAC[] = {0x02, 0xF0, 0x0D, 0xBE, 0xEF, 0x01};
SMTPSession smtp;
SMTPSession smtp;
Session_Config config;
EthernetClient eth_client;
// Callback function to get the Email sending status
void smtpCallback(SMTP_Status status);
void ResetEthernet()
{
Serial.println("Resetting WIZnet W5500 Ethernet Board... ");
pinMode(WIZNET_RESET_PIN, OUTPUT);
digitalWrite(WIZNET_RESET_PIN, HIGH);
delay(200);
digitalWrite(WIZNET_RESET_PIN, LOW);
delay(50);
digitalWrite(WIZNET_RESET_PIN, HIGH);
delay(200);
}
void networkConnection()
{
Ethernet.init(WIZNET_CS_PIN);
ResetEthernet();
Serial.println("Starting Ethernet connection...");
Ethernet.begin(Eth_MAC);
unsigned long to = millis();
while (Ethernet.linkStatus() == LinkOFF || millis() - to < 2000)
{
delay(100);
}
if (Ethernet.linkStatus() == LinkON)
{
Serial.print("Connected with IP ");
Serial.println(Ethernet.localIP());
}
else
{
Serial.println("Can't connect");
}
}
void networkStatusRequestCallback()
{
smtp.setNetworkStatus(Ethernet.linkStatus() == LinkON);
}
void setup()
{
Serial.begin(115200);
networkConnection();
config.server.host_name = "smtp.gmail.com"; //for gmail.com
config.server.port = 587; // requires connection upgrade via STARTTLS
config.login.email = "your Email address"; //set to empty for no SMTP Authentication
Expand All @@ -1066,12 +1016,7 @@ void setup()
// Set the message content
message.text.content = "This is simple plain text message";
// Set the callback function for connection upgrade
smtp.networkStatusRequestCallback(networkStatusRequestCallback);
smtp.networkConnectionRequestCallback(networkConnection);
smtp.setClient(&eth_client);
smtp.setEthernetClient(&eth_client, Eth_MAC, WIZNET_CS_PIN, WIZNET_RESET_PIN);
// Set debug option
smtp.debug(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,22 @@
* Github: https://github.com/mobizt/ESP-Mail-Client
*
* Copyright (c) 2023 mobizt
*/
*/

/**
* This example shows how to send Email using EthernetClient.
*
* This example used ESP32 and WIZnet W5500 Ethernet module.
*
* For ESP32 and LAN8720 see examples/SMTP/Ethernet/ESP32/Send_Text.ino
*
* ESP32 Arduino SDK native Ethernet using ETH.h is currently support Ethernet PHY chips
*
* For ESP32 and LAN8720 see examples/SMTP/Ethernet/ESP32/Send_Text.ino.
*
* ESP32 Arduino SDK native Ethernet using ETH.h is currently support Ethernet PHY chips included the following
* LAN8720, TLK101, RTL8201, DP83848, DM9051, KSZ8041 and KSZ8081.
*
* For ESP8266, the native Ethernet is currently supported ENC28J60, W5100 and W5500.
*
* You do not need to set external Client with native Ethernet support PHY/MAC chips.
*
*
*/

/** Note for library update from v2.x.x to v3.x.x.
*
*
* Struct data names changed
*
* "ESP_Mail_Session" changes to "Session_Config"
Expand Down Expand Up @@ -69,57 +63,21 @@ const int analog_pin = 34;

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

/**
IPAddress localIP(192, 168, 1, 104);
IPAddress subnet(255, 255, 0, 0);
IPAddress gateway(192, 168, 1, 1);
IPAddress dnsServer(8, 8, 8, 8);
bool optional = false; // Use this static IP only no DHCP
ESP_Mail_StaticIP staIP(localIP, subnet, gateway, dnsServer, optional);
*/

SMTPSession smtp;

EthernetClient eth_client;

void smtpCallback(SMTP_Status status);

void ResetEthernet()
{
Serial.println("Resetting WIZnet W5500 Ethernet Board... ");
pinMode(WIZNET_RESET_PIN, OUTPUT);
digitalWrite(WIZNET_RESET_PIN, HIGH);
delay(200);
digitalWrite(WIZNET_RESET_PIN, LOW);
delay(50);
digitalWrite(WIZNET_RESET_PIN, HIGH);
delay(200);
}

void networkConnection()
{

Ethernet.init(WIZNET_CS_PIN);

ResetEthernet();

Serial.println("Starting Ethernet connection...");
Ethernet.begin(Eth_MAC);

unsigned long to = millis();

while (Ethernet.linkStatus() == LinkOFF || millis() - to < 2000)
{
delay(100);
}

if (Ethernet.linkStatus() == LinkON)
{
Serial.print("Connected with IP ");
Serial.println(Ethernet.localIP());
}
else
{
Serial.println("Can't connect");
}
}

void networkStatusRequestCallback()
{
smtp.setNetworkStatus(Ethernet.linkStatus() == LinkON);
}

void sendEmail()
{

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

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

smtp.setClient(&eth_client);

smtp.networkConnectionRequestCallback(networkConnection);

smtp.networkStatusRequestCallback(networkStatusRequestCallback);
/* Assign the pointer to global defined Ethernet Client object */
smtp.setEthernetClient(&eth_client, Eth_MAC, WIZNET_CS_PIN, WIZNET_RESET_PIN); // The staIP can be assigned to the fifth param

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

Serial.println();

networkConnection();

/*
For internal NTP client
For times east of the Prime Meridian use 0-12
For times west of the Prime Meridian add 12 to the offset.
Ex. American/Denver GMT would be -6. 6 + 12 = 18
See https://en.wikipedia.org/wiki/Time_zone for a list of the GMT/UTC timezone offsets
*/
MailClient.setUDPClient(&udp_client, 0 /* GMT offset */);

smtp.debug(1);

smtp.callback(smtpCallback);
Expand Down
Loading

0 comments on commit c6ce44f

Please sign in to comment.