Fix lack of email sending if there are no cinema addresses (only extra ones) (#2514).
[dcpomatic.git] / src / lib / kdm_with_metadata.cc
index bebb5df86d0b44f6af12a2c5f371d2e2620eed6e..5cbadc911a0dfba290dec7661f575688ad68e0c5 100644 (file)
@@ -153,8 +153,8 @@ write_directories (
                if (!boost::filesystem::exists (path) || confirm_overwrite (path)) {
                        boost::filesystem::create_directories (path);
                        write_files(kdm, path, filename_format, confirm_overwrite);
+                       written += kdm.size();
                }
-               written += kdm.size();
        }
 
        return written;
@@ -220,10 +220,6 @@ send_emails (
 
                auto first = kdms_for_cinema.front();
 
-               if (first->emails().empty()) {
-                       continue;
-               }
-
                auto zip_file = boost::filesystem::temp_directory_path() / boost::filesystem::unique_path();
                boost::filesystem::create_directories (zip_file);
                zip_file /= container_name_format.get(first->name_values(), ".zip");
@@ -250,7 +246,18 @@ send_emails (
                }
                boost::algorithm::replace_all (body, "$SCREENS", screens.substr (0, screens.length() - 2));
 
-               Emailer email (config->kdm_from(), first->emails(), subject, body);
+               auto emails = first->emails();
+               std::copy(extra_addresses.begin(), extra_addresses.end(), std::back_inserter(emails));
+               if (emails.empty()) {
+                       continue;
+               }
+
+               Emailer email (config->kdm_from(), { emails.front() }, subject, body);
+
+               /* Use CC for the second and subsequent email addresses, so we seem less spammy (#2310) */
+               for (auto cc = std::next(emails.begin()); cc != emails.end(); ++cc) {
+                       email.add_cc(*cc);
+               }
 
                for (auto cc: config->kdm_cc()) {
                        email.add_cc (cc);
@@ -278,21 +285,6 @@ send_emails (
 
                log_details (email);
 
-               for (auto extra: extra_addresses) {
-                       Emailer email (config->kdm_from(), { extra }, subject, body);
-                       email.add_attachment (zip_file, container_name_format.get(first->name_values(), ".zip"), "application/zip");
-
-                       try {
-                               email.send (config->mail_server(), config->mail_port(), config->mail_protocol(), config->mail_user(), config->mail_password());
-                       } catch (...) {
-                               boost::filesystem::remove (zip_file);
-                               log_details (email);
-                               throw;
-                       }
-
-                       log_details (email);
-               }
-
                boost::filesystem::remove (zip_file);
        }
 }