SmtpClient.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * SmtpClient.h - Asynchronous SmtpClient that supports the following features:
8  *
9  * - extended HELO command set
10  * - support for PIPELINING
11  * - support for STARTTLS (if the directive ENABLE_SSL=1 is set)
12  * - support for smtp connection over SSL (if the directive ENABLE_SSL=1 is set)
13  * - support for PLAIN and CRAM-MD5 authentication
14  * - support for multiple attachments
15  * - Support for base64 and quoted-printable transfer encoding
16  *
17  * Author: 2018 - Slavey Karadzhov <slav@attachix.com>
18  *
19  ****/
20 
21 #pragma once
22 
29 #include "TcpClient.h"
30 #include "MailMessage.h"
31 #include "Url.h"
32 #include <BitManipulations.h>
33 #include <WString.h>
34 #include <WVector.h>
36 #include <WConstants.h>
37 #include <Data/ObjectQueue.h>
38 
39 /* Maximum waiting emails in the mail queue */
40 #define SMTP_QUEUE_SIZE 5
41 
42 /* Buffer size used to read the error messages */
43 #define SMTP_ERROR_LENGTH 40
44 
48 #define SMTP_CODE_SERVICE_READY 220
49 #define SMTP_CODE_BYE 221
50 #define SMTP_CODE_AUTH_OK 235
51 #define SMTP_CODE_REQUEST_OK 250
52 #define SMTP_CODE_AUTH_CHALLENGE 334
53 #define SMTP_CODE_START_DATA 354
54 
55 #define SMTP_OPT_PIPELINE bit(0)
56 #define SMTP_OPT_STARTTLS bit(1)
57 #define SMTP_OPT_AUTH_PLAIN bit(2)
58 #define SMTP_OPT_AUTH_LOGIN bit(3)
59 #define SMTP_OPT_AUTH_CRAM_MD5 bit(4)
60 
61 enum SmtpState {
84 };
85 
86 class SmtpClient;
87 
88 using SmtpClientCallback = Delegate<int(SmtpClient& client, int code, char* status)>;
89 
90 class SmtpClient : protected TcpClient
91 {
92 public:
93  SmtpClient(bool autoDestroy = false) : TcpClient(autoDestroy)
94  {
95  }
96 
98 
106  bool connect(const Url& url);
107 
119  bool send(const String& from, const String& to, const String& subject, const String& body);
120  bool send(const String& from, const String& to, const String& subject, String&& body) noexcept;
129  bool send(MailMessage* message);
130 
137 
138  size_t countPending()
139  {
140  return mailQ.count();
141  }
142 
146  void quit();
147 
152  {
153  return state;
154  }
155 
161  {
162  messageSentCallback = callback;
163  }
164 
170  {
171  errorCallback = callback;
172  }
173 
174  using TcpClient::getSsl;
176  using TcpClient::setTimeOut;
177 
178 protected:
179  err_t onReceive(pbuf* buf) override;
180  void onReadyToSendData(TcpConnectionEvent sourceEvent) override;
181 
184 
185 private:
186  Url url;
187  Vector<String> authMethods;
189  char code[4]{0};
190  int codeValue{0};
191  String authChallenge;
192  char message[SMTP_ERROR_LENGTH + 1]{0};
193  bool isLastLine{false};
194  uint8_t codeLength{0};
195  int options{0};
196  MailMessage* outgoingMail{nullptr};
197  SmtpState state{eSMTP_Banner};
198 
199  SmtpClientCallback errorCallback;
200  SmtpClientCallback messageSentCallback;
201 
202 private:
206  int smtpParse(char* data, size_t len);
207 
212  MultipartStream::BodyPart multipartProducer();
213 };
214 
unsigned int count() const override
Definition: FIFO.h:37
Definition: MailMessage.h:32
Definition: SmtpClient.h:91
err_t onReceive(pbuf *buf) override
SmtpClient(bool autoDestroy=false)
Definition: SmtpClient.h:93
void onReadyToSendData(TcpConnectionEvent sourceEvent) override
bool connect(const Url &url)
Connects to remote URL.
void onServerError(SmtpClientCallback callback)
Callback that will be called every an error occurs.
Definition: SmtpClient.h:169
void quit()
Sends a quit command to the server and closes the TCP connection.
bool send(const String &from, const String &to, const String &subject, String &&body) noexcept
void onMessageSent(SmtpClientCallback callback)
Callback that will be called every time a message is sent successfully.
Definition: SmtpClient.h:160
size_t countPending()
Definition: SmtpClient.h:138
void sendMailHeaders(MailMessage *mail)
bool send(MailMessage *message)
Powerful method to queues a single message before it is sent later to the SMTP server.
bool send(const String &from, const String &to, const String &subject, const String &body)
Queues a single message before it is sent later to the SMTP server.
bool sendMailBody(MailMessage *mail)
SmtpState getState()
Returns the current state of the SmtpClient.
Definition: SmtpClient.h:151
MailMessage * getCurrentMessage()
Gets the current message.
The String class.
Definition: WString.h:137
Definition: TcpClient.h:46
void setTimeOut(uint16_t waitTimeOut)
Ssl::Session * getSsl()
Get a pointer to the current SSL session object.
Definition: TcpConnection.h:152
void setSslInitHandler(Ssl::Session::InitDelegate handler)
Set the SSL session initialisation callback.
Definition: TcpConnection.h:129
Class to manage URL instance.
Definition: Url.h:67
#define SMTP_ERROR_LENGTH
Definition: SmtpClient.h:43
SmtpState
Definition: SmtpClient.h:61
@ eSMTP_SendingData
Definition: SmtpClient.h:76
@ eSMTP_SendData
Definition: SmtpClient.h:75
@ eSMTP_Banner
Definition: SmtpClient.h:62
@ eSMTP_Sent
Definition: SmtpClient.h:81
@ eSMTP_SendingBody
Definition: SmtpClient.h:80
@ eSMTP_RequestingAuthChallenge
Definition: SmtpClient.h:67
@ eSMTP_SendAuthResponse
Definition: SmtpClient.h:68
@ eSMTP_Hello
Definition: SmtpClient.h:63
@ eSMTP_Ready
Definition: SmtpClient.h:70
@ eSMTP_SendingRcpt
Definition: SmtpClient.h:74
@ eSMTP_SendingAuth
Definition: SmtpClient.h:69
@ eSMTP_Quitting
Definition: SmtpClient.h:82
@ eSMTP_SendingMail
Definition: SmtpClient.h:72
@ eSMTP_SendMail
Definition: SmtpClient.h:71
@ eSMTP_StartBody
Definition: SmtpClient.h:79
@ eSMTP_SendingHeaders
Definition: SmtpClient.h:78
@ eSMTP_SendAuth
Definition: SmtpClient.h:65
@ eSMTP_SendHeader
Definition: SmtpClient.h:77
@ eSMTP_StartTLS
Definition: SmtpClient.h:64
@ eSMTP_SendRcpt
Definition: SmtpClient.h:73
@ eSMTP_Disconnect
Definition: SmtpClient.h:83
@ eSMTP_SendingAuthLogin
Definition: SmtpClient.h:66
TcpConnectionEvent
Definition: TcpConnection.h:26
Each result item contains a set of headers plus content stream.
Definition: MultipartStream.h:29