2 Copyright (C) 2015 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/>.
21 #include "compose.hpp"
24 #include "exceptions.h"
25 #include <curl/curl.h>
26 #include <boost/algorithm/string.hpp>
27 #include <boost/date_time/c_local_time_adjustor.hpp>
28 #include <boost/foreach.hpp>
37 using boost::shared_ptr;
40 Emailer::Emailer (string from, list<string> to, string subject, string body)
47 boost::algorithm::replace_all (_body, "\n", "\r\n");
48 boost::algorithm::replace_all (_body, "\0", " ");
52 Emailer::add_cc (string cc)
58 Emailer::add_bcc (string bcc)
64 Emailer::add_attachment (boost::filesystem::path file, string name, string mime_type)
69 a.mime_type = mime_type;
70 _attachments.push_back (a);
74 curl_data_shim (void* ptr, size_t size, size_t nmemb, void* userp)
76 return reinterpret_cast<Emailer*>(userp)->get_data (ptr, size, nmemb);
80 curl_debug_shim (CURL* curl, curl_infotype type, char* data, size_t size, void* userp)
82 return reinterpret_cast<Emailer*>(userp)->debug (curl, type, data, size);
86 Emailer::get_data (void* ptr, size_t size, size_t nmemb)
88 size_t const t = min (_email.length() - _offset, size * nmemb);
89 memcpy (ptr, _email.substr (_offset, t).c_str(), t);
95 Emailer::send (string server, int port, string user, string password)
98 time_t now = time (0);
99 strftime (date_buffer, sizeof(date_buffer), "%a, %d %b %Y %H:%M:%S ", localtime (&now));
101 boost::posix_time::ptime const utc_now = boost::posix_time::second_clock::universal_time ();
102 boost::posix_time::ptime const local_now = boost::date_time::c_local_adjustor<boost::posix_time::ptime>::utc_to_local (utc_now);
103 boost::posix_time::time_duration offset = local_now - utc_now;
104 sprintf (date_buffer + strlen(date_buffer), "%s%02d%02d", (offset.hours() >= 0 ? "+" : "-"), abs (offset.hours()), offset.minutes());
106 SafeStringStream email;
108 email << "Date: " << date_buffer << "\r\n"
109 << "To: " << address_list (_to) << "\r\n"
110 << "From: " << _from << "\r\n";
113 email << "Cc: " << address_list (_cc) << "\r\n";
116 if (!_bcc.empty ()) {
117 email << "Bcc: " << address_list (_bcc) << "\r\n";
120 string const chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
122 for (int i = 0; i < 32; ++i) {
123 boundary += chars[rand() % chars.length()];
126 if (!_attachments.empty ()) {
127 email << "MIME-Version: 1.0\r\n"
128 << "Content-Type: multipart/mixed; boundary=" << boundary << "\r\n";
131 email << "Subject: " << _subject << "\r\n"
132 << "User-Agent: DCP-o-matic\r\n"
135 if (!_attachments.empty ()) {
136 email << "--" << boundary << "\r\n"
137 << "Content-Type: text/plain; charset=utf-8\r\n\r\n";
142 BOOST_FOREACH (Attachment i, _attachments) {
143 email << "\r\n\r\n--" << boundary << "\r\n"
144 << "Content-Type: " << i.mime_type << "; name=" << i.name << "\r\n"
145 << "Content-Transfer-Encoding: Base64\r\n"
146 << "Content-Disposition: attachment; filename=" << i.name << "\r\n\r\n";
148 BIO* b64 = BIO_new (BIO_f_base64());
150 BIO* bio = BIO_new (BIO_s_mem());
151 bio = BIO_push (b64, bio);
154 BIO_write (bio, data.data().get(), data.size());
155 (void) BIO_flush (bio);
158 long int bytes = BIO_get_mem_data (bio, &out);
159 email << string (out, bytes);
164 if (!_attachments.empty ()) {
165 email << "\r\n--" << boundary << "--\r\n";
168 _email = email.str ();
170 curl_global_init (CURL_GLOBAL_DEFAULT);
172 CURL* curl = curl_easy_init ();
174 throw NetworkError ("Could not initialise libcurl");
177 curl_easy_setopt (curl, CURLOPT_URL, String::compose ("smtp://%1:%2", server.c_str(), port).c_str());
179 if (!user.empty ()) {
180 curl_easy_setopt (curl, CURLOPT_USERNAME, user.c_str ());
182 if (!password.empty ()) {
183 curl_easy_setopt (curl, CURLOPT_PASSWORD, password.c_str());
186 curl_easy_setopt (curl, CURLOPT_MAIL_FROM, _from.c_str());
188 struct curl_slist* recipients = 0;
189 BOOST_FOREACH (string i, _to) {
190 recipients = curl_slist_append (recipients, i.c_str());
192 BOOST_FOREACH (string i, _cc) {
193 recipients = curl_slist_append (recipients, i.c_str());
195 BOOST_FOREACH (string i, _bcc) {
196 recipients = curl_slist_append (recipients, i.c_str());
199 curl_easy_setopt (curl, CURLOPT_MAIL_RCPT, recipients);
201 curl_easy_setopt (curl, CURLOPT_READFUNCTION, curl_data_shim);
202 curl_easy_setopt (curl, CURLOPT_READDATA, this);
203 curl_easy_setopt (curl, CURLOPT_UPLOAD, 1L);
205 curl_easy_setopt (curl, CURLOPT_USE_SSL, (long) CURLUSESSL_TRY);
206 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 0L);
207 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 0L);
208 curl_easy_setopt (curl, CURLOPT_VERBOSE, 1L);
209 curl_easy_setopt (curl, CURLOPT_DEBUGFUNCTION, curl_debug_shim);
210 curl_easy_setopt (curl, CURLOPT_DEBUGDATA, this);
212 CURLcode const r = curl_easy_perform (curl);
214 throw KDMError (String::compose (_("Failed to send email (%1)"), curl_easy_strerror (r)));
217 curl_slist_free_all (recipients);
218 curl_easy_cleanup (curl);
219 curl_global_cleanup ();
223 Emailer::address_list (list<string> addresses)
226 BOOST_FOREACH (string i, addresses) {
230 return o.substr (0, o.length() - 2);
234 Emailer::debug (CURL *, curl_infotype type, char* data, size_t size)
236 if (type == CURLINFO_TEXT) {
237 _notes += string (data, size);
238 } else if (type == CURLINFO_HEADER_IN) {
239 _notes += "<- " + string (data, size);
240 } else if (type == CURLINFO_HEADER_OUT) {
241 _notes += "-> " + string (data, size);