From 55a43ed40d407f7eb778d38e2f31e1e127de3879 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Sun, 24 Jul 2022 20:04:14 -0700 Subject: [PATCH] https://telecominfraproject.atlassian.net/browse/WIFI-10345 Signed-off-by: stephb9959 --- build | 2 +- src/SMTPMailerService.cpp | 32 +++++++++++++++++++++++--------- src/SMTPMailerService.h | 8 +++++++- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/build b/build index d8263ee..bf0d87a 100644 --- a/build +++ b/build @@ -1 +1 @@ -2 \ No newline at end of file +4 \ No newline at end of file diff --git a/src/SMTPMailerService.cpp b/src/SMTPMailerService.cpp index d5756d2..1be61b3 100644 --- a/src/SMTPMailerService.cpp +++ b/src/SMTPMailerService.cpp @@ -12,6 +12,7 @@ #include "Poco/Exception.h" #include "Poco/Net/SSLManager.h" #include "Poco/Net/Context.h" +#include "Poco/Net/NetException.h" #include "SMTPMailerService.h" #include "framework/MicroService.h" @@ -83,12 +84,20 @@ namespace OpenWifi { auto Recipient = i->Attrs.find(RECIPIENT_EMAIL)->second; uint64_t now = OpenWifi::Now(); if((i->LastTry==0 || (now-i->LastTry)>MailRetry_)) { - if (SendIt(*i)) { - Logger().information(fmt::format("Attempting to deliver for mail '{}'.", Recipient)); - i = Messages_.erase(i); - } else { - i->LastTry = now; - ++i; + switch(SendIt(*i)) { + case MessageSendStatus::msg_sent: { + Logger().information(fmt::format("Attempting to deliver for mail '{}'.", Recipient)); + i = Messages_.erase(i); + } break; + case MessageSendStatus::msg_not_sent_but_resend: { + Logger().information(fmt::format("Mail for '{}' was not. We will retry later.", Recipient)); + i->LastTry = now; + ++i; + } break; + case MessageSendStatus::msg_not_sent_but_do_not_resend: { + Logger().information(fmt::format("Mail for '{}' will not be sent. Check email address", Recipient)); + i = Messages_.erase(i); + } break; } } else if ((now-i->Posted)>MailAbandon_) { Logger().information(fmt::format("Mail for '{}' has timed out and will not be sent.", Recipient)); @@ -106,7 +115,7 @@ namespace OpenWifi { } } - bool SMTPMailerService::SendIt(const MessageEvent &Msg) { + MessageSendStatus SMTPMailerService::SendIt(const MessageEvent &Msg) { std::string Recipient; try @@ -171,16 +180,21 @@ namespace OpenWifi { ); session.sendMessage(Message); session.close(); - return true; + return MessageSendStatus::msg_sent; + } + catch (const Poco::Net::SMTPException &S) { + Logger().log(S); + return MessageSendStatus::msg_not_sent_but_do_not_resend; } catch (const Poco::Exception& E) { Logger().log(E); + return MessageSendStatus::msg_not_sent_but_resend; } catch (const std::exception &E) { Logger().warning(fmt::format("Cannot send message to:{}, error: {}",Recipient, E.what())); + return MessageSendStatus::msg_not_sent_but_do_not_resend; } - return false; } } diff --git a/src/SMTPMailerService.h b/src/SMTPMailerService.h index c564156..b765a2a 100644 --- a/src/SMTPMailerService.h +++ b/src/SMTPMailerService.h @@ -56,6 +56,12 @@ namespace OpenWifi { } typedef std::map MessageAttributes; + enum class MessageSendStatus { + msg_sent, + msg_not_sent_but_resend, + msg_not_sent_but_do_not_resend + }; + class SMTPMailerService : public SubSystemServer, Poco::Runnable { public: static SMTPMailerService *instance() { @@ -76,7 +82,7 @@ namespace OpenWifi { void Stop() override; bool SendMessage(const std::string &Recipient, const std::string &Name, const MessageAttributes &Attrs); - bool SendIt(const MessageEvent &Msg); + MessageSendStatus SendIt(const MessageEvent &Msg); void LoadMyConfig(); void reinitialize(Poco::Util::Application &self) override; bool Enabled() const { return Enabled_; }