6 #include "NetClientSmtp.hpp"
7 #include "NetClientDns.hpp"
10 const uint8_t NetClientSmtp::BASE64_INDEX_TABLE[] = {
11 'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
'I',
'J',
'K',
'L',
'M',
'N',
'O',
'P',
'Q',
'R',
'S',
'T',
'U',
'V',
'W',
'X',
'Y',
'Z',
'a',
'b',
'c',
'd',
12 'e',
'f',
'g',
'h',
'i',
'j',
'k',
'l',
'm',
'n',
'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
'w',
'x',
'y',
'z',
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
15 bool NetClientSmtp::start(
const char* server,
bool authRequired,
const char* user,
const char* pass,
const char* from,
const char* to,
const char* subject) {
17 NetDefs::IP_ADDR serverAddr;
19 if (!NetClientDns::resolve(server, &serverAddr)) {
23 if (!mSocket.connect(serverAddr, 25)) {
27 int replyCode = getReplyCode();
28 if (replyCode < 200 || replyCode >= 300) {
32 mSocket.printf (
"HELO %s\r\n", user);
33 replyCode = getReplyCode();
34 if (replyCode < 200 || replyCode >= 300) {
39 mSocket.printf (
"AUTH LOGIN\r\n");
40 replyCode = getReplyCode();
41 if (replyCode < 300 || replyCode >= 400) {
46 mSocket.printf (
"\r\n");
47 replyCode = getReplyCode();
48 if (replyCode < 300 || replyCode >= 400) {
53 mSocket.printf (
"\r\n");
54 replyCode = getReplyCode();
55 if (replyCode < 200 || replyCode >= 300) {
60 mSocket.printf (
"MAIL FROM:<%s>\r\n", from);
61 replyCode = getReplyCode();
62 if (replyCode < 200 || replyCode >= 300) {
66 mSocket.printf (
"RCPT TO:<%s>\r\n", to);
67 replyCode = getReplyCode();
68 if (replyCode < 200 || replyCode >= 300) {
72 mSocket.printf (
"DATA\r\n");
73 replyCode = getReplyCode();
74 if (replyCode < 300 || replyCode >= 400) {
78 mSocket.printf (
"from:<%s>\r\nsubject:%s\r\n\r\n", from, subject);
83 int NetClientSmtp::queueBody(
const char* format, ...) {
87 va_start(vaList, format);
89 len = LibStringFormat::vprintf(mSocket,format, vaList);
96 bool NetClientSmtp::end() {
97 mSocket.printf(
"\r\n.\r\n");
99 int replyCode = getReplyCode();
100 if (replyCode < 200 || replyCode >= 300) {
106 void NetClientSmtp::closeConnection() {
110 void NetClientSmtp::sendBase64(
const char*
string) {
111 int len = strlen(
string);
113 for (
int i = 0; i < len;) {
115 uint32_t bitPattern = (uint32_t)
string[i++] << 16;
117 bitPattern += (uint32_t)
string[i++] << 8;
121 bitPattern += (uint32_t)
string[i++];
127 encoded[0] = BASE64_INDEX_TABLE[(bitPattern >> 18) & 0x3F];
128 encoded[1] = BASE64_INDEX_TABLE[(bitPattern >> 12) & 0x3F];
129 encoded[2] = BASE64_INDEX_TABLE[(bitPattern >> 6) & 0x3F];
130 encoded[3] = BASE64_INDEX_TABLE[(bitPattern >> 0) & 0x3F];
132 if (sentChars == 1) {
136 else if (sentChars == 2) {
140 mSocket.write(encoded, 4);
144 int NetClientSmtp::getReplyCode() {
152 int replyCode = ((int) (tmp[0] -
'0') * 100) + ((
int) (tmp[1] -
'0') * 10) + ((int) (tmp[2] -
'0'));
154 bool isLastLine =
true;
163 if (tmp[0] ==
'\n') {