for TwitterMbed
Fork of GSwifi_old by
GSwifi_smtp.cpp@21:1270827d431a, 2012-11-08 (annotated)
- Committer:
- gsfan
- Date:
- Thu Nov 08 01:35:37 2012 +0000
- Revision:
- 21:1270827d431a
- Parent:
- 20:151b5a4fdd29
fix
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gsfan | 18:4b97804c37d1 | 1 | #include "dbg.h" |
gsfan | 18:4b97804c37d1 | 2 | #include "mbed.h" |
gsfan | 18:4b97804c37d1 | 3 | #include "GSwifi.h" |
gsfan | 18:4b97804c37d1 | 4 | |
gsfan | 20:151b5a4fdd29 | 5 | #ifdef GS_USE_SMTP |
gsfan | 20:151b5a4fdd29 | 6 | |
gsfan | 19:cad912f5a6ba | 7 | int GSwifi::mail (Host &host, const char *to, const char *from, const char *subject, const char *mesg, const char *user, const char *pwd) { |
gsfan | 18:4b97804c37d1 | 8 | int ret = -1; |
gsfan | 18:4b97804c37d1 | 9 | int cid; |
gsfan | 18:4b97804c37d1 | 10 | |
gsfan | 18:4b97804c37d1 | 11 | if (! _connect || _status != GSSTAT_READY) return -1; |
gsfan | 18:4b97804c37d1 | 12 | |
gsfan | 18:4b97804c37d1 | 13 | if (host.getIp().isNull()) { |
gsfan | 18:4b97804c37d1 | 14 | if (getHostByName(host)) { |
gsfan | 18:4b97804c37d1 | 15 | if (getHostByName(host)) return -1; |
gsfan | 18:4b97804c37d1 | 16 | } |
gsfan | 18:4b97804c37d1 | 17 | } |
gsfan | 18:4b97804c37d1 | 18 | if (host.getPort() == 0) { |
gsfan | 18:4b97804c37d1 | 19 | host.setPort(25); |
gsfan | 18:4b97804c37d1 | 20 | } |
gsfan | 18:4b97804c37d1 | 21 | |
gsfan | 18:4b97804c37d1 | 22 | cid = open(host, GSPROT_TCP); |
gsfan | 18:4b97804c37d1 | 23 | if (cid < 0) return -1; |
gsfan | 18:4b97804c37d1 | 24 | DBG("cid %d\r\n", cid); |
gsfan | 18:4b97804c37d1 | 25 | |
gsfan | 18:4b97804c37d1 | 26 | if (wait_smtp(cid ,220)) goto exit; |
gsfan | 18:4b97804c37d1 | 27 | |
gsfan | 18:4b97804c37d1 | 28 | // send request |
gsfan | 18:4b97804c37d1 | 29 | send(cid, "EHLO gainspan\r\n", 15); |
gsfan | 18:4b97804c37d1 | 30 | wait_ms(100); |
gsfan | 18:4b97804c37d1 | 31 | if (wait_smtp(cid ,250)) goto exit; |
gsfan | 18:4b97804c37d1 | 32 | |
gsfan | 18:4b97804c37d1 | 33 | if (user && pwd) { |
gsfan | 18:4b97804c37d1 | 34 | // smtp auth |
gsfan | 18:4b97804c37d1 | 35 | char tmp[80], buf[100]; |
gsfan | 18:4b97804c37d1 | 36 | int len; |
gsfan | 18:4b97804c37d1 | 37 | snprintf(tmp, sizeof(tmp), "%s%c%s%c%s", user, 0, user, 0, pwd); |
gsfan | 18:4b97804c37d1 | 38 | len = strlen(user) * 2 + strlen(pwd) + 2; |
gsfan | 18:4b97804c37d1 | 39 | base64encode(tmp, len, buf, sizeof(buf)); |
gsfan | 18:4b97804c37d1 | 40 | send(cid, "AUTH PLAIN ", 11); |
gsfan | 18:4b97804c37d1 | 41 | send(cid, buf, strlen(buf)); |
gsfan | 18:4b97804c37d1 | 42 | send(cid, "\r\n", 2); |
gsfan | 18:4b97804c37d1 | 43 | if (wait_smtp(cid ,235)) goto quit; |
gsfan | 18:4b97804c37d1 | 44 | } |
gsfan | 18:4b97804c37d1 | 45 | |
gsfan | 18:4b97804c37d1 | 46 | send(cid, "MAIL FROM: ", 11); |
gsfan | 18:4b97804c37d1 | 47 | send(cid, from, strlen(from)); |
gsfan | 18:4b97804c37d1 | 48 | send(cid, "\r\n", 2); |
gsfan | 18:4b97804c37d1 | 49 | if (wait_smtp(cid ,250)) goto quit; |
gsfan | 18:4b97804c37d1 | 50 | |
gsfan | 18:4b97804c37d1 | 51 | send(cid, "RCPT TO: ", 9); |
gsfan | 18:4b97804c37d1 | 52 | send(cid, to, strlen(to)); |
gsfan | 18:4b97804c37d1 | 53 | send(cid, "\r\n", 2); |
gsfan | 18:4b97804c37d1 | 54 | if (wait_smtp(cid ,250)) goto quit; |
gsfan | 18:4b97804c37d1 | 55 | |
gsfan | 18:4b97804c37d1 | 56 | send(cid, "DATA\r\n", 6); |
gsfan | 18:4b97804c37d1 | 57 | if (wait_smtp(cid ,354)) goto quit; |
gsfan | 18:4b97804c37d1 | 58 | |
gsfan | 18:4b97804c37d1 | 59 | // mail data |
gsfan | 18:4b97804c37d1 | 60 | send(cid, mesg, strlen(mesg)); |
gsfan | 18:4b97804c37d1 | 61 | send(cid, "\r\n.\r\n", 5); |
gsfan | 18:4b97804c37d1 | 62 | if (wait_smtp(cid ,250)) goto quit; |
gsfan | 18:4b97804c37d1 | 63 | ret = 0; |
gsfan | 18:4b97804c37d1 | 64 | |
gsfan | 21:1270827d431a | 65 | LOG("From: %s To: %s %d\r\n", from, to, strlen(mesg)); |
gsfan | 21:1270827d431a | 66 | |
gsfan | 18:4b97804c37d1 | 67 | quit: |
gsfan | 18:4b97804c37d1 | 68 | send(cid, "QUIT\r\n", 6); |
gsfan | 18:4b97804c37d1 | 69 | if (wait_smtp(cid ,221)) goto exit; |
gsfan | 18:4b97804c37d1 | 70 | |
gsfan | 18:4b97804c37d1 | 71 | exit: |
gsfan | 18:4b97804c37d1 | 72 | close(cid); |
gsfan | 18:4b97804c37d1 | 73 | return ret; |
gsfan | 18:4b97804c37d1 | 74 | } |
gsfan | 18:4b97804c37d1 | 75 | |
gsfan | 18:4b97804c37d1 | 76 | int GSwifi::wait_smtp (int cid, int code) { |
gsfan | 18:4b97804c37d1 | 77 | Timer timeout; |
gsfan | 18:4b97804c37d1 | 78 | int i, n, len = 0; |
gsfan | 20:151b5a4fdd29 | 79 | char buf[200], data[100]; |
gsfan | 18:4b97804c37d1 | 80 | |
gsfan | 18:4b97804c37d1 | 81 | // wait responce |
gsfan | 18:4b97804c37d1 | 82 | timeout.start(); |
gsfan | 18:4b97804c37d1 | 83 | while (timeout.read_ms() < SMTP_TIMEOUT) { |
gsfan | 18:4b97804c37d1 | 84 | wait_ms(100); |
gsfan | 18:4b97804c37d1 | 85 | poll(); |
gsfan | 18:4b97804c37d1 | 86 | n = recv(cid, buf, sizeof(buf)); |
gsfan | 18:4b97804c37d1 | 87 | for (i = 0; i < n; i ++) { |
gsfan | 18:4b97804c37d1 | 88 | if (buf[i] == '\r') continue; |
gsfan | 18:4b97804c37d1 | 89 | if (buf[i] == '\n') { |
gsfan | 18:4b97804c37d1 | 90 | if (len == 0) continue; |
gsfan | 18:4b97804c37d1 | 91 | goto next; |
gsfan | 18:4b97804c37d1 | 92 | } else |
gsfan | 18:4b97804c37d1 | 93 | if (len < sizeof(data) - 1) { |
gsfan | 18:4b97804c37d1 | 94 | data[len] = buf[i]; |
gsfan | 18:4b97804c37d1 | 95 | len ++; |
gsfan | 18:4b97804c37d1 | 96 | } |
gsfan | 18:4b97804c37d1 | 97 | } |
gsfan | 18:4b97804c37d1 | 98 | } |
gsfan | 18:4b97804c37d1 | 99 | next: |
gsfan | 18:4b97804c37d1 | 100 | data[len] = 0; |
gsfan | 18:4b97804c37d1 | 101 | len = 0; |
gsfan | 18:4b97804c37d1 | 102 | DBG("smtp: %s\r\n", data); |
gsfan | 18:4b97804c37d1 | 103 | timeout.stop(); |
gsfan | 18:4b97804c37d1 | 104 | |
gsfan | 18:4b97804c37d1 | 105 | // check return code |
gsfan | 18:4b97804c37d1 | 106 | i = atoi(data); |
gsfan | 18:4b97804c37d1 | 107 | DBG("smtp status %d\r\n", i); |
gsfan | 18:4b97804c37d1 | 108 | if (i == code) return 0; |
gsfan | 18:4b97804c37d1 | 109 | |
gsfan | 18:4b97804c37d1 | 110 | return -1; |
gsfan | 18:4b97804c37d1 | 111 | } |
gsfan | 20:151b5a4fdd29 | 112 | |
gsfan | 20:151b5a4fdd29 | 113 | #endif |