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 
115  bool send(const String& from, const String& to, const String& subject, const String& body);
116 
123  bool send(MailMessage* message);
124 
131 
132  size_t countPending()
133  {
134  return mailQ.count();
135  }
136 
140  void quit();
141 
146  {
147  return state;
148  }
149 
155  {
156  messageSentCallback = callback;
157  }
158 
164  {
165  errorCallback = callback;
166  }
167 
168  using TcpClient::getSsl;
170  using TcpClient::setTimeOut;
171 
172 protected:
173  err_t onReceive(pbuf* buf) override;
174  void onReadyToSendData(TcpConnectionEvent sourceEvent) override;
175 
176  void sendMailHeaders(MailMessage* mail);
177  bool sendMailBody(MailMessage* mail);
178 
179 private:
180  Url url;
181  Vector<String> authMethods;
183  char code[4] = {0};
184  int codeValue = 0;
185  String authChallenge;
186  char message[SMTP_ERROR_LENGTH + 1] = {0};
187  bool isLastLine = false;
188  uint8_t codeLength = 0;
189  int options = 0;
190  MailMessage* outgoingMail = nullptr;
191  SmtpState state = eSMTP_Banner;
192 
193  SmtpClientCallback errorCallback = nullptr;
194  SmtpClientCallback messageSentCallback = nullptr;
195 
196 private:
200  int smtpParse(char* data, size_t len);
201 
206  HttpPartResult multipartProducer();
207 };
208 
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:112
MailMessage * getCurrentMessage()
Gets the current message.
void onServerError(SmtpClientCallback callback)
Callback that will be called every an error occurs.
Definition: SmtpClient.h:163
SmtpClient(bool autoDestroy=false)
unsigned int count() const override
Definition: FIFO.h:38
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:135
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
Definition: SmtpClient.h:64
size_t countPending()
Definition: SmtpClient.h:132
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:154
Definition: SmtpClient.h:65
Delegate< int(SmtpClient &client, int code, char *status)> SmtpClientCallback
Definition: SmtpClient.h:86
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.
Multipart stream class.
Definition: MultipartStream.h:25
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:145
Definition: SmtpClient.h:79