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
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
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
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
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
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
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
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
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
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
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
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
122#ifndef SMTP_CLIENT_PRIVATE_CONTEXT
123 #define SMTP_CLIENT_PRIVATE_CONTEXT
127#if (SMTP_CLIENT_TLS_SUPPORT == ENABLED)
128 #include "core/crypto.h"
133#if (SMTP_CLIENT_LOGIN_AUTH_SUPPORT == ENABLED || SMTP_CLIENT_PLAIN_AUTH_SUPPORT == ENABLED)
134 #include "core/crypto.h"
135 #include "encoding/base64.h"
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"
147#define SMTP_RELAY_PORT 25
149#define SMTP_SUBMISSION_PORT 587
151#define SMTPS_SUBMISSION_PORT 465
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)
161#define SmtpClientContext struct _SmtpClientContext
175 SMTP_MODE_PLAINTEXT = 1,
176 SMTP_MODE_IMPLICIT_TLS = 2,
177 SMTP_MODE_EXPLICIT_TLS = 3
187 SMTP_ADDR_TYPE_FROM = 0,
188 SMTP_ADDR_TYPE_TO = 1,
189 SMTP_ADDR_TYPE_CC = 2,
190 SMTP_ADDR_TYPE_BCC = 3
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
224#if (SMTP_CLIENT_TLS_SUPPORT == ENABLED)
259#if (SMTP_CLIENT_TLS_SUPPORT == ENABLED)
264#if (SMTP_CLIENT_MIME_SUPPORT == ENABLED)
273 char_t
buffer[SMTP_CLIENT_BUFFER_SIZE + 1];
280 SMTP_CLIENT_PRIVATE_CONTEXT
285typedef error_t (*SmtpReplyCallback)(SmtpClientContext *context, char_t *replyLine, uint_t replyCode);
288error_t smtpClientInit(SmtpClientContext *context);
290#if (SMTP_CLIENT_TLS_SUPPORT == ENABLED)
292error_t smtpClientRegisterTlsInitCallback(SmtpClientContext *context,
299error_t smtpClientBindToInterface(SmtpClientContext *context,
300 NetInterface *interface);
302error_t smtpClientConnect(SmtpClientContext *context,
305error_t smtpClientLogin(SmtpClientContext *context,
const char_t *username,
306 const char_t *password);
308error_t smtpClientSetContentType(SmtpClientContext *context,
309 const char_t *contentType);
311error_t smtpClientSetMultipartBoundary(SmtpClientContext *context,
312 const char_t *boundary);
314error_t smtpClientWriteMailHeader(SmtpClientContext *context,
316 uint_t numRecipients,
const char_t *subject);
318error_t smtpClientWriteMailBody(SmtpClientContext *context,
319 const void *data,
size_t length,
size_t *written, uint_t flags);
321error_t smtpClientWriteMultipartHeader(SmtpClientContext *context,
322 const char_t *filename,
const char_t *contentType,
323 const char_t *contentTransferEncoding, bool_t last);
325error_t smtpClientWriteMultipartBody(SmtpClientContext *context,
326 const void *data,
size_t length,
size_t *written, uint_t flags);
328error_t smtpClientCloseMailBody(SmtpClientContext *context);
330uint_t smtpClientGetReplyCode(SmtpClientContext *context);
332error_t smtpClientDisconnect(SmtpClientContext *context);
333error_t smtpClientClose(SmtpClientContext *context);
335void smtpClientDeinit(SmtpClientContext *context);
error_t
Error codes.
Definition error.h:43
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