Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
OLD
GSwifi_smtp.cpp@18:4b97804c37d1, 2012-11-01 (annotated)
- Committer:
- gsfan
- Date:
- Thu Nov 01 02:55:57 2012 +0000
- Revision:
- 18:4b97804c37d1
- Child:
- 19:cad912f5a6ba
support smtp (send mail) and http server (httpd)
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 | 18:4b97804c37d1 | 5 | int GSwifi::mail (Host &host, char *to, char *from, char *subject, char *mesg, char *user, char *pwd) { |
| gsfan | 18:4b97804c37d1 | 6 | int ret = -1; |
| gsfan | 18:4b97804c37d1 | 7 | int cid; |
| gsfan | 18:4b97804c37d1 | 8 | |
| gsfan | 18:4b97804c37d1 | 9 | if (! _connect || _status != GSSTAT_READY) return -1; |
| gsfan | 18:4b97804c37d1 | 10 | |
| gsfan | 18:4b97804c37d1 | 11 | if (host.getIp().isNull()) { |
| gsfan | 18:4b97804c37d1 | 12 | if (getHostByName(host)) { |
| gsfan | 18:4b97804c37d1 | 13 | if (getHostByName(host)) return -1; |
| gsfan | 18:4b97804c37d1 | 14 | } |
| gsfan | 18:4b97804c37d1 | 15 | } |
| gsfan | 18:4b97804c37d1 | 16 | if (host.getPort() == 0) { |
| gsfan | 18:4b97804c37d1 | 17 | host.setPort(25); |
| gsfan | 18:4b97804c37d1 | 18 | } |
| gsfan | 18:4b97804c37d1 | 19 | |
| gsfan | 18:4b97804c37d1 | 20 | cid = open(host, GSPROT_TCP); |
| gsfan | 18:4b97804c37d1 | 21 | if (cid < 0) return -1; |
| gsfan | 18:4b97804c37d1 | 22 | DBG("cid %d\r\n", cid); |
| gsfan | 18:4b97804c37d1 | 23 | |
| gsfan | 18:4b97804c37d1 | 24 | if (wait_smtp(cid ,220)) goto exit; |
| gsfan | 18:4b97804c37d1 | 25 | |
| gsfan | 18:4b97804c37d1 | 26 | // send request |
| gsfan | 18:4b97804c37d1 | 27 | send(cid, "EHLO gainspan\r\n", 15); |
| gsfan | 18:4b97804c37d1 | 28 | wait_ms(100); |
| gsfan | 18:4b97804c37d1 | 29 | if (wait_smtp(cid ,250)) goto exit; |
| gsfan | 18:4b97804c37d1 | 30 | |
| gsfan | 18:4b97804c37d1 | 31 | if (user && pwd) { |
| gsfan | 18:4b97804c37d1 | 32 | // smtp auth |
| gsfan | 18:4b97804c37d1 | 33 | char tmp[80], buf[100]; |
| gsfan | 18:4b97804c37d1 | 34 | int len; |
| gsfan | 18:4b97804c37d1 | 35 | snprintf(tmp, sizeof(tmp), "%s%c%s%c%s", user, 0, user, 0, pwd); |
| gsfan | 18:4b97804c37d1 | 36 | len = strlen(user) * 2 + strlen(pwd) + 2; |
| gsfan | 18:4b97804c37d1 | 37 | base64encode(tmp, len, buf, sizeof(buf)); |
| gsfan | 18:4b97804c37d1 | 38 | send(cid, "AUTH PLAIN ", 11); |
| gsfan | 18:4b97804c37d1 | 39 | send(cid, buf, strlen(buf)); |
| gsfan | 18:4b97804c37d1 | 40 | send(cid, "\r\n", 2); |
| gsfan | 18:4b97804c37d1 | 41 | if (wait_smtp(cid ,235)) goto quit; |
| gsfan | 18:4b97804c37d1 | 42 | } |
| gsfan | 18:4b97804c37d1 | 43 | |
| gsfan | 18:4b97804c37d1 | 44 | send(cid, "MAIL FROM: ", 11); |
| gsfan | 18:4b97804c37d1 | 45 | send(cid, from, strlen(from)); |
| gsfan | 18:4b97804c37d1 | 46 | send(cid, "\r\n", 2); |
| gsfan | 18:4b97804c37d1 | 47 | if (wait_smtp(cid ,250)) goto quit; |
| gsfan | 18:4b97804c37d1 | 48 | |
| gsfan | 18:4b97804c37d1 | 49 | send(cid, "RCPT TO: ", 9); |
| gsfan | 18:4b97804c37d1 | 50 | send(cid, to, strlen(to)); |
| gsfan | 18:4b97804c37d1 | 51 | send(cid, "\r\n", 2); |
| gsfan | 18:4b97804c37d1 | 52 | if (wait_smtp(cid ,250)) goto quit; |
| gsfan | 18:4b97804c37d1 | 53 | |
| gsfan | 18:4b97804c37d1 | 54 | send(cid, "DATA\r\n", 6); |
| gsfan | 18:4b97804c37d1 | 55 | if (wait_smtp(cid ,354)) goto quit; |
| gsfan | 18:4b97804c37d1 | 56 | |
| gsfan | 18:4b97804c37d1 | 57 | // mail data |
| gsfan | 18:4b97804c37d1 | 58 | send(cid, mesg, strlen(mesg)); |
| gsfan | 18:4b97804c37d1 | 59 | send(cid, "\r\n.\r\n", 5); |
| gsfan | 18:4b97804c37d1 | 60 | if (wait_smtp(cid ,250)) goto quit; |
| gsfan | 18:4b97804c37d1 | 61 | ret = 0; |
| gsfan | 18:4b97804c37d1 | 62 | |
| gsfan | 18:4b97804c37d1 | 63 | quit: |
| gsfan | 18:4b97804c37d1 | 64 | send(cid, "QUIT\r\n", 6); |
| gsfan | 18:4b97804c37d1 | 65 | if (wait_smtp(cid ,221)) goto exit; |
| gsfan | 18:4b97804c37d1 | 66 | |
| gsfan | 18:4b97804c37d1 | 67 | exit: |
| gsfan | 18:4b97804c37d1 | 68 | close(cid); |
| gsfan | 18:4b97804c37d1 | 69 | return ret; |
| gsfan | 18:4b97804c37d1 | 70 | } |
| gsfan | 18:4b97804c37d1 | 71 | |
| gsfan | 18:4b97804c37d1 | 72 | int GSwifi::wait_smtp (int cid, int code) { |
| gsfan | 18:4b97804c37d1 | 73 | Timer timeout; |
| gsfan | 18:4b97804c37d1 | 74 | int i, n, len = 0; |
| gsfan | 18:4b97804c37d1 | 75 | char buf[500], data[100]; |
| gsfan | 18:4b97804c37d1 | 76 | |
| gsfan | 18:4b97804c37d1 | 77 | // wait responce |
| gsfan | 18:4b97804c37d1 | 78 | timeout.start(); |
| gsfan | 18:4b97804c37d1 | 79 | while (timeout.read_ms() < SMTP_TIMEOUT) { |
| gsfan | 18:4b97804c37d1 | 80 | wait_ms(100); |
| gsfan | 18:4b97804c37d1 | 81 | poll(); |
| gsfan | 18:4b97804c37d1 | 82 | n = recv(cid, buf, sizeof(buf)); |
| gsfan | 18:4b97804c37d1 | 83 | for (i = 0; i < n; i ++) { |
| gsfan | 18:4b97804c37d1 | 84 | if (buf[i] == '\r') continue; |
| gsfan | 18:4b97804c37d1 | 85 | if (buf[i] == '\n') { |
| gsfan | 18:4b97804c37d1 | 86 | if (len == 0) continue; |
| gsfan | 18:4b97804c37d1 | 87 | goto next; |
| gsfan | 18:4b97804c37d1 | 88 | } else |
| gsfan | 18:4b97804c37d1 | 89 | if (len < sizeof(data) - 1) { |
| gsfan | 18:4b97804c37d1 | 90 | data[len] = buf[i]; |
| gsfan | 18:4b97804c37d1 | 91 | len ++; |
| gsfan | 18:4b97804c37d1 | 92 | } |
| gsfan | 18:4b97804c37d1 | 93 | } |
| gsfan | 18:4b97804c37d1 | 94 | } |
| gsfan | 18:4b97804c37d1 | 95 | next: |
| gsfan | 18:4b97804c37d1 | 96 | data[len] = 0; |
| gsfan | 18:4b97804c37d1 | 97 | len = 0; |
| gsfan | 18:4b97804c37d1 | 98 | DBG("smtp: %s\r\n", data); |
| gsfan | 18:4b97804c37d1 | 99 | timeout.stop(); |
| gsfan | 18:4b97804c37d1 | 100 | |
| gsfan | 18:4b97804c37d1 | 101 | // check return code |
| gsfan | 18:4b97804c37d1 | 102 | i = atoi(data); |
| gsfan | 18:4b97804c37d1 | 103 | DBG("smtp status %d\r\n", i); |
| gsfan | 18:4b97804c37d1 | 104 | if (i == code) return 0; |
| gsfan | 18:4b97804c37d1 | 105 | |
| gsfan | 18:4b97804c37d1 | 106 | return -1; |
| gsfan | 18:4b97804c37d1 | 107 | } |