Skip to content

Instantly share code, notes, and snippets.

@technobly
Forked from dmiddlecamp/emailDemo.cpp
Created February 12, 2014 06:12
Show Gist options
  • Save technobly/8950800 to your computer and use it in GitHub Desktop.
Save technobly/8950800 to your computer and use it in GitHub Desktop.
#define SMTP_SERVER "smtp.yourserver.com"
#define SMTP_USER_BASE64 "base64_encode_your_user"
#define SMTP_PASS_BASE64 "base64_encode_your_pass"
#define SMTP_FROM_EMAIL "email@from.com"
#define SMTP_TO_EMAIL "email@to.com"
#define SMTP_SUBJECT "Email from a Core!"
#define SMTP_BODY "Body body body"
#include "application.h"
#include <string.h>
//void idle();
//int SendEmail(char *smtpServer, char *username, char *password, char *fromEmail, char *toEmail, char *subject, char *body);
//int handshake(char *smtpServer);
//int authenticate(char *username, char *password);
//void echoSocketWrite(TCPClient src, const char *line) ;
//void echoSocketWrite(TCPClient src, String line);
//void flushToSerial(TCPClient src);
//int blockForResponse(char *response, unsigned int maxWait);
TCPClient client;
int sentOnce = 0;
void setup() {
pinMode(D0, INPUT_PULLDOWN);
Spark.variable("emailSent", &sentOnce, INT);
Serial.begin(9600);
}
void loop() {
if (digitalRead(D0) == HIGH) {
if (sentOnce == 0) {
RGB.control(true);
RGB.color(250, 0,0);
sentOnce = SendEmail(SMTP_SERVER, SMTP_USER_BASE64, SMTP_PASS_BASE64, SMTP_FROM_EMAIL, SMTP_TO_EMAIL, SMTP_SUBJECT, SMTP_BODY);
if (sentOnce) {
RGB.color(0, 250, 0);
}
RGB.control(false);
}
}
}
void idle() {
SPARK_WLAN_Loop();
}
/**
* Thank you @jboswell !
*/
int SendEmail(char *smtpServer, char *username, char *password, char *fromEmail, char *toEmail, char *subject, char *body) {
if (!client.connect(smtpServer, 25)) {
return 0;
}
Serial.write("Connected!\n");
if (!handshake(smtpServer)) {
Serial.write("handshake failed...");
client.stop();
return 0;
}
if (!authenticate(username, password)) {
Serial.write("auth failed...");
client.stop();
return 0;
}
Serial.write("sending mail...\n");
echoSocketWrite(client, String("MAIL FROM:<") + String(fromEmail) + String(">") + String("\r\n"));
echoSocketWrite(client, String("RCPT TO:<") + String(toEmail) + String(">") + String("\r\n"));
flushToSerial(client); //client.flush();
echoSocketWrite(client, "DATA\r\n");
echoSocketWrite(client, String("from: ") + String(fromEmail) + String("\r\n"));
echoSocketWrite(client, String("to: ") + String(toEmail) + String("\r\n"));
echoSocketWrite(client, String("subject: ") + String(subject) + String("\r\n"));
flushToSerial(client); //client.flush();
echoSocketWrite(client, "\r\n");
echoSocketWrite(client, body);
echoSocketWrite(client, "\r\n");
echoSocketWrite(client, ".");
echoSocketWrite(client, "\r\n");
echoSocketWrite(client, "\r\n");
flushToSerial(client); //client.flush();
if (blockForResponse("250", 2500) == 1) {
Serial.write("Email sent.\n");
}
echoSocketWrite(client, "quit\r\n");
echoSocketWrite(client, "\r\n");
if (blockForResponse("220", 2500) == 1) {
Serial.write("Said goodbye.\n");
}
flushToSerial(client); //client.flush();
client.stop();
delay(100);
}
int handshake(char *smtpServer) {
//wait for the server to say something.
flushToSerial(client); //client.flush();
Serial.write("waiting for the server to say hello...\n");
if (blockForResponse("220", 5000) == 0) {
//handshake failed.
return 0;
}
idle();
Serial.write("Saying hello back.\n");
echoSocketWrite(client, String("EHLO ") + String(smtpServer) + String("\r\n"));
if (blockForResponse("250", 1250) == 0) {
//handshake failed.
return 0;
}
idle();
return 1;
}
int authenticate(char *username, char *password) {
Serial.write("logging in...\n");
echoSocketWrite(client, "AUTH LOGIN\r\n");
blockForResponse("334", 5000);
echoSocketWrite(client, String(username) + String("\r\n"));
blockForResponse("334", 5000);
echoSocketWrite(client, String(password) + String("\r\n"));
blockForResponse("235", 5000);
idle();
return 1;
}
void echoSocketWrite(TCPClient src, const char *line) {
Serial.write(line);
src.print(line);
}
void echoSocketWrite(TCPClient src, String line) {
flushToSerial(src);
delay(10);
Serial.write(line.c_str());
src.print(line.c_str());
}
void flushToSerial(TCPClient src) {
while (src.available()) {
Serial.write(src.read());
}
}
/* really basic / error prone way of listening for and parsing results */
int blockForResponse(char *response, unsigned int maxWait) {
unsigned int startTime = millis();
int idx = 0;
char buffer[16];
while ((millis() - startTime) < maxWait) {
while (client.available()) {
buffer[idx] = client.read();
Serial.write(buffer[idx]);
idx++;
if (idx > 16) {
idx = 0;
}
if (strstr(buffer, response) != NULL) {
Serial.write("heard response.\n");
return 1;
}
}
delay(100);
}
Serial.write("timed out.\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment