smtp_client.h
Go to the documentation of this file.
1 /**
2  * @file smtp_client.h
3  * @brief SMTP client (Simple Mail Transfer Protocol)
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2020 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 1.9.8
29  **/
30 
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 //TLS supported?
122 #if (SMTP_CLIENT_TLS_SUPPORT == ENABLED)
123  #include "core/crypto.h"
124  #include "tls.h"
125 #endif
126 
127 //LOGIN or PLAIN authentication supported?
128 #if (SMTP_CLIENT_LOGIN_AUTH_SUPPORT == ENABLED || SMTP_CLIENT_PLAIN_AUTH_SUPPORT == ENABLED)
129  #include "core/crypto.h"
130  #include "encoding/base64.h"
131 #endif
132 
133 //CRAM-MD5 authentication supported?
134 #if (SMTP_CLIENT_CRAM_MD5_AUTH_SUPPORT == ENABLED)
135  #include "core/crypto.h"
136  #include "encoding/base64.h"
137  #include "mac/hmac.h"
138  #include "hash/md5.h"
139 #endif
140 
141 //SMTP port number (message relaying)
142 #define SMTP_RELAY_PORT 25
143 //SMTP port number (message submission)
144 #define SMTP_SUBMISSION_PORT 587
145 //SMTPS port number (message submission over TLS)
146 #define SMTPS_SUBMISSION_PORT 465
147 
148 //Test macros for SMTP response codes
149 #define SMTP_REPLY_CODE_2YZ(code) ((code) >= 200 && (code) < 300)
150 #define SMTP_REPLY_CODE_3YZ(code) ((code) >= 300 && (code) < 400)
151 #define SMTP_REPLY_CODE_4YZ(code) ((code) >= 400 && (code) < 500)
152 #define SMTP_REPLY_CODE_5YZ(code) ((code) >= 500 && (code) < 600)
153 
154 //Forward declaration of SmtpClientContext structure
155 struct _SmtpClientContext;
156 #define SmtpClientContext struct _SmtpClientContext
157 
158 //C++ guard
159 #ifdef __cplusplus
160 extern "C" {
161 #endif
162 
163 
164 /**
165  * @brief SMTP connection modes
166  **/
167 
168 typedef enum
169 {
174 
175 
176 /**
177  * @brief Email address types
178  **/
179 
180 typedef enum
181 {
187 
188 
189 /**
190  * @brief SMTP client states
191  */
192 
193 typedef enum
194 {
208 
209 
210 /**
211  * @brief Multiline reply parsing callback function
212  **/
213 
215  char_t *replyLine);
216 
217 
218 //TLS supported?
219 #if (SMTP_CLIENT_TLS_SUPPORT == ENABLED)
220 
221 /**
222  * @brief TLS initialization callback function
223  **/
224 
227 
228 #endif
229 
230 
231 /**
232  * @brief Email address
233  **/
234 
235 typedef struct
236 {
240 } SmtpMailAddr;
241 
242 
243 /**
244  * @brief SMTP client context
245  **/
246 
248 {
249  SmtpClientState state; ///<SMTP client state
250  NetInterface *interface; ///<Underlying network interface
251  systime_t timeout; ///<Timeout value
252  systime_t timestamp; ///<Timestamp to manage timeout
253  Socket *socket; ///<Underlying socket
254 #if (SMTP_CLIENT_TLS_SUPPORT == ENABLED)
255  TlsContext *tlsContext; ///<TLS context
256  TlsSessionState tlsSession; ///<TLS session state
257  SmtpClientTlsInitCallback tlsInitCallback; ///<TLS initialization callback function
258 #endif
259 #if (SMTP_CLIENT_MIME_SUPPORT == ENABLED)
261  char_t boundary[SMTP_CLIENT_BOUNDARY_MAX_LEN + 1]; ///<Boundary string
262  bool_t base64Encoding; ///<Base64 encoding
263 #endif
264  bool_t startTlsSupported; ///<STARTTLS command supported
265  bool_t authLoginSupported; ///<LOGIN authentication mechanism supported
266  bool_t authPlainSupported; ///<PLAIN authentication mechanism supported
267  bool_t authCramMd5Supported; ///<CRAM-MD5 authentication mechanism supported
268  char_t buffer[SMTP_CLIENT_BUFFER_SIZE + 1]; ///<Memory buffer for input/output operations
269  size_t bufferLen; ///<Length of the buffer, in bytes
270  size_t bufferPos; ///<Current position in the buffer
271  size_t commandLen; ///<Length of the SMTP command, in bytes
272  size_t replyLen; ///<Length of the SMTP reply, in bytes
273  uint_t replyCode; ///<SMTP reply code
274  uint_t recipientIndex; ///<Index of the current recipient
275 };
276 
277 
278 //Callback function to parse a response line
279 typedef error_t (*SmtpReplyCallback)(SmtpClientContext *context, char_t *replyLine, uint_t replyCode);
280 
281 //SMTP related functions
283 
284 #if (SMTP_CLIENT_TLS_SUPPORT == ENABLED)
285 
287  SmtpClientTlsInitCallback callback);
288 
289 #endif
290 
292 
294  NetInterface *interface);
295 
297  const IpAddr *serverIpAddr, uint16_t serverPort, SmtpConnectionMode mode);
298 
299 error_t smtpClientLogin(SmtpClientContext *context, const char_t *username,
300  const char_t *password);
301 
303  const char_t *contentType);
304 
306  const char_t *boundary);
307 
309  const SmtpMailAddr *from, const SmtpMailAddr *recipients,
310  uint_t numRecipients, const char_t *subject);
311 
313  const void *data, size_t length, size_t *written, uint_t flags);
314 
316  const char_t *filename, const char_t *contentType,
317  const char_t *contentTransferEncoding, bool_t last);
318 
320  const void *data, size_t length, size_t *written, uint_t flags);
321 
323 
325 
328 
329 void smtpClientDeinit(SmtpClientContext *context);
330 
331 //C++ guard
332 #ifdef __cplusplus
333 }
334 #endif
335 
336 #endif
error_t smtpClientCloseMailBody(SmtpClientContext *context)
Complete email sending process and wait for server's status.
Definition: smtp_client.c:1081
uint8_t length
Definition: coap_common.h:190
TlsContext * tlsContext
TLS context.
Definition: smtp_client.h:255
SMTP client context.
Definition: smtp_client.h:247
int bool_t
Definition: compiler_port.h:49
char_t contentType[SMTP_CLIENT_CONTENT_TYPE_MAX_LEN+1]
Content type.
Definition: smtp_client.h:260
size_t replyLen
Length of the SMTP reply, in bytes.
Definition: smtp_client.h:272
#define SMTP_CLIENT_BUFFER_SIZE
Definition: smtp_client.h:88
uint8_t data[]
Definition: ethernet.h:209
error_t smtpClientClose(SmtpClientContext *context)
Close the connection with the SMTP server.
Definition: smtp_client.c:1278
IP network address.
Definition: ip.h:78
size_t bufferPos
Current position in the buffer.
Definition: smtp_client.h:270
systime_t timestamp
Timestamp to manage timeout.
Definition: smtp_client.h:252
Email address.
Definition: smtp_client.h:235
uint16_t last
Definition: ipv4_frag.h:96
bool_t authLoginSupported
LOGIN authentication mechanism supported.
Definition: smtp_client.h:265
error_t smtpClientWriteMultipartBody(SmtpClientContext *context, const void *data, size_t length, size_t *written, uint_t flags)
Write data to the the multipart body.
Definition: smtp_client.c:934
error_t smtpClientDisconnect(SmtpClientContext *context)
Gracefully disconnect from the SMTP server.
Definition: smtp_client.c:1186
error_t smtpClientRegisterTlsInitCallback(SmtpClientContext *context, SmtpClientTlsInitCallback callback)
Register TLS initialization callback function.
Definition: smtp_client.c:102
SmtpMailAddrType
Email address types.
Definition: smtp_client.h:180
char_t buffer[SMTP_CLIENT_BUFFER_SIZE+1]
Memory buffer for input/output operations.
Definition: smtp_client.h:268
bool_t authPlainSupported
PLAIN authentication mechanism supported.
Definition: smtp_client.h:266
#define SmtpClientContext
Definition: smtp_client.h:156
SmtpClientState
SMTP client states.
Definition: smtp_client.h:193
char_t filename[]
Definition: tftp_common.h:91
#define TlsContext
Definition: tls.h:36
#define SMTP_CLIENT_CONTENT_TYPE_MAX_LEN
Definition: smtp_client.h:109
error_t
Error codes.
Definition: error.h:42
bool_t authCramMd5Supported
CRAM-MD5 authentication mechanism supported.
Definition: smtp_client.h:267
error_t smtpClientWriteMultipartHeader(SmtpClientContext *context, const char_t *filename, const char_t *contentType, const char_t *contentTransferEncoding, bool_t last)
Write multipart header.
Definition: smtp_client.c:797
error_t(* SmtpClientTlsInitCallback)(SmtpClientContext *context, TlsContext *tlsContext)
TLS initialization callback function.
Definition: smtp_client.h:225
#define NetInterface
Definition: net.h:36
error_t smtpClientLogin(SmtpClientContext *context, const char_t *username, const char_t *password)
Login to the SMTP server using the provided user name and password.
Definition: smtp_client.c:431
#define SMTP_CLIENT_BOUNDARY_MAX_LEN
Definition: smtp_client.h:116
General definitions for cryptographic algorithms.
SmtpMailAddrType type
Definition: smtp_client.h:239
Base64 encoding scheme.
error_t smtpClientWriteMailBody(SmtpClientContext *context, const void *data, size_t length, size_t *written, uint_t flags)
Write email body.
Definition: smtp_client.c:727
Socket * socket
Underlying socket.
Definition: smtp_client.h:253
error_t smtpClientSetMultipartBoundary(SmtpClientContext *context, const char_t *boundary)
Define the boundary string to be used (multipart encoding)
Definition: smtp_client.c:520
SmtpClientState state
SMTP client state.
Definition: smtp_client.h:249
char_t * name
Definition: smtp_client.h:237
void smtpClientDeinit(SmtpClientContext *context)
Release SMTP client context.
Definition: smtp_client.c:1299
error_t smtpClientSetTimeout(SmtpClientContext *context, systime_t timeout)
Set communication timeout.
Definition: smtp_client.c:126
uint8_t flags
Definition: tcp.h:314
char char_t
Definition: compiler_port.h:43
systime_t timeout
Timeout value.
Definition: smtp_client.h:251
TlsSessionState tlsSession
TLS session state.
Definition: smtp_client.h:256
uint_t smtpClientGetReplyCode(SmtpClientContext *context)
Retrieve server's reply code.
Definition: smtp_client.c:1159
error_t(* SmtpClientReplyCallback)(SmtpClientContext *context, char_t *replyLine)
Multiline reply parsing callback function.
Definition: smtp_client.h:214
error_t(* SmtpReplyCallback)(SmtpClientContext *context, char_t *replyLine, uint_t replyCode)
Definition: smtp_client.h:279
uint_t recipientIndex
Index of the current recipient.
Definition: smtp_client.h:274
SmtpConnectionMode
SMTP connection modes.
Definition: smtp_client.h:168
TLS session state.
Definition: tls.h:1865
MD5 (Message-Digest Algorithm)
error_t smtpClientWriteMailHeader(SmtpClientContext *context, const SmtpMailAddr *from, const SmtpMailAddr *recipients, uint_t numRecipients, const char_t *subject)
Write email header.
Definition: smtp_client.c:559
#define Socket
Definition: socket.h:36
bool_t base64Encoding
Base64 encoding.
Definition: smtp_client.h:262
error_t smtpClientInit(SmtpClientContext *context)
Initialize SMTP client context.
Definition: smtp_client.c:61
error_t smtpClientConnect(SmtpClientContext *context, const IpAddr *serverIpAddr, uint16_t serverPort, SmtpConnectionMode mode)
Establish a connection with the specified SMTP server.
Definition: smtp_client.c:171
TLS (Transport Layer Security)
uint_t replyCode
SMTP reply code.
Definition: smtp_client.h:273
size_t bufferLen
Length of the buffer, in bytes.
Definition: smtp_client.h:269
uint8_t mode
Definition: ntp_common.h:149
error_t smtpClientSetContentType(SmtpClientContext *context, const char_t *contentType)
Set the content type to be used.
Definition: smtp_client.c:484
SmtpClientTlsInitCallback tlsInitCallback
TLS initialization callback function.
Definition: smtp_client.h:257
char_t * addr
Definition: smtp_client.h:238
size_t commandLen
Length of the SMTP command, in bytes.
Definition: smtp_client.h:271
error_t smtpClientBindToInterface(SmtpClientContext *context, NetInterface *interface)
Bind the SMTP client to a particular network interface.
Definition: smtp_client.c:147
bool_t startTlsSupported
STARTTLS command supported.
Definition: smtp_client.h:264
unsigned int uint_t
Definition: compiler_port.h:45
TCP/IP stack core.
NetInterface * interface
Underlying network interface.
Definition: smtp_client.h:250
uint32_t systime_t
Definition: compiler_port.h:46
HMAC (Keyed-Hashing for Message Authentication)
char_t boundary[SMTP_CLIENT_BOUNDARY_MAX_LEN+1]
Boundary string.
Definition: smtp_client.h:261