accept more TLS versions, ignore zero return
[ghsmtp.git] / jnk / Send-test.cpp
blob1b2b24ee7f390b4a3c0bed336eeabc9c659bdc4e
1 #include "Send.hpp"
3 #include "Now.hpp"
4 #include "Pill.hpp"
5 #include "osutil.hpp"
7 #include <fmt/format.h>
8 #include <fmt/ostream.h>
10 using namespace std::string_literals;
12 int main(int argc, char* argv[])
14 std::ios::sync_with_stdio(false);
15 google::ParseCommandLineFlags(&argc, &argv, true);
17 auto server_identity = [] {
18 auto const id_from_env{getenv("GHSMTP_SERVER_ID")};
19 if (id_from_env)
20 return std::string{id_from_env};
22 auto const hostname{osutil::get_hostname()};
23 if (hostname.find('.') != std::string::npos)
24 return hostname;
26 LOG(FATAL) << "can't determine my server ID, set GHSMTP_SERVER_ID maybe";
27 return "(none)"s;
28 }();
30 auto const dom_from{Domain(server_identity)};
31 auto const dom_to{Domain(server_identity)};
33 auto const config_path = osutil::get_config_dir();
35 auto snd{Send(config_path, "smtp-test")};
36 snd.set_sender(Domain(server_identity));
38 Mailbox from("gene", dom_from);
39 // Mailbox to("forward", dom_to);
40 Mailbox to("anything", dom_to);
42 auto const date{Now{}};
43 auto const pill{Pill{}};
44 auto const mid_str =
45 fmt::format("<{}.{}@{}>", date.sec(), pill, server_identity);
47 fmt::memory_buffer bfr;
48 fmt::format_to(bfr, "Message-ID: {}\r\n", mid_str.c_str());
49 fmt::format_to(bfr, "From: \"Gene Hightower\" <{}>\r\n",
50 from.as_string(Mailbox::domain_encoding::utf8));
51 fmt::format_to(bfr, "To: \"Gene Hightower\" <{}>\r\n",
52 to.as_string(Mailbox::domain_encoding::utf8));
53 fmt::format_to(bfr, "Subject: Testing, one, two, three.\r\n");
54 fmt::format_to(bfr, "Date: {}\r\n", date.c_str());
55 fmt::format_to(bfr, "Authentication-Results: {}; none\r\n", server_identity);
56 fmt::format_to(bfr, "MIME-Version: 1.0\r\n");
57 fmt::format_to(bfr, "Content-Type: text/plain; charset=utf-8\r\n");
59 fmt::format_to(bfr, "\r\n");
61 fmt::format_to(bfr, "This is the body of the email.\r\n");
62 auto const msg_str = fmt::to_string(bfr);
64 auto res{DNS::Resolver{config_path}};
66 CHECK(snd.mail_from(from));
67 std::string err;
68 CHECK(snd.rcpt_to(res, to, err));
69 CHECK(snd.send(msg_str));
70 snd.quit();