mikroSDK Reference Manual
smtp_client.h
Go to the documentation of this file.
1
31#ifndef _SMTP_CLIENT_H
32#define _SMTP_CLIENT_H
33
34//Dependencies
35#include "core/net.h"
36
37//SMTP client support
38#ifndef SMTP_CLIENT_SUPPORT
39 #define SMTP_CLIENT_SUPPORT ENABLED
40#elif (SMTP_CLIENT_SUPPORT != ENABLED && SMTP_CLIENT_SUPPORT != DISABLED)
41 #error SMTP_CLIENT_SUPPORT parameter is not valid
42#endif
43
44//MIME extension support
45#ifndef SMTP_CLIENT_MIME_SUPPORT
46 #define SMTP_CLIENT_MIME_SUPPORT ENABLED
47#elif (SMTP_CLIENT_MIME_SUPPORT != ENABLED && SMTP_CLIENT_MIME_SUPPORT != DISABLED)
48 #error SMTP_CLIENT_MIME_SUPPORT parameter is not valid
49#endif
50
51//SMTP over TLS
52#ifndef SMTP_CLIENT_TLS_SUPPORT
53 #define SMTP_CLIENT_TLS_SUPPORT DISABLED
54#elif (SMTP_CLIENT_TLS_SUPPORT != ENABLED && SMTP_CLIENT_TLS_SUPPORT != DISABLED)
55 #error SMTP_CLIENT_TLS_SUPPORT parameter is not valid
56#endif
57
58//LOGIN authentication support
59#ifndef SMTP_CLIENT_LOGIN_AUTH_SUPPORT
60 #define SMTP_CLIENT_LOGIN_AUTH_SUPPORT ENABLED
61#elif (SMTP_CLIENT_LOGIN_AUTH_SUPPORT != ENABLED && SMTP_CLIENT_LOGIN_AUTH_SUPPORT != DISABLED)
62 #error SMTP_CLIENT_LOGIN_AUTH_SUPPORT parameter is not valid
63#endif
64
65//PLAIN authentication support
66#ifndef SMTP_CLIENT_PLAIN_AUTH_SUPPORT
67 #define SMTP_CLIENT_PLAIN_AUTH_SUPPORT ENABLED
68#elif (SMTP_CLIENT_PLAIN_AUTH_SUPPORT != ENABLED && SMTP_CLIENT_PLAIN_AUTH_SUPPORT != DISABLED)
69 #error SMTP_CLIENT_PLAIN_AUTH_SUPPORT parameter is not valid
70#endif
71
72//CRAM-MD5 authentication support
73#ifndef SMTP_CLIENT_CRAM_MD5_AUTH_SUPPORT
74 #define SMTP_CLIENT_CRAM_MD5_AUTH_SUPPORT DISABLED
75#elif (SMTP_CLIENT_CRAM_MD5_AUTH_SUPPORT != ENABLED && SMTP_CLIENT_CRAM_MD5_AUTH_SUPPORT != DISABLED)
76 #error SMTP_CLIENT_CRAM_MD5_AUTH_SUPPORT parameter is not valid
77#endif
78
79//Default timeout
80#ifndef SMTP_CLIENT_DEFAULT_TIMEOUT
81 #define SMTP_CLIENT_DEFAULT_TIMEOUT 20000
82#elif (SMTP_CLIENT_DEFAULT_TIMEOUT < 1000)
83 #error SMTP_CLIENT_DEFAULT_TIMEOUT parameter is not valid
84#endif
85
86//Size of the buffer for input/output operations
87#ifndef SMTP_CLIENT_BUFFER_SIZE
88 #define SMTP_CLIENT_BUFFER_SIZE 512
89#elif (SMTP_CLIENT_BUFFER_SIZE < 64)
90 #error SMTP_CLIENT_BUFFER_SIZE parameter is not valid
91#endif
92
93//TX buffer size for TLS connections
94#ifndef SMTP_CLIENT_TLS_TX_BUFFER_SIZE
95 #define SMTP_CLIENT_TLS_TX_BUFFER_SIZE 2048
96#elif (SMTP_CLIENT_TLS_TX_BUFFER_SIZE < 512)
97 #error SMTP_CLIENT_TLS_TX_BUFFER_SIZE parameter is not valid
98#endif
99
100//RX buffer size for TLS connections
101#ifndef SMTP_CLIENT_TLS_RX_BUFFER_SIZE
102 #define SMTP_CLIENT_TLS_RX_BUFFER_SIZE 4096
103#elif (SMTP_CLIENT_TLS_RX_BUFFER_SIZE < 512)
104 #error SMTP_CLIENT_TLS_RX_BUFFER_SIZE parameter is not valid
105#endif
106
107//Maximum length for content type
108#ifndef SMTP_CLIENT_CONTENT_TYPE_MAX_LEN
109 #define SMTP_CLIENT_CONTENT_TYPE_MAX_LEN 32
110#elif (SMTP_CLIENT_CONTENT_TYPE_MAX_LEN < 1)
111 #error SMTP_CLIENT_CONTENT_TYPE_MAX_LEN parameter is not valid
112#endif
113
114//Maximum length for boundary string
115#ifndef SMTP_CLIENT_BOUNDARY_MAX_LEN
116 #define SMTP_CLIENT_BOUNDARY_MAX_LEN 70
117#elif (SMTP_CLIENT_BOUNDARY_MAX_LEN < 1)
118 #error SMTP_CLIENT_BOUNDARY_MAX_LEN parameter is not valid
119#endif
120
121//Application specific context
122#ifndef SMTP_CLIENT_PRIVATE_CONTEXT
123 #define SMTP_CLIENT_PRIVATE_CONTEXT
124#endif
125
126//TLS supported?
127#if (SMTP_CLIENT_TLS_SUPPORT == ENABLED)
128 #include "core/crypto.h"
129 #include "tls.h"
130#endif
131
132//LOGIN or PLAIN authentication supported?
133#if (SMTP_CLIENT_LOGIN_AUTH_SUPPORT == ENABLED || SMTP_CLIENT_PLAIN_AUTH_SUPPORT == ENABLED)
134 #include "core/crypto.h"
135 #include "encoding/base64.h"
136#endif
137
138//CRAM-MD5 authentication supported?
139#if (SMTP_CLIENT_CRAM_MD5_AUTH_SUPPORT == ENABLED)
140 #include "core/crypto.h"
141 #include "encoding/base64.h"
142 #include "mac/hmac.h"
143 #include "hash/md5.h"
144#endif
145
146//SMTP port number (message relaying)
147#define SMTP_RELAY_PORT 25
148//SMTP port number (message submission)
149#define SMTP_SUBMISSION_PORT 587
150//SMTPS port number (message submission over TLS)
151#define SMTPS_SUBMISSION_PORT 465
152
153//Test macros for SMTP response codes
154#define SMTP_REPLY_CODE_2YZ(code) ((code) >= 200 && (code) < 300)
155#define SMTP_REPLY_CODE_3YZ(code) ((code) >= 300 && (code) < 400)
156#define SMTP_REPLY_CODE_4YZ(code) ((code) >= 400 && (code) < 500)
157#define SMTP_REPLY_CODE_5YZ(code) ((code) >= 500 && (code) < 600)
158
159//Forward declaration of SmtpClientContext structure
160struct _SmtpClientContext;
161#define SmtpClientContext struct _SmtpClientContext
162
163//C++ guard
164#ifdef __cplusplus
165extern "C" {
166#endif
167
168
173typedef enum
174{
175 SMTP_MODE_PLAINTEXT = 1,
176 SMTP_MODE_IMPLICIT_TLS = 2,
177 SMTP_MODE_EXPLICIT_TLS = 3
179
180
185typedef enum
186{
187 SMTP_ADDR_TYPE_FROM = 0,
188 SMTP_ADDR_TYPE_TO = 1,
189 SMTP_ADDR_TYPE_CC = 2,
190 SMTP_ADDR_TYPE_BCC = 3
192
193
198typedef enum
199{
200 SMTP_CLIENT_STATE_DISCONNECTED = 0,
201 SMTP_CLIENT_STATE_CONNECTING_TCP = 1,
202 SMTP_CLIENT_STATE_CONNECTING_TLS = 2,
203 SMTP_CLIENT_STATE_CONNECTED = 3,
204 SMTP_CLIENT_STATE_SUB_COMMAND_1 = 4,
205 SMTP_CLIENT_STATE_SUB_COMMAND_2 = 5,
206 SMTP_CLIENT_STATE_SUB_COMMAND_3 = 6,
207 SMTP_CLIENT_STATE_MAIL_HEADER = 7,
208 SMTP_CLIENT_STATE_MAIL_BODY = 8,
209 SMTP_CLIENT_STATE_MULTIPART_HEADER = 9,
210 SMTP_CLIENT_STATE_MULTIPART_BODY = 10,
211 SMTP_CLIENT_STATE_DISCONNECTING = 11
213
214
219typedef error_t (*SmtpClientReplyCallback)(SmtpClientContext *context,
220 char_t *replyLine);
221
222
223//TLS supported?
224#if (SMTP_CLIENT_TLS_SUPPORT == ENABLED)
225
230typedef error_t (*SmtpClientTlsInitCallback)(SmtpClientContext *context,
231 TlsContext *tlsContext);
232
233#endif
234
235
240typedef struct
241{
242 char_t *name;
243 char_t *addr;
244 SmtpMailAddrType type;
246
247
253{
255 NetInterface *interface;
258 Socket *socket;
259#if (SMTP_CLIENT_TLS_SUPPORT == ENABLED)
260 TlsContext *tlsContext;
261 TlsSessionState tlsSession;
263#endif
264#if (SMTP_CLIENT_MIME_SUPPORT == ENABLED)
265 char_t contentType[SMTP_CLIENT_CONTENT_TYPE_MAX_LEN + 1];
266 char_t boundary[SMTP_CLIENT_BOUNDARY_MAX_LEN + 1];
268#endif
273 char_t buffer[SMTP_CLIENT_BUFFER_SIZE + 1];
274 size_t bufferLen;
275 size_t bufferPos;
276 size_t commandLen;
277 size_t replyLen;
278 uint_t replyCode;
280 SMTP_CLIENT_PRIVATE_CONTEXT
281};
282
283
284//Callback function to parse a response line
285typedef error_t (*SmtpReplyCallback)(SmtpClientContext *context, char_t *replyLine, uint_t replyCode);
286
287//SMTP related functions
288error_t smtpClientInit(SmtpClientContext *context);
289
290#if (SMTP_CLIENT_TLS_SUPPORT == ENABLED)
291
292error_t smtpClientRegisterTlsInitCallback(SmtpClientContext *context,
294
295#endif
296
297error_t smtpClientSetTimeout(SmtpClientContext *context, systime_t timeout);
298
299error_t smtpClientBindToInterface(SmtpClientContext *context,
300 NetInterface *interface);
301
302error_t smtpClientConnect(SmtpClientContext *context,
303 const IpAddr *serverIpAddr, uint16_t serverPort, SmtpConnectionMode mode);
304
305error_t smtpClientLogin(SmtpClientContext *context, const char_t *username,
306 const char_t *password);
307
308error_t smtpClientSetContentType(SmtpClientContext *context,
309 const char_t *contentType);
310
311error_t smtpClientSetMultipartBoundary(SmtpClientContext *context,
312 const char_t *boundary);
313
314error_t smtpClientWriteMailHeader(SmtpClientContext *context,
315 const SmtpMailAddr *from, const SmtpMailAddr *recipients,
316 uint_t numRecipients, const char_t *subject);
317
318error_t smtpClientWriteMailBody(SmtpClientContext *context,
319 const void *data, size_t length, size_t *written, uint_t flags);
320
321error_t smtpClientWriteMultipartHeader(SmtpClientContext *context,
322 const char_t *filename, const char_t *contentType,
323 const char_t *contentTransferEncoding, bool_t last);
324
325error_t smtpClientWriteMultipartBody(SmtpClientContext *context,
326 const void *data, size_t length, size_t *written, uint_t flags);
327
328error_t smtpClientCloseMailBody(SmtpClientContext *context);
329
330uint_t smtpClientGetReplyCode(SmtpClientContext *context);
331
332error_t smtpClientDisconnect(SmtpClientContext *context);
333error_t smtpClientClose(SmtpClientContext *context);
334
335void smtpClientDeinit(SmtpClientContext *context);
336
337//C++ guard
338#ifdef __cplusplus
339}
340#endif
341
342#endif
error_t
Error codes.
Definition error.h:43
TCP/IP stack core.
uint32_t systime_t
System time.
Definition os_port_none.h:90
SmtpClientState
SMTP client states.
Definition smtp_client.h:199
error_t(* SmtpClientReplyCallback)(SmtpClientContext *context, char_t *replyLine)
Multiline reply parsing callback function.
Definition smtp_client.h:219
SmtpConnectionMode
SMTP connection modes.
Definition smtp_client.h:174
error_t(* SmtpClientTlsInitCallback)(SmtpClientContext *context, TlsContext *tlsContext)
TLS initialization callback function.
Definition smtp_client.h:230
SmtpMailAddrType
Email address types.
Definition smtp_client.h:186
IP network address.
Definition ip.h:72
Email address.
Definition smtp_client.h:241
SMTP client context.
Definition smtp_client.h:253
bool_t authCramMd5Supported
CRAM-MD5 authentication mechanism supported.
Definition smtp_client.h:272
char_t buffer[SMTP_CLIENT_BUFFER_SIZE+1]
Memory buffer for input/output operations.
Definition smtp_client.h:273
size_t replyLen
Length of the SMTP reply, in bytes.
Definition smtp_client.h:277
size_t commandLen
Length of the SMTP command, in bytes.
Definition smtp_client.h:276
uint_t recipientIndex
Index of the current recipient.
Definition smtp_client.h:279
bool_t startTlsSupported
STARTTLS command supported.
Definition smtp_client.h:269
size_t bufferLen
Length of the buffer, in bytes.
Definition smtp_client.h:274
bool_t base64Encoding
Base64 encoding.
Definition smtp_client.h:267
SmtpClientState state
SMTP client state.
Definition smtp_client.h:254
systime_t timeout
Timeout value.
Definition smtp_client.h:256
bool_t authPlainSupported
PLAIN authentication mechanism supported.
Definition smtp_client.h:271
uint_t replyCode
SMTP reply code.
Definition smtp_client.h:278
TlsContext * tlsContext
TLS context.
Definition smtp_client.h:260
SmtpClientTlsInitCallback tlsInitCallback
TLS initialization callback function.
Definition smtp_client.h:262
TlsSessionState tlsSession
TLS session state.
Definition smtp_client.h:261
char_t boundary[SMTP_CLIENT_BOUNDARY_MAX_LEN+1]
Boundary string.
Definition smtp_client.h:266
NetInterface * interface
Underlying network interface.
Definition smtp_client.h:255
char_t contentType[SMTP_CLIENT_CONTENT_TYPE_MAX_LEN+1]
Content type.
Definition smtp_client.h:265
size_t bufferPos
Current position in the buffer.
Definition smtp_client.h:275
Socket * socket
Underlying socket.
Definition smtp_client.h:258
systime_t timestamp
Timestamp to manage timeout.
Definition smtp_client.h:257
bool_t authLoginSupported
LOGIN authentication mechanism supported.
Definition smtp_client.h:270