Jump to content
xeebot

[SOLVED] Sending Email To Hosted Server

Recommended Posts

Back in June, I wrote a very basic sketch that sent an email via my hosted SMTP server. Unfortunately, the USB drive I stored the sketch on is toast.

I don't recall the source and after digging through the examples, libraries and the forum, I'm unable to find an example I might have drawn from.

Does anyone have an idea of what code example I might have used? I know I didn't write anything from scratch. I also know I didn't use Temboo or SMTP2Go, as I still have the email in my gmail mailbox (and it came from my hosted domain).

 

Share this post


Link to post
Share on other sites

I don't think that is it, BUT it looks great. I should be able to just switch Ethernet for WiFi and go to town.

Thank you, sir!!!!

Share this post


Link to post
Share on other sites

@energia

Sorry for the delay following up on this - had some family medical issues that I had to be present for.

If it is okay, I'm posting my version here in case somebody else needs to do the same thing, will save them a few steps

I basically combined the sketch you linked to with the WPA example - this allowed me to get details about the board (mac, IP) and do basic troubleshooting. Switched the Ethernet client to WiFi and plugged in my own values for SMTP and email addresses.  I put a comment in indicating which lines somebody who wants to use this would need to change.

My next step is to combine this with the "test" firmware I wrote for my PCB, and, if everything works on the Launchpad, upload the bin using Uniflash. Will let you know how that works out!

Code from attached Sketch:
 

//combines the WPA WiFi example with the Arduino "panic email" sketch - uses WiFi client in place of Ethernet - WPA client shows data about your board and connection in serial monitor
//
//replace the variables on lines 8, 10, 12, 15, 17, 18, 67, 71, 75, 82, and 84 (for your board and your SMTP server)
//
//If all variables are correct, board connects to router then waits until SW3 is pressed, then sends email. 
//
#include <SPI.h>
#include <WiFi.h>
WiFiClient client;
//the Pin for SW3 on a launchpad
int buttonPinSW3 = 3;
//your router's SSID
char ssid[] = "YourSSID";
//your router's password
char password[] = "YourPassword";
boolean statusCheck=false; 
//your board's mac address
byte mac[] = { 0xDA, 0xE5, 0x13, 0x25, 0x47, 0xB8 }; 
//your SMTP server
char server[] = "mail.send.com";
int SMTPport = 25;
void setup()
{
  Serial.begin(115200);
  Serial.print("Attempting to connect to Network named: ");
  Serial.println(ssid); 
  WiFi.begin(ssid, password);
  while ( WiFi.status() != WL_CONNECTED) {
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid); 
    Serial.println(".");
    delay(300);
  }  
  Serial.println("\nYou're connected to the network");
  Serial.println("Waiting for an ip address");
  while (WiFi.localIP() == INADDR_NONE) {
    Serial.print(".");
    delay(300);
  }
  Serial.println("\nIP Address obtained");
  printCurrentNet();
  printWifiData();  
  pinMode(buttonPinSW3, INPUT);
}
void loop()
{
  if(digitalRead(buttonPinSW3)==HIGH && statusCheck==false)
  {
      if(sendEmail()) Serial.println(F("Email sent"));
      else Serial.println(F("Email failed"));
   statusCheck = true;
  }
  else if (digitalRead(buttonPinSW3)==LOW)
  {
  statusCheck = false; 
  }
}
byte sendEmail()
{
  byte thisByte = 0;
  byte respCode;
  if(client.connect(server,SMTPport)) {
    Serial.println(F("connected"));
  } else {
    Serial.println(F("connection failed"));
    return 0;
  }
  if(!eRcv()) return 0;
// change to your public ip
  client.println(F("helo [173.6.157.102]"));
  if(!eRcv()) return 0;
  Serial.println(F("Sending From"));
// change to your email address (sender)
  client.println(F("MAIL From: <sender@send.com>"));
  if(!eRcv()) return 0;
// change to recipient address
  Serial.println(F("Sending To"));
  client.println(F("RCPT To: <reader@readme.com>"));
  if(!eRcv()) return 0;
  Serial.println(F("Sending DATA"));
  client.println(F("DATA"));
  if(!eRcv()) return 0;
  Serial.println(F("Sending email"));
// change to recipient address
  client.println(F("To: You <reader@readme.com>"));
// change to your address
  client.println(F("From: Me <sender@send.com>"));
  client.println(F("Subject: Panic Alarm!\r\n"));
  client.println(F("The panic button was pressed!!!"));
  client.println(F("."));
  if(!eRcv()) return 0;
  Serial.println(F("Sending QUIT"));
  client.println(F("QUIT"));
  if(!eRcv()) return 0;
  client.stop();
  Serial.println(F("disconnected"));
  return 1;
}
byte eRcv() {
  byte respCode;
  byte thisByte;
  int loopCount = 0;
  while(!client.available()) {
    delay(1);
    loopCount++;
    // if nothing received for 10 seconds, timeout
    if(loopCount > 10000) {
      client.stop();
      Serial.println(F("\r\nTimeout"));
      return 0;
    }
  }
  respCode = client.peek();
  while(client.available())
  {  
    thisByte = client.read();    
    Serial.write(thisByte);
  }
  if(respCode >= '4')
  {
    efail();
    return 0;  
  }
  return 1;
}
void efail() {
  byte thisByte = 0;
  int loopCount = 0;
  client.println(F("QUIT"));
  while(!client.available()) {
    delay(1);
    loopCount++;
    // if nothing received for 10 seconds, timeout
    if(loopCount > 10000) {
      client.stop();
      Serial.println(F("\r\nTimeout"));
      return;
    }
  }
  while(client.available())
  {  
    thisByte = client.read();    
    Serial.write(thisByte);
  }
  client.stop();
  Serial.println(F("disconnected"));
}
void printWifiData() {
  // print your WiFi IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
  Serial.println(ip);
  byte mac[6];
  WiFi.macAddress(mac);
  Serial.print("MAC address: ");
  Serial.print(mac[5], HEX);
  Serial.print(":");
  Serial.print(mac[4], HEX);
  Serial.print(":");
  Serial.print(mac[3], HEX);
  Serial.print(":");
  Serial.print(mac[2], HEX);
  Serial.print(":");
  Serial.print(mac[1], HEX);
  Serial.print(":");
  Serial.println(mac[0], HEX);
}
void printCurrentNet() {
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());
  byte bssid[6];
  WiFi.BSSID(bssid);
  Serial.print("BSSID: ");
  Serial.print(bssid[5], HEX);
  Serial.print(":");
  Serial.print(bssid[4], HEX);
  Serial.print(":");
  Serial.print(bssid[3], HEX);
  Serial.print(":");
  Serial.print(bssid[2], HEX);
  Serial.print(":");
  Serial.print(bssid[1], HEX);
  Serial.print(":");
  Serial.println(bssid[0], HEX);
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.println(rssi);
  byte encryption = WiFi.encryptionType();
  Serial.print("Encryption Type:");
  Serial.println(encryption, HEX);
  Serial.println();
}

 

WiFi_Email_Example.ino

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×