From e9167bc88529bf5eb71bdefa4e69fdbac790a391 Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Sun, 16 Feb 2025 00:52:53 +0100 Subject: Add retry for email sending (#2963). --- src/lib/email.cc | 20 ++++++++++++++++++++ src/lib/email.h | 1 + src/lib/kdm_with_metadata.cc | 2 +- 3 files changed, 22 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/lib/email.cc b/src/lib/email.cc index 028624c69..f0ff9f910 100644 --- a/src/lib/email.cc +++ b/src/lib/email.cc @@ -21,6 +21,7 @@ #include "compose.hpp" #include "config.h" +#include "dcpomatic_log.h" #include "email.h" #include "exceptions.h" #include "util.h" @@ -109,6 +110,25 @@ Email::get_data(void* ptr, size_t size, size_t nmemb) } +void +Email::send_with_retry(string server, int port, EmailProtocol protocol, int retries, string user, string password) +{ + int this_try = 0; + while (true) { + try { + send(server, port, protocol, user, password); + return; + } catch (NetworkError& e) { + LOG_ERROR("Error %1 when trying to send email on attempt %2 of 3", e.what(), this_try + 1, retries); + if (this_try == (retries - 1)) { + throw; + } + } + ++this_try; + } +} + + void Email::send(string server, int port, EmailProtocol protocol, string user, string password) { diff --git a/src/lib/email.h b/src/lib/email.h index ac4703453..0ef68379b 100644 --- a/src/lib/email.h +++ b/src/lib/email.h @@ -35,6 +35,7 @@ public: void add_attachment (boost::filesystem::path file, std::string name, std::string mime_type); void send (std::string server, int port, EmailProtocol protocol, std::string user = "", std::string password = ""); + void send_with_retry(std::string server, int port, EmailProtocol protocol, int retries, std::string user = "", std::string password = ""); std::string notes () const { return _notes; diff --git a/src/lib/kdm_with_metadata.cc b/src/lib/kdm_with_metadata.cc index d09f6930b..b6e027ef3 100644 --- a/src/lib/kdm_with_metadata.cc +++ b/src/lib/kdm_with_metadata.cc @@ -277,7 +277,7 @@ send_emails( }; try { - email.send(config->mail_server(), config->mail_port(), config->mail_protocol(), config->mail_user(), config->mail_password()); + email.send_with_retry(config->mail_server(), config->mail_port(), config->mail_protocol(), 5, config->mail_user(), config->mail_password()); } catch (...) { log_details(email); throw; -- cgit v1.2.3