GainSpan Wi-Fi library see: http://mbed.org/users/gsfan/notebook/gainspan_wifi/

Fork of GSwifi_old by gs fan

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers GSwifi_smtp.cpp Source File

GSwifi_smtp.cpp

00001 #include "dbg.h"
00002 #include "mbed.h"
00003 #include "GSwifi.h"
00004 
00005 #ifdef GS_USE_SMTP
00006 
00007 int GSwifi::mail (Host &host, const char *to, const char *from, const char *subject, const char *mesg, const char *user, const char *pwd) {
00008     int ret = -1;
00009     int cid;
00010 
00011     if (! _connect || _status != GSSTAT_READY) return -1;
00012 
00013     if (host.getIp().isNull()) {
00014         if (getHostByName(host)) {
00015             if (getHostByName(host)) return -1;
00016         }
00017     }
00018     if (host.getPort() == 0) {
00019         host.setPort(25);
00020     }
00021 
00022     cid = open(host, GSPROT_TCP);
00023     if (cid < 0) return -1;
00024     DBG("cid %d\r\n", cid);
00025 
00026     if (wait_smtp(cid ,220)) goto exit;
00027 
00028     // send request
00029     send(cid, "EHLO gainspan\r\n", 15);
00030     wait_ms(100);
00031     if (wait_smtp(cid ,250)) goto exit;
00032 
00033     if (user && pwd) {
00034         // smtp auth
00035         char tmp[80], buf[100];
00036         int len;
00037         snprintf(tmp, sizeof(tmp), "%s%c%s%c%s", user, 0, user, 0, pwd);
00038         len = strlen(user) * 2 + strlen(pwd) + 2;
00039         base64encode(tmp, len, buf, sizeof(buf));
00040         send(cid, "AUTH PLAIN ", 11);
00041         send(cid, buf, strlen(buf));
00042         send(cid, "\r\n", 2);
00043         if (wait_smtp(cid ,235)) goto quit;
00044     }
00045  
00046     send(cid, "MAIL FROM: ", 11);
00047     send(cid, from, strlen(from));
00048     send(cid, "\r\n", 2);
00049     if (wait_smtp(cid ,250)) goto quit;
00050  
00051     send(cid, "RCPT TO: ", 9);
00052     send(cid, to, strlen(to));
00053     send(cid, "\r\n", 2);
00054     if (wait_smtp(cid ,250)) goto quit;
00055  
00056     send(cid, "DATA\r\n", 6);
00057     if (wait_smtp(cid ,354)) goto quit;
00058  
00059     // mail data
00060     send(cid, mesg, strlen(mesg));
00061     send(cid, "\r\n.\r\n", 5);
00062     if (wait_smtp(cid ,250)) goto quit;
00063     ret = 0;
00064  
00065     LOG("From: %s To: %s %d\r\n", from, to, strlen(mesg));
00066 
00067 quit:
00068     send(cid, "QUIT\r\n", 6);
00069     if (wait_smtp(cid ,221)) goto exit;
00070  
00071 exit:
00072     close(cid);
00073     return ret;
00074 }
00075 
00076 int GSwifi::wait_smtp (int cid, int code) {
00077     Timer timeout;
00078     int i, n, len = 0;
00079     char buf[200], data[100];
00080 
00081     // wait responce
00082     timeout.start();
00083     while (timeout.read_ms() < SMTP_TIMEOUT) {
00084         wait_ms(100);
00085         poll();
00086         n = recv(cid, buf, sizeof(buf));
00087         for (i = 0; i < n; i ++) {
00088             if (buf[i] == '\r') continue;
00089             if (buf[i] == '\n') {
00090                 if (len == 0) continue;
00091                 goto next;
00092             } else
00093             if (len < sizeof(data) - 1) {
00094                 data[len] = buf[i];
00095                 len ++;
00096             }
00097         }
00098     }
00099 next:
00100     data[len] = 0;
00101     len = 0;
00102     DBG("smtp: %s\r\n", data);
00103     timeout.stop();
00104  
00105     // check return code
00106     i = atoi(data);
00107     DBG("smtp status %d\r\n", i);
00108     if (i == code) return 0;
00109  
00110     return -1;
00111 }
00112 
00113 #endif