SMS AND EMAIL WORKING
Dependents: Application-SimpleSMTPClient_HelloWorld final
Fork of SimpleSMTPClient by
SimpleSMTPClient.cpp@0:3ea21ce21fe1, 2012-12-03 (annotated)
- Committer:
- sunifu
- Date:
- Mon Dec 03 09:55:59 2012 +0000
- Revision:
- 0:3ea21ce21fe1
- Child:
- 1:58550f022776
- Child:
- 3:67a97516c315
Ver1.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sunifu | 0:3ea21ce21fe1 | 1 | /* |
sunifu | 0:3ea21ce21fe1 | 2 | * |
sunifu | 0:3ea21ce21fe1 | 3 | * mbed Simple SMTP Client |
sunifu | 0:3ea21ce21fe1 | 4 | * Copyright (c) 2012 Tadao Iida |
sunifu | 0:3ea21ce21fe1 | 5 | * Released under the MIT License: http://mbed.org/license/mit |
sunifu | 0:3ea21ce21fe1 | 6 | */ |
sunifu | 0:3ea21ce21fe1 | 7 | |
sunifu | 0:3ea21ce21fe1 | 8 | //Debug is disabled by default |
sunifu | 0:3ea21ce21fe1 | 9 | #if 0 |
sunifu | 0:3ea21ce21fe1 | 10 | //Enable debug |
sunifu | 0:3ea21ce21fe1 | 11 | #define __DEBUG__ |
sunifu | 0:3ea21ce21fe1 | 12 | #include <cstdio> |
sunifu | 0:3ea21ce21fe1 | 13 | #define DBG(x, ...) std::printf("[NTPClient : DBG]"x"\r\n", ##__VA_ARGS__); |
sunifu | 0:3ea21ce21fe1 | 14 | |
sunifu | 0:3ea21ce21fe1 | 15 | #else |
sunifu | 0:3ea21ce21fe1 | 16 | //Disable debug |
sunifu | 0:3ea21ce21fe1 | 17 | #define DBG(x, ...) |
sunifu | 0:3ea21ce21fe1 | 18 | |
sunifu | 0:3ea21ce21fe1 | 19 | #endif |
sunifu | 0:3ea21ce21fe1 | 20 | |
sunifu | 0:3ea21ce21fe1 | 21 | /** @file |
sunifu | 0:3ea21ce21fe1 | 22 | * @brief Simple SMTP Client |
sunifu | 0:3ea21ce21fe1 | 23 | */ |
sunifu | 0:3ea21ce21fe1 | 24 | |
sunifu | 0:3ea21ce21fe1 | 25 | #include "mbed.h" |
sunifu | 0:3ea21ce21fe1 | 26 | #include "EthernetInterface.h" |
sunifu | 0:3ea21ce21fe1 | 27 | #include "SimpleSMTPClient.h" |
sunifu | 0:3ea21ce21fe1 | 28 | |
sunifu | 0:3ea21ce21fe1 | 29 | SimpleSMTPClient::SimpleSMTPClient() : smtp() |
sunifu | 0:3ea21ce21fe1 | 30 | { |
sunifu | 0:3ea21ce21fe1 | 31 | |
sunifu | 0:3ea21ce21fe1 | 32 | |
sunifu | 0:3ea21ce21fe1 | 33 | } |
sunifu | 0:3ea21ce21fe1 | 34 | |
sunifu | 0:3ea21ce21fe1 | 35 | |
sunifu | 0:3ea21ce21fe1 | 36 | // Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
sunifu | 0:3ea21ce21fe1 | 37 | int SimpleSMTPClient::base64enc(const char *input, unsigned int length, char *output, int outputlen) { |
sunifu | 0:3ea21ce21fe1 | 38 | static const char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
sunifu | 0:3ea21ce21fe1 | 39 | unsigned int c, c1, c2, c3; |
sunifu | 0:3ea21ce21fe1 | 40 | |
sunifu | 0:3ea21ce21fe1 | 41 | if (outputlen < (((length-1)/3)+1)<<2) return -1; |
sunifu | 0:3ea21ce21fe1 | 42 | |
sunifu | 0:3ea21ce21fe1 | 43 | for(unsigned int i = 0, j = 0; i<length; i+=3,j+=4) { |
sunifu | 0:3ea21ce21fe1 | 44 | c1 = ((((unsigned char)*((unsigned char *)&input[i])))); |
sunifu | 0:3ea21ce21fe1 | 45 | c2 = (length>i+1)?((((unsigned char)*((unsigned char *)&input[i+1])))):0; |
sunifu | 0:3ea21ce21fe1 | 46 | c3 = (length>i+2)?((((unsigned char)*((unsigned char *)&input[i+2])))):0; |
sunifu | 0:3ea21ce21fe1 | 47 | |
sunifu | 0:3ea21ce21fe1 | 48 | c = ((c1 & 0xFC) >> 2); |
sunifu | 0:3ea21ce21fe1 | 49 | output[j+0] = base64[c]; |
sunifu | 0:3ea21ce21fe1 | 50 | c = ((c1 & 0x03) << 4) | ((c2 & 0xF0) >> 4); |
sunifu | 0:3ea21ce21fe1 | 51 | output[j+1] = base64[c]; |
sunifu | 0:3ea21ce21fe1 | 52 | c = ((c2 & 0x0F) << 2) | ((c3 & 0xC0) >> 6); |
sunifu | 0:3ea21ce21fe1 | 53 | output[j+2] = (length>i+1)?base64[c]:'='; |
sunifu | 0:3ea21ce21fe1 | 54 | c = (c3 & 0x3F); |
sunifu | 0:3ea21ce21fe1 | 55 | output[j+3] = (length>i+2)?base64[c]:'='; |
sunifu | 0:3ea21ce21fe1 | 56 | } |
sunifu | 0:3ea21ce21fe1 | 57 | output[(((length-1)/3)+1)<<2] = '\0'; |
sunifu | 0:3ea21ce21fe1 | 58 | return 0; |
sunifu | 0:3ea21ce21fe1 | 59 | } |
sunifu | 0:3ea21ce21fe1 | 60 | |
sunifu | 0:3ea21ce21fe1 | 61 | // SMTP ReceiveMessage |
sunifu | 0:3ea21ce21fe1 | 62 | int SimpleSMTPClient::receiveMessage(int code) { |
sunifu | 0:3ea21ce21fe1 | 63 | int i; |
sunifu | 0:3ea21ce21fe1 | 64 | char buf[1500]; |
sunifu | 0:3ea21ce21fe1 | 65 | |
sunifu | 0:3ea21ce21fe1 | 66 | do{ |
sunifu | 0:3ea21ce21fe1 | 67 | i = smtp.receive(buf, sizeof(buf)); |
sunifu | 0:3ea21ce21fe1 | 68 | buf[i]='\0'; |
sunifu | 0:3ea21ce21fe1 | 69 | printf("Recv>>%s[%d] \r\n",buf,i); |
sunifu | 0:3ea21ce21fe1 | 70 | }while(atoi(buf) != code && i > 0 ); |
sunifu | 0:3ea21ce21fe1 | 71 | |
sunifu | 0:3ea21ce21fe1 | 72 | // Check return code |
sunifu | 0:3ea21ce21fe1 | 73 | if (i < 0 ) |
sunifu | 0:3ea21ce21fe1 | 74 | return -1; |
sunifu | 0:3ea21ce21fe1 | 75 | |
sunifu | 0:3ea21ce21fe1 | 76 | return 0; |
sunifu | 0:3ea21ce21fe1 | 77 | } |
sunifu | 0:3ea21ce21fe1 | 78 | |
sunifu | 0:3ea21ce21fe1 | 79 | int SimpleSMTPClient::sendmail (char *host, char *user, char *pwd, char *port,SMTPAuth auth) { |
sunifu | 0:3ea21ce21fe1 | 80 | |
sunifu | 0:3ea21ce21fe1 | 81 | int ret = -1; |
sunifu | 0:3ea21ce21fe1 | 82 | |
sunifu | 0:3ea21ce21fe1 | 83 | smtp.set_blocking(false, 1500); |
sunifu | 0:3ea21ce21fe1 | 84 | smtp.connect(host, atoi(port)) ; |
sunifu | 0:3ea21ce21fe1 | 85 | if ( !smtp.is_connected() ){ |
sunifu | 0:3ea21ce21fe1 | 86 | printf("Connection Error!\r\n"); |
sunifu | 0:3ea21ce21fe1 | 87 | smtp.close(); |
sunifu | 0:3ea21ce21fe1 | 88 | return -1; |
sunifu | 0:3ea21ce21fe1 | 89 | } |
sunifu | 0:3ea21ce21fe1 | 90 | |
sunifu | 0:3ea21ce21fe1 | 91 | // send request |
sunifu | 0:3ea21ce21fe1 | 92 | wait_ms(100); |
sunifu | 0:3ea21ce21fe1 | 93 | smtp.send("EHLO mbed\r\n", 11); |
sunifu | 0:3ea21ce21fe1 | 94 | if (receiveMessage(250)){ |
sunifu | 0:3ea21ce21fe1 | 95 | smtp.close(); |
sunifu | 0:3ea21ce21fe1 | 96 | return -1; |
sunifu | 0:3ea21ce21fe1 | 97 | } |
sunifu | 0:3ea21ce21fe1 | 98 | |
sunifu | 0:3ea21ce21fe1 | 99 | if ( auth == SMTP_AUTH_PLAIN ){ |
sunifu | 0:3ea21ce21fe1 | 100 | if ( user && pwd ) { |
sunifu | 0:3ea21ce21fe1 | 101 | // SMTP auth |
sunifu | 0:3ea21ce21fe1 | 102 | char tmp[80], buf[100]; |
sunifu | 0:3ea21ce21fe1 | 103 | int len; |
sunifu | 0:3ea21ce21fe1 | 104 | snprintf(tmp, sizeof(tmp), "%s%c%s%c%s",user, 0, user, 0, pwd); |
sunifu | 0:3ea21ce21fe1 | 105 | len = strlen(user)*2 + strlen(pwd) + 2; |
sunifu | 0:3ea21ce21fe1 | 106 | base64enc(tmp, len, buf, sizeof(buf)); |
sunifu | 0:3ea21ce21fe1 | 107 | smtp.send("AUTH PLAIN ", 11); |
sunifu | 0:3ea21ce21fe1 | 108 | smtp.send(buf, strlen(buf)); |
sunifu | 0:3ea21ce21fe1 | 109 | smtp.send("\r\n", 2); |
sunifu | 0:3ea21ce21fe1 | 110 | #ifdef __DEBUG__ |
sunifu | 0:3ea21ce21fe1 | 111 | DBG("base64[%s]\r\n", buf); |
sunifu | 0:3ea21ce21fe1 | 112 | #endif |
sunifu | 0:3ea21ce21fe1 | 113 | if (receiveMessage(235)){ |
sunifu | 0:3ea21ce21fe1 | 114 | smtp.send("QUIT\r\n", 6); |
sunifu | 0:3ea21ce21fe1 | 115 | smtp.close(); |
sunifu | 0:3ea21ce21fe1 | 116 | } |
sunifu | 0:3ea21ce21fe1 | 117 | } |
sunifu | 0:3ea21ce21fe1 | 118 | } |
sunifu | 0:3ea21ce21fe1 | 119 | |
sunifu | 0:3ea21ce21fe1 | 120 | smtp.send("MAIL FROM: ", 11); |
sunifu | 0:3ea21ce21fe1 | 121 | smtp.send(getFromAddress(), strlen(getFromAddress())); |
sunifu | 0:3ea21ce21fe1 | 122 | smtp.send("\r\n", 2); |
sunifu | 0:3ea21ce21fe1 | 123 | if (receiveMessage(250)) { |
sunifu | 0:3ea21ce21fe1 | 124 | smtp.close(); |
sunifu | 0:3ea21ce21fe1 | 125 | return -1; |
sunifu | 0:3ea21ce21fe1 | 126 | } |
sunifu | 0:3ea21ce21fe1 | 127 | char tmp[128],addr[128]; |
sunifu | 0:3ea21ce21fe1 | 128 | int i = 0; |
sunifu | 0:3ea21ce21fe1 | 129 | strcpy(tmp,getToAddress()); |
sunifu | 0:3ea21ce21fe1 | 130 | |
sunifu | 0:3ea21ce21fe1 | 131 | while( tmp[i] != '\0' ){ |
sunifu | 0:3ea21ce21fe1 | 132 | int j = 0; |
sunifu | 0:3ea21ce21fe1 | 133 | while( tmp[i] != ',' ){ |
sunifu | 0:3ea21ce21fe1 | 134 | addr[j] = tmp[i]; |
sunifu | 0:3ea21ce21fe1 | 135 | |
sunifu | 0:3ea21ce21fe1 | 136 | if( tmp[i] == '\0' ) break; |
sunifu | 0:3ea21ce21fe1 | 137 | j++; |
sunifu | 0:3ea21ce21fe1 | 138 | i++; |
sunifu | 0:3ea21ce21fe1 | 139 | } |
sunifu | 0:3ea21ce21fe1 | 140 | addr[j]='\0'; |
sunifu | 0:3ea21ce21fe1 | 141 | |
sunifu | 0:3ea21ce21fe1 | 142 | #ifdef __DEBUG__ |
sunifu | 0:3ea21ce21fe1 | 143 | DBG("ToAddress[%s]\r\n", addr); |
sunifu | 0:3ea21ce21fe1 | 144 | #endif |
sunifu | 0:3ea21ce21fe1 | 145 | |
sunifu | 0:3ea21ce21fe1 | 146 | smtp.send("RCPT TO: ", 9); |
sunifu | 0:3ea21ce21fe1 | 147 | smtp.send(addr, strlen(addr)); |
sunifu | 0:3ea21ce21fe1 | 148 | smtp.send("\r\n", 2); |
sunifu | 0:3ea21ce21fe1 | 149 | if(tmp[i]==',') i++; |
sunifu | 0:3ea21ce21fe1 | 150 | |
sunifu | 0:3ea21ce21fe1 | 151 | if (receiveMessage(250)) goto exit; |
sunifu | 0:3ea21ce21fe1 | 152 | } |
sunifu | 0:3ea21ce21fe1 | 153 | ret = makeHeader(); |
sunifu | 0:3ea21ce21fe1 | 154 | if ( ret == -1 ) { |
sunifu | 0:3ea21ce21fe1 | 155 | printf("The headers are less than 256 characters.\r\n"); |
sunifu | 0:3ea21ce21fe1 | 156 | goto exit ; |
sunifu | 0:3ea21ce21fe1 | 157 | } |
sunifu | 0:3ea21ce21fe1 | 158 | |
sunifu | 0:3ea21ce21fe1 | 159 | // mail Body |
sunifu | 0:3ea21ce21fe1 | 160 | smtp.send("DATA\r\n", 6); |
sunifu | 0:3ea21ce21fe1 | 161 | smtp.send(getHeader(), strlen(getHeader())); |
sunifu | 0:3ea21ce21fe1 | 162 | smtp.send(getMessage(), strlen(getMessage())); |
sunifu | 0:3ea21ce21fe1 | 163 | if (receiveMessage(354)) goto exit; |
sunifu | 0:3ea21ce21fe1 | 164 | |
sunifu | 0:3ea21ce21fe1 | 165 | smtp.send("\r\n.\r\n", 5); |
sunifu | 0:3ea21ce21fe1 | 166 | if (receiveMessage(250)) goto exit; |
sunifu | 0:3ea21ce21fe1 | 167 | #ifdef __DEBUG__ |
sunifu | 0:3ea21ce21fe1 | 168 | DBG("Header %s\r\n", getHeader()); |
sunifu | 0:3ea21ce21fe1 | 169 | DBG("Message %s\r\n", getMessage()); |
sunifu | 0:3ea21ce21fe1 | 170 | #endif |
sunifu | 0:3ea21ce21fe1 | 171 | |
sunifu | 0:3ea21ce21fe1 | 172 | ret = 0; |
sunifu | 0:3ea21ce21fe1 | 173 | |
sunifu | 0:3ea21ce21fe1 | 174 | smtp.send("QUIT\r\n", 6); |
sunifu | 0:3ea21ce21fe1 | 175 | if (receiveMessage(221)) goto exit; |
sunifu | 0:3ea21ce21fe1 | 176 | |
sunifu | 0:3ea21ce21fe1 | 177 | exit: |
sunifu | 0:3ea21ce21fe1 | 178 | smtp.close(); |
sunifu | 0:3ea21ce21fe1 | 179 | |
sunifu | 0:3ea21ce21fe1 | 180 | return ret; |
sunifu | 0:3ea21ce21fe1 | 181 | } |
sunifu | 0:3ea21ce21fe1 | 182 | |
sunifu | 0:3ea21ce21fe1 | 183 | int SimpleSMTPClient::setMessage(char *sub,char *msg) |
sunifu | 0:3ea21ce21fe1 | 184 | { |
sunifu | 0:3ea21ce21fe1 | 185 | int n; |
sunifu | 0:3ea21ce21fe1 | 186 | n = strlen(sub); |
sunifu | 0:3ea21ce21fe1 | 187 | if( n >= 64 ) |
sunifu | 0:3ea21ce21fe1 | 188 | return -1; |
sunifu | 0:3ea21ce21fe1 | 189 | n = strlen(msg); |
sunifu | 0:3ea21ce21fe1 | 190 | if( n >= 1244 ) |
sunifu | 0:3ea21ce21fe1 | 191 | return -1; |
sunifu | 0:3ea21ce21fe1 | 192 | strcpy(subject,sub); |
sunifu | 0:3ea21ce21fe1 | 193 | strcpy(message,msg); |
sunifu | 0:3ea21ce21fe1 | 194 | return 0; |
sunifu | 0:3ea21ce21fe1 | 195 | } |
sunifu | 0:3ea21ce21fe1 | 196 | |
sunifu | 0:3ea21ce21fe1 | 197 | int SimpleSMTPClient::addMessage(char *buf) |
sunifu | 0:3ea21ce21fe1 | 198 | { |
sunifu | 0:3ea21ce21fe1 | 199 | int n; |
sunifu | 0:3ea21ce21fe1 | 200 | n = strlen(message); |
sunifu | 0:3ea21ce21fe1 | 201 | n = n+strlen(buf); |
sunifu | 0:3ea21ce21fe1 | 202 | if( n >= 1244 ) |
sunifu | 0:3ea21ce21fe1 | 203 | return -1; |
sunifu | 0:3ea21ce21fe1 | 204 | strcat(message,buf); |
sunifu | 0:3ea21ce21fe1 | 205 | return 0; |
sunifu | 0:3ea21ce21fe1 | 206 | } |
sunifu | 0:3ea21ce21fe1 | 207 | |
sunifu | 0:3ea21ce21fe1 | 208 | int SimpleSMTPClient::setFromAddress(char *fromaddr) |
sunifu | 0:3ea21ce21fe1 | 209 | { |
sunifu | 0:3ea21ce21fe1 | 210 | int n; |
sunifu | 0:3ea21ce21fe1 | 211 | n = strlen(fromaddr); |
sunifu | 0:3ea21ce21fe1 | 212 | if( n <= 0 ) |
sunifu | 0:3ea21ce21fe1 | 213 | return -1; |
sunifu | 0:3ea21ce21fe1 | 214 | strcpy(from,fromaddr); |
sunifu | 0:3ea21ce21fe1 | 215 | |
sunifu | 0:3ea21ce21fe1 | 216 | return n; |
sunifu | 0:3ea21ce21fe1 | 217 | } |
sunifu | 0:3ea21ce21fe1 | 218 | char* SimpleSMTPClient::getFromAddress() |
sunifu | 0:3ea21ce21fe1 | 219 | { |
sunifu | 0:3ea21ce21fe1 | 220 | return from; |
sunifu | 0:3ea21ce21fe1 | 221 | } |
sunifu | 0:3ea21ce21fe1 | 222 | int SimpleSMTPClient::setToAddress(char *toaddr) |
sunifu | 0:3ea21ce21fe1 | 223 | { |
sunifu | 0:3ea21ce21fe1 | 224 | int n; |
sunifu | 0:3ea21ce21fe1 | 225 | n = strlen(toaddr); |
sunifu | 0:3ea21ce21fe1 | 226 | if( n <= 0 && 128 <= n) |
sunifu | 0:3ea21ce21fe1 | 227 | return -1; |
sunifu | 0:3ea21ce21fe1 | 228 | strcpy(to,toaddr); |
sunifu | 0:3ea21ce21fe1 | 229 | |
sunifu | 0:3ea21ce21fe1 | 230 | return n; |
sunifu | 0:3ea21ce21fe1 | 231 | } |
sunifu | 0:3ea21ce21fe1 | 232 | char* SimpleSMTPClient::getToAddress() |
sunifu | 0:3ea21ce21fe1 | 233 | { |
sunifu | 0:3ea21ce21fe1 | 234 | return to; |
sunifu | 0:3ea21ce21fe1 | 235 | } |
sunifu | 0:3ea21ce21fe1 | 236 | |
sunifu | 0:3ea21ce21fe1 | 237 | char* SimpleSMTPClient::getSubject() |
sunifu | 0:3ea21ce21fe1 | 238 | { |
sunifu | 0:3ea21ce21fe1 | 239 | return subject; |
sunifu | 0:3ea21ce21fe1 | 240 | } |
sunifu | 0:3ea21ce21fe1 | 241 | |
sunifu | 0:3ea21ce21fe1 | 242 | char* SimpleSMTPClient::getHeader() |
sunifu | 0:3ea21ce21fe1 | 243 | { |
sunifu | 0:3ea21ce21fe1 | 244 | return header; |
sunifu | 0:3ea21ce21fe1 | 245 | } |
sunifu | 0:3ea21ce21fe1 | 246 | |
sunifu | 0:3ea21ce21fe1 | 247 | char* SimpleSMTPClient::getMessage() |
sunifu | 0:3ea21ce21fe1 | 248 | { |
sunifu | 0:3ea21ce21fe1 | 249 | return message; |
sunifu | 0:3ea21ce21fe1 | 250 | } |
sunifu | 0:3ea21ce21fe1 | 251 | |
sunifu | 0:3ea21ce21fe1 | 252 | int SimpleSMTPClient::makeHeader(void) |
sunifu | 0:3ea21ce21fe1 | 253 | { |
sunifu | 0:3ea21ce21fe1 | 254 | strcpy(header, "From: "); |
sunifu | 0:3ea21ce21fe1 | 255 | strcat(header, getFromAddress()); |
sunifu | 0:3ea21ce21fe1 | 256 | strcat(header, "\r\n"); |
sunifu | 0:3ea21ce21fe1 | 257 | strcat(header, "To: " ); |
sunifu | 0:3ea21ce21fe1 | 258 | strcat(header, getToAddress()); |
sunifu | 0:3ea21ce21fe1 | 259 | strcat(header, "\r\n"); |
sunifu | 0:3ea21ce21fe1 | 260 | strcat(header, "Subject: "); |
sunifu | 0:3ea21ce21fe1 | 261 | strcat(header, getSubject()); |
sunifu | 0:3ea21ce21fe1 | 262 | strcat(header, "\r\n\r\n") ; |
sunifu | 0:3ea21ce21fe1 | 263 | |
sunifu | 0:3ea21ce21fe1 | 264 | if ( strlen(header) > 256 ) |
sunifu | 0:3ea21ce21fe1 | 265 | return -1; |
sunifu | 0:3ea21ce21fe1 | 266 | return 0; |
sunifu | 0:3ea21ce21fe1 | 267 | } |