Skip to content

Commit b8452aa

Browse files
committed
fix: Error 554 5.0.0: failed to create parser: unexpected EOF. Issue #43
1 parent 4ce35c3 commit b8452aa

File tree

4 files changed

+33
-8
lines changed

4 files changed

+33
-8
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ Session.vim
1616
.vimspector.json
1717
.ccls-cache
1818
.idea
19-
cmake-build-debug
19+
cmake-build-debug
20+
compile_commands.json

src/attachment.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
#include "attachment.h"
2+
#include <algorithm>
23
#include <cstdint>
4+
#include <fstream>
35
#include <ios>
46
#include <iostream>
57
#include <limits>
8+
#include <sstream>
69
#include <string>
10+
#include "base64.h"
711
#include "stringutils.h"
812

913
using namespace jed_utils;
@@ -141,8 +145,10 @@ const char *Attachment::getBase64EncodedFile() const {
141145
in.read(&contents[0], static_cast<std::streamsize>(contents.size()));
142146
in.close();
143147
std::string base64_result = Base64::Encode(reinterpret_cast<const unsigned char*>(contents.c_str()), contents.length());
144-
auto *base64_file = new char[base64_result.length() + 1];
145-
strncpy(base64_file, base64_result.c_str(), base64_result.length() + 1);
148+
// Make sure that the line length are limited to 76 chars (RFC5322)
149+
std::string base64Wrapped = wrap_rfc5322(base64_result);
150+
auto *base64_file = new char[base64Wrapped.length() + 1];
151+
strncpy(base64_file, base64Wrapped.c_str(), base64Wrapped.length() + 1);
146152
return base64_file;
147153
}
148154
in.close();
@@ -328,3 +334,18 @@ const char *Attachment::getMimeType() const {
328334

329335
return "";
330336
}
337+
338+
std::string Attachment::wrap_rfc5322(const std::string& input,
339+
size_t max_line_length) const {
340+
std::ostringstream wrapped;
341+
size_t pos = 0;
342+
343+
while (pos < input.size()) {
344+
size_t len = std::min(max_line_length, input.size() - pos);
345+
wrapped.write(&input[pos], static_cast<int64_t>(len));
346+
wrapped << "\r\n";
347+
pos += len;
348+
}
349+
350+
return wrapped.str();
351+
}

src/attachment.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
#define ATTACHMENT_H
33

44
#include <cstring>
5-
#include <fstream>
6-
#include "base64.h"
5+
#include <string>
76

87
#ifdef _WIN32
98
#ifdef SMTPCLIENT_STATIC
@@ -71,6 +70,8 @@ class ATTACHMENT_API Attachment {
7170
char *mName;
7271
char *mFilename;
7372
char *mContentId;
73+
std::string wrap_rfc5322(const std::string& input,
74+
size_t max_line_length = 76) const;
7475
};
7576
} // namespace jed_utils
7677

src/smtpclientbase.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,6 @@ int SMTPClientBase::setMailBody(const Message &pMsg) {
961961
std::ostringstream body_ss;
962962
body_ss << "--sep\r\nContent-Type: " << pMsg.getMimeType() << "; charset=UTF-8\r\n\r\n" << pMsg.getBody() << "\r\n";
963963
std::string body_real = body_ss.str();
964-
addCommunicationLogItem(body_real.c_str());
965964

966965
// If there's attachments, prepare the attachments text content
967966
Attachment** arr_attachment = pMsg.getAttachments();
@@ -982,18 +981,21 @@ int SMTPClientBase::setMailBody(const Message &pMsg) {
982981
}
983982
int body_part_ret_code = (*this.*sendCommandPtr)(body_real.substr(index_start, length).c_str(), CLIENT_SENDMAIL_BODYPART_ERROR);
984983
if (body_part_ret_code != 0) {
984+
addCommunicationLogItem(body_real.c_str());
985985
return body_part_ret_code;
986986
}
987987
}
988988
} else {
989989
int body_ret_code = (*this.*sendCommandPtr)(body_real.c_str(), CLIENT_SENDMAIL_BODY_ERROR);
990990
if (body_ret_code != 0) {
991+
addCommunicationLogItem(body_real.c_str());
991992
return body_ret_code;
992993
}
993994
}
995+
addCommunicationLogItem(body_real.c_str());
994996

995997
// End of data
996-
std::string end_data_command { "\r\n.\r\n" };
998+
std::string end_data_command { "\r\n--sep--\r\n.\r\n" };
997999
addCommunicationLogItem(end_data_command.c_str());
9981000
int end_data_ret_code = (*this.*sendCommandWithFeedbackPtr)(end_data_command.c_str(), CLIENT_SENDMAIL_END_DATA_ERROR, CLIENT_SENDMAIL_END_DATA_TIMEOUT);
9991001
if (end_data_ret_code != STATUS_CODE_REQUESTED_MAIL_ACTION_OK_OR_COMPLETED) {
@@ -1061,7 +1063,7 @@ std::string SMTPClientBase::createAttachmentsText(const std::vector<Attachment*>
10611063
delete[] b64;
10621064
}
10631065
}
1064-
retval += "\r\n--sep--";
1066+
retval += "\r\n";
10651067
return retval;
10661068
}
10671069

0 commit comments

Comments
 (0)