-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemail.cpp
More file actions
118 lines (101 loc) · 3.84 KB
/
email.cpp
File metadata and controls
118 lines (101 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// This file is part of xmasGifts.
//
// xmasGifts is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// xmasGifts is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with xmasGifts. If not, see <http://www.gnu.org/licenses/>.
//
#include "email.h"
#include <quickmail.h>
#include <iostream>
#include <sstream>
#include "guid.h"
#include "output.h"
namespace
{
std::string_view getEmailAddrDomain(std::string_view const emailAddr)
{
auto posAtChar = emailAddr.find_first_of('@');
if (posAtChar != std::string_view::npos) {
return emailAddr.substr(posAtChar + 1);
} else {
return emailAddr;
}
}
bool validEmailConfig(config::Config const &cfg)
{
return not cfg.getEmailPwd().empty() && not cfg.getEmailSender().empty() &&
not cfg.getEmailUsername().empty() &&
not cfg.getSmtpServer().empty();
}
} // namespace
namespace email
{
constexpr std::string_view msgSubject{"Ho Ho Ho!"};
constexpr std::string_view msgSalutation{"Hallo"};
constexpr std::string_view msgPart1{
"Ich freue mich, dass du mich auch dieses Jahr tatkräftig beim "
"Wichteln unterstützt. Deine Aufgabe bis Weihnachten: ein unvergessliches, "
"grandioses, lustiges und "
"nicht all zu teures Geschenk für"};
constexpr std::string_view msgPart2{
"basteln/kaufen/bestellen/organisieren. "
"Viel Spass und Erfolg!\n\nDeine Familie Schweizer Wichtelfee"};
void sendEmails(std::vector<Person> &giftList, config::Config const &cfg)
{
if (not cfg.useEmails()) {
// sending emails not commanded
return;
} else if (not validEmailConfig(cfg)) {
std::cerr << "Email configuration incomplete, not sending any email."
<< std::endl;
return;
}
std::cout << "Sending emails ";
// giftee iterator points one ahead
auto itGiftee = ++(giftList.begin());
for (auto itDonor = giftList.begin(); itDonor != giftList.end();
++itDonor) {
// wrap around the giftee (which always points one ahead)
if (itGiftee == giftList.end()) {
itGiftee = giftList.begin();
}
quickmail mailobj =
quickmail_create(cfg.getEmailSender().c_str(), msgSubject.data());
quickmail_add_to(mailobj, itDonor->email.value().c_str());
std::ostringstream ss;
ss << msgSalutation << " " << itDonor->name << ",\n\n";
ss << msgPart1 << " " << itGiftee->name << " ";
ss << msgPart2;
quickmail_set_body(mailobj, ss.str().c_str());
// Google needs a GUID in the email header's Message-ID
// (otherwise you have a good chance it's moved to Junk)
ss.str("");
ss.clear();
ss << "Message-ID: <" << guid::getGuidStr8_4_4_4_12() << "@"
<< getEmailAddrDomain(cfg.getEmailSender()) << ">";
quickmail_add_header(mailobj, ss.str().c_str());
constexpr unsigned smtpport{25};
const char *emailSendResult = quickmail_send(
mailobj, cfg.getSmtpServer().c_str(), smtpport,
cfg.getEmailUsername().c_str(), cfg.getEmailPwd().c_str());
if (emailSendResult) {
std::cerr << "Could not send an email to " << itDonor->name << " ("
<< itDonor->email.value() << "): " << emailSendResult
<< std::endl;
} else {
std::cout << ".";
}
++itGiftee;
}
std::cout << " Done." << std::endl;
}
} // namespace email