Email Example for WINC1500

For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
pkonigsberg
 
Posts: 41
Joined: Wed Apr 11, 2018 1:35 pm

Email Example for WINC1500

Post by pkonigsberg »

All of the email examples I can find use the ESP8266 but I have the newer WINC1500 and can't find any email examples.
I'd like to send an email from the feather M0 with wifi. I'm ok with storing a gmail address and password in the code to log into a gmail account.
Can anyone point me to an example sketch for email using the WINC1500? I've tried EMailSender and some others but they fail to compile and I believe it is because they were made for the ESP8266. Thank you.

User avatar
Greg314
 
Posts: 11
Joined: Wed Mar 10, 2021 12:04 pm

Re: Email Example for WINC1500

Post by Greg314 »

I am trying the same thing. I did some Googling and came up with this in the Arduino forum: https://forum.arduino.cc/index.php?topic=378220.0. I have tried to compile this program but errors kept popping up.

The program that Mr. Patterson wrote deals with attachments and a micro-SD card, which mine does not. So, I removed, what I believe is, the unneeded parts of the code and kept just that having to do with sending an email.

This is what I came up with.

Code: Select all

/*
 * This is my attempt to get the Feather M0 and WiFi101 to send an email.
 * 
 * This is based on "WiFi101Smtp.ino" written by D. R. Patterson in August 2016
 * https://forum.arduino.cc/index.php?topic=378220.0
 */

#include <SPI.h>
#include <WiFi101.h>

#define led 14

char ssid[] = "ssid";                       // network SSID
char pass[] = "password";                   // network password

int wifiStatus = WL_IDLE_STATUS;

String wifiRead;
int x;                                        // general purpose

WiFiClient client;

void setup()
{
  //Configure pins for Adafruit ATWINC1500 Feather M0
  WiFi.setPins(8, 7, 4, 2);

  pinMode(led, OUTPUT);                                     // status led
  digitalWrite(led, LOW);

  Serial.begin(9600);                                       // to talk to ide
  while(!Serial);

  // Connect to WiFi
  while (wifiStatus != WL_CONNECTED)
  {
    Serial.println("Connecting to Exhibits.");
    wifiStatus = WiFi.begin(ssid, pass);

    // five second delay between attempts to connect
    wait5();
  }

  WiFi.config(IPAddress(192, 168, 1, 125));
}

//*************************
//****** LOOP *************
//*************************

void loop()
{
  sendEmail();

  // wait 5 and try again
  wait5();
}

/**********************
***** SUBROUTINES *****
***********************/

void sendEmail()
{
  client.flush();

  Serial.println("Step 1");
  
  if (! client.connect("smtp-relay.gmail.com", 25))
  { 
    Serial.println("Unable to connect.");
    client.stop();
    return;
  }

  Serial.println("Step 2");

  client.println("HELO www.abc.org");
  wifiRead = client.readStringUntil('\n');
  wifiRead += '\n';
  Serial.print(wifiRead);

  client.println("MAIL FROM: <[email protected]");
  wifiRead = client.readStringUntil('\n');
  wifiRead += '\n';
  Serial.print(wifiRead);

  client.println("RCPT TO: <[email protected]>");
  wifiRead = client.readStringUntil('\n');
  wifiRead += '\n';
  Serial.print(wifiRead);

  client.println("DATA");
  wifiRead = client.readStringUntil('\n');
  wifiRead += '\n';
  Serial.print(wifiRead);

  client.println("TO: Exhibit Maintenance <[email protected]>");
  client.println("From: Exhibit <[email protected]>");
  client.println("Subject: FYI");
  client.println("This is a test.  This is only a test.");

  client.println("\r\n.\r\nQUIT");
  wifiRead = client.readStringUntil('\n');
  wifiRead += '\n';
  Serial.print(wifiRead);
}

void wait5()
{
  Serial.print("Countdown: ");
  
  for (x = 5; x > 0; x--)
  {
    Serial.print(x);
    Serial.print(" ");
    delay(1000);
  }
  
  Serial.println("0");
}
My problem is that it never connects to the mail server. It sits there for about 20 seconds then continues. It connects to the network just fine, but not the mail server.

What's interesting is that I use pretty much the same method using an ESP01 via AT codes. It works great, sends an email every week, never fails.

So, I will be watching this thread with eager anticipation, hoping that somebody will come to our rescue.

User avatar
pkonigsberg
 
Posts: 41
Joined: Wed Apr 11, 2018 1:35 pm

Re: Email Example for WINC1500

Post by pkonigsberg »

Interesting code! Thank you for posting. I am wondering if the server connection is more of an issue with google though? I don't believe google would allow a generic device to connect to its mail server and start sending mail through it. Otherwise they'd be a gateway for a ton of spam. I think you either need to use their IMAP API or get login credentials via OAuth and log into a gmail account that you will send the mail from. Does this logic sound right? And if so, does anyone have any experience using either the GMail IMAP API or OAuth login process?

User avatar
Greg314
 
Posts: 11
Joined: Wed Mar 10, 2021 12:04 pm

Re: Email Example for WINC1500

Post by Greg314 »

I don't use Google as my mail server. I didn't say that.

User avatar
pkonigsberg
 
Posts: 41
Joined: Wed Apr 11, 2018 1:35 pm

Re: Email Example for WINC1500

Post by pkonigsberg »

In the code posted above there is a line that reads:
if (! client.connect("smtp-relay.gmail.com", 25))

So that looks like it is trying to connect to a gmail server and I don't think Google would allow that connection without some kind of authentication.

User avatar
pkonigsberg
 
Posts: 41
Joined: Wed Apr 11, 2018 1:35 pm

Re: Email Example for WINC1500

Post by pkonigsberg »

In fact, from
https://support.google.com/a/answer/176 ... mtp-server

It says:
Use the Gmail SMTP server
If you connect using SSL or TLS, you can send mail to anyone inside or outside of your organization using smtp.gmail.com as your server.

This option requires you to authenticate with your Gmail or Google Workspace account and passwords.

User avatar
Greg314
 
Posts: 11
Joined: Wed Mar 10, 2021 12:04 pm

Re: Email Example for WINC1500

Post by Greg314 »

You're right. I am using Google for my mail server. I misspoke and I apologize.

As I understand it, the reason that I can use gmail is because the company is. As long as my device has the company domain, everything is OK. Please, don't ask to many questions about it. I am not a wifi magician.

On a brighter note. I used Telnet (Putty) to connect and, successfully, send several emails. So, there is nothing wrong on that side.

The problem, so far, is with the WiFi101 library. I don't know why it isn't connecting. And a little stumped as to what to do next.

User avatar
pkonigsberg
 
Posts: 41
Joined: Wed Apr 11, 2018 1:35 pm

Re: Email Example for WINC1500

Post by pkonigsberg »

Sadly I've given up for now on trying to send an email from my own GMail account. It appears overly cumbersome and complicated and there were widely varying approaches around the internet. (Not one clear set of instructions.) I do have a web server on AWS, so I have my feather make a web call with variables that pass a test on a php web page that sends an email out. That was my solution. If anyone ever does accomplish it, please post here how you did it. I would love to know.

User avatar
Greg314
 
Posts: 11
Joined: Wed Mar 10, 2021 12:04 pm

Re: Email Example for WINC1500

Post by Greg314 »

I have discovered that not using WiFi.config() makes it work a lot better. My IT wizards have setup a permanent IP address for my module based on its MAC. So I deleted the WiFi.config() line. Now it sends emails with no problem.

Now I have to figure out why the module seems to disconnect from the network during the night. But, that's a different problem.

User avatar
pkonigsberg
 
Posts: 41
Joined: Wed Apr 11, 2018 1:35 pm

Re: Email Example for WINC1500

Post by pkonigsberg »

So your posted code above works with an assigned IP address? The line I had trouble getting to work was:
client.connect("smtp-relay.gmail.com", 25))

User avatar
Greg314
 
Posts: 11
Joined: Wed Mar 10, 2021 12:04 pm

Re: Email Example for WINC1500

Post by Greg314 »

Correct. Let the server assign the IP address.

As for connecting to the mail server, I got nothing. I am making an assumption that your device has a gmail account and is sending emails to other gmail accounts of the same domain. I think that's the key.

Best of luck.

User avatar
Greg314
 
Posts: 11
Joined: Wed Mar 10, 2021 12:04 pm

Re: Email Example for WINC1500

Post by Greg314 »

Well, so much for that idea. My Feather has decided to not connect to gmail again. I have no idea why.

User avatar
Greg314
 
Posts: 11
Joined: Wed Mar 10, 2021 12:04 pm

Re: Email Example for WINC1500

Post by Greg314 »

Turns out that the IT wizards were doing some magic with the firewall. After I mentioned that my Feather couldn't connect, they did some more magic and everything is good again. Something about the IP my feather was using wasn't allowed through the firewall(?).

User avatar
pkonigsberg
 
Posts: 41
Joined: Wed Apr 11, 2018 1:35 pm

Re: Email Example for WINC1500

Post by pkonigsberg »

Thank you for the update. Has there been any changes to your code posted up above?

User avatar
Greg314
 
Posts: 11
Joined: Wed Mar 10, 2021 12:04 pm

Re: Email Example for WINC1500

Post by Greg314 »

Except for deleting the WiFi.config line just before the loop, everything is the same.

Locked
Please be positive and constructive with your questions and comments.

Return to “Wireless: WiFi and Bluetooth”