2 Copyright (C) 2015-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
22 #include "compose.hpp"
25 #include "exceptions.h"
26 #include <curl/curl.h>
27 #include <boost/algorithm/string.hpp>
28 #include <boost/date_time/c_local_time_adjustor.hpp>
36 using std::shared_ptr;
42 Emailer::Emailer(string from, vector<string> to, string subject, string body)
54 Emailer::fix (string s) const
56 boost::algorithm::replace_all (s, "\n", "\r\n");
57 boost::algorithm::replace_all (s, "\0", " ");
63 Emailer::add_cc (string cc)
70 Emailer::add_bcc (string bcc)
77 Emailer::add_attachment (boost::filesystem::path file, string name, string mime_type)
82 a.mime_type = mime_type;
83 _attachments.push_back (a);
88 curl_data_shim (void* ptr, size_t size, size_t nmemb, void* userp)
90 return reinterpret_cast<Emailer*>(userp)->get_data (ptr, size, nmemb);
95 curl_debug_shim (CURL* curl, curl_infotype type, char* data, size_t size, void* userp)
97 return reinterpret_cast<Emailer*>(userp)->debug (curl, type, data, size);
102 Emailer::get_data (void* ptr, size_t size, size_t nmemb)
104 size_t const t = min (_email.length() - _offset, size * nmemb);
105 memcpy (ptr, _email.substr (_offset, t).c_str(), t);
112 Emailer::send (string server, int port, EmailProtocol protocol, string user, string password)
114 char date_buffer[128];
115 time_t now = time (0);
116 strftime (date_buffer, sizeof(date_buffer), "%a, %d %b %Y %H:%M:%S ", localtime(&now));
118 auto const utc_now = boost::posix_time::second_clock::universal_time ();
119 auto const local_now = boost::date_time::c_local_adjustor<boost::posix_time::ptime>::utc_to_local (utc_now);
120 auto offset = local_now - utc_now;
121 sprintf (date_buffer + strlen(date_buffer), "%s%02d%02d", (offset.hours() >= 0 ? "+" : "-"), int(abs(offset.hours())), int(offset.minutes()));
123 _email = "Date: " + string(date_buffer) + "\r\n"
124 "To: " + address_list (_to) + "\r\n"
125 "From: " + _from + "\r\n";
128 _email += "Cc: " + address_list(_cc) + "\r\n";
132 _email += "Bcc: " + address_list(_bcc) + "\r\n";
135 string const chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
137 for (int i = 0; i < 32; ++i) {
138 boundary += chars[rand() % chars.length()];
141 if (!_attachments.empty ()) {
142 _email += "MIME-Version: 1.0\r\n"
143 "Content-Type: multipart/mixed; boundary=" + boundary + "\r\n";
146 _email += "Subject: " + encode_rfc1342(_subject) + "\r\n"
147 "User-Agent: DCP-o-matic\r\n"
150 if (!_attachments.empty ()) {
151 _email += "--" + boundary + "\r\n"
152 + "Content-Type: text/plain; charset=utf-8\r\n\r\n";
157 for (auto i: _attachments) {
158 _email += "\r\n\r\n--" + boundary + "\r\n"
159 "Content-Type: " + i.mime_type + "; name=" + encode_rfc1342(i.name) + "\r\n"
160 "Content-Transfer-Encoding: Base64\r\n"
161 "Content-Disposition: attachment; filename=" + encode_rfc1342(i.name) + "\r\n\r\n";
163 auto b64 = BIO_new (BIO_f_base64());
165 throw std::bad_alloc();
168 auto bio = BIO_new (BIO_s_mem());
170 throw std::bad_alloc();
172 bio = BIO_push (b64, bio);
174 ArrayData data (i.file);
175 BIO_write (bio, data.data(), data.size());
176 (void) BIO_flush (bio);
179 long int bytes = BIO_get_mem_data (bio, &out);
180 _email += fix (string (out, bytes));
185 if (!_attachments.empty ()) {
186 _email += "\r\n--" + boundary + "--\r\n";
189 curl_global_init (CURL_GLOBAL_DEFAULT);
191 auto curl = curl_easy_init ();
193 throw NetworkError ("Could not initialise libcurl");
196 if ((protocol == EmailProtocol::AUTO && port == 465) || protocol == EmailProtocol::SSL) {
197 /* "SSL" or "Implicit TLS"; I think curl wants us to use smtps here */
198 curl_easy_setopt (curl, CURLOPT_URL, String::compose("smtps://%1:%2", server, port).c_str());
200 curl_easy_setopt (curl, CURLOPT_URL, String::compose("smtp://%1:%2", server, port).c_str());
203 if (!user.empty ()) {
204 curl_easy_setopt (curl, CURLOPT_USERNAME, user.c_str ());
206 if (!password.empty ()) {
207 curl_easy_setopt (curl, CURLOPT_PASSWORD, password.c_str());
210 curl_easy_setopt (curl, CURLOPT_MAIL_FROM, _from.c_str());
212 struct curl_slist* recipients = nullptr;
214 recipients = curl_slist_append (recipients, i.c_str());
217 recipients = curl_slist_append (recipients, i.c_str());
220 recipients = curl_slist_append (recipients, i.c_str());
223 curl_easy_setopt (curl, CURLOPT_MAIL_RCPT, recipients);
225 curl_easy_setopt (curl, CURLOPT_READFUNCTION, curl_data_shim);
226 curl_easy_setopt (curl, CURLOPT_READDATA, this);
227 curl_easy_setopt (curl, CURLOPT_UPLOAD, 1L);
229 if (protocol == EmailProtocol::AUTO || protocol == EmailProtocol::STARTTLS) {
230 curl_easy_setopt (curl, CURLOPT_USE_SSL, (long) CURLUSESSL_TRY);
232 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 0L);
233 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 0L);
234 curl_easy_setopt (curl, CURLOPT_VERBOSE, 1L);
235 curl_easy_setopt (curl, CURLOPT_DEBUGFUNCTION, curl_debug_shim);
236 curl_easy_setopt (curl, CURLOPT_DEBUGDATA, this);
238 auto const r = curl_easy_perform (curl);
240 throw NetworkError (_("Failed to send email"), string(curl_easy_strerror(r)));
243 curl_slist_free_all (recipients);
244 curl_easy_cleanup (curl);
245 curl_global_cleanup ();
250 Emailer::address_list(vector<string> addresses)
253 for (auto i: addresses) {
257 return o.substr (0, o.length() - 2);
262 Emailer::debug (CURL *, curl_infotype type, char* data, size_t size)
264 if (type == CURLINFO_TEXT) {
265 _notes += string (data, size);
266 } else if (type == CURLINFO_HEADER_IN) {
267 _notes += "<- " + string (data, size);
268 } else if (type == CURLINFO_HEADER_OUT) {
269 _notes += "-> " + string (data, size);
276 Emailer::encode_rfc1342 (string subject)
278 auto b64 = BIO_new(BIO_f_base64());
280 throw std::bad_alloc();
283 auto bio = BIO_new(BIO_s_mem());
285 throw std::bad_alloc();
288 bio = BIO_push(b64, bio);
289 BIO_write(bio, subject.c_str(), subject.length());
290 (void) BIO_flush(bio);
293 long int bytes = BIO_get_mem_data(bio, &out);
294 string base64_subject(out, bytes);
297 boost::algorithm::replace_all(base64_subject, "\n", "");
298 return "=?utf-8?B?" + base64_subject + "?=";