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 <Data/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 
89 
90 class SmtpClient : protected TcpClient
91 {
92 public:
93  SmtpClient(bool autoDestroy = false);
94  ~SmtpClient();
95 
103  bool connect(const Url& url);
104 
116  bool send(const String& from, const String& to, const String& subject, const String& body);
117  bool send(const String& from, const String& to, const String& subject, String&& body) noexcept;
126  bool send(MailMessage* message);
127 
134 
135  size_t countPending()
136  {
137  return mailQ.count();
138  }
139 
143  void quit();
144 
149  {
150  return state;
151  }
152 
158  {
159  messageSentCallback = callback;
160  }
161 
167  {
168  errorCallback = callback;
169  }
170 
171  using TcpClient::getSsl;
173  using TcpClient::setTimeOut;
174 
175 protected:
176  err_t onReceive(pbuf* buf) override;
177  void onReadyToSendData(TcpConnectionEvent sourceEvent) override;
178 
179  void sendMailHeaders(MailMessage* mail);
180  bool sendMailBody(MailMessage* mail);
181 
182 private:
183  Url url;
184  Vector<String> authMethods;
186  char code[4] = {0};
187  int codeValue = 0;
188  String authChallenge;
189  char message[SMTP_ERROR_LENGTH + 1] = {0};
190  bool isLastLine = false;
191  uint8_t codeLength = 0;
192  int options = 0;
193  MailMessage* outgoingMail = nullptr;
194  SmtpState state = eSMTP_Banner;
195 
196  SmtpClientCallback errorCallback = nullptr;
197  SmtpClientCallback messageSentCallback = nullptr;
198 
199 private:
203  int smtpParse(char* data, size_t len);
204 
209  MultipartStream::BodyPart multipartProducer();
210 };
211 
Definition: SmtpClient.h:72
bool sendMailBody(MailMessage *mail)
#define SMTP_ERROR_LENGTH
Definition: SmtpClient.h:43
Definition: SmtpClient.h:62
Definition: SmtpClient.h:74
Definition: SmtpClient.h:75
Class to manage URL instance.
Definition: Url.h:66
Definition: SmtpClient.h:83
Definition: SmtpClient.h:67
void setSslInitHandler(Ssl::Session::InitDelegate handler)
Set the SSL session initialisation callback.
Definition: TcpConnection.h:125
MailMessage * getCurrentMessage()
Gets the current message.
void onServerError(SmtpClientCallback callback)
Callback that will be called every an error occurs.
Definition: SmtpClient.h:166
SmtpClient(bool autoDestroy=false)
unsigned int count() const override
Definition: FIFO.h:37
void onReadyToSendData(TcpConnectionEvent sourceEvent) override
Definition: SmtpClient.h:70
Definition: SmtpClient.h:71
Definition: SmtpClient.h:77
Ssl::Session * getSsl()
Get a pointer to the current SSL session object.
Definition: TcpConnection.h:148
Definition: SmtpClient.h:76
Definition: MailMessage.h:31
The String class.
Definition: WString.h:136
void quit()
Sends a quit command to the server and closes the TCP conneciton.
Definition: SmtpClient.h:80
Definition: TcpClient.h:46
void setTimeOut(uint16_t waitTimeOut)
TcpConnectionEvent
Definition: TcpConnection.h:25
Each result item contains a set of headers plus content stream.
Definition: MultipartStream.h:29
Definition: SmtpClient.h:64
size_t countPending()
Definition: SmtpClient.h:135
void sendMailHeaders(MailMessage *mail)
Definition: SmtpClient.h:73
SmtpState
Definition: SmtpClient.h:61
Definition: SmtpClient.h:66
bool connect(const Url &url)
Connects to remote URL.
Definition: SmtpClient.h:82
Definition: SmtpClient.h:68
Definition: SmtpClient.h:78
Definition: SmtpClient.h:90
Definition: SmtpClient.h:69
void onMessageSent(SmtpClientCallback callback)
Callback that will be called every time a message is sent successfully.
Definition: SmtpClient.h:157
Definition: SmtpClient.h:65
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.
err_t onReceive(pbuf *buf) override
Definition: SmtpClient.h:63
Definition: SmtpClient.h:81
SmtpState getState()
Returns the current state of the SmtpClient.
Definition: SmtpClient.h:148
Definition: SmtpClient.h:79