aboutsummaryrefslogtreecommitdiff
path: root/libbutl/sendmail.mxx
blob: 0d5b23991eb6080a5c1b330de1df0a1ed1c85c52 (plain)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// file      : libbutl/sendmail.mxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#ifndef __cpp_modules_ts
#pragma once
#endif

// C includes.

#ifndef __cpp_lib_modules_ts
#include <string>

#include <cstddef> // size_t
#include <utility> // move(), forward()
#endif

// Other includes.

#ifdef __cpp_modules_ts
export module butl.sendmail;
#ifdef __cpp_lib_modules_ts
import std.core;
#endif
import butl.process;
import butl.fdstream;
import butl.small_vector;
#else
#include <libbutl/process.mxx>
#include <libbutl/fdstream.mxx>
#include <libbutl/small-vector.mxx>
#endif

#include <libbutl/export.hxx>

LIBBUTL_MODEXPORT namespace butl
{
  // Send email using the sendmail(1) program.
  //
  // Write the body of the email to out. Note that you must explicitly close
  // it before calling wait(). Throw process_error and io_error (both derive
  // from system_error) in case of errors.
  //
  // Typical usage:
  //
  // try
  // {
  //   sendmail sm (2,                    // Diagnostics to stderr.
  //                "",                   // Default From: address.
  //                "Test subject",
  //                {"test@example.com"});
  //
  //   sm.out << "Test body" << endl;
  //
  //   sm.out.close ();
  //
  //   if (!sm.wait ())
  //     ... // sendmail returned non-zero status.
  // }
  // catch (const std::system_error& e)
  // {
  //   cerr << "sendmail error: " << e << endl;
  // }
  //
  class LIBBUTL_SYMEXPORT sendmail: public process
  {
  public:
    ofdstream out;

    // Notes:
    //
    // - If from is empty then the process user's address is used.
    //
    // - The to/cc/bcc addressed should already be quoted if required.
    //
    using recipients_type = small_vector<std::string, 1>;

    template <typename E>
    sendmail (E&& err,
              const std::string& from,
              const std::string& subject,
              const recipients_type& to);

    template <typename E>
    sendmail (E&& err,
              const std::string& from,
              const std::string& subject,
              const recipients_type& to,
              const recipients_type& cc);

    template <typename E, typename... O>
    sendmail (E&& err,
              const std::string& from,
              const std::string& subject,
              const recipients_type& to,
              const recipients_type& cc,
              const recipients_type& bcc,
              O&&... options);

    // Version with the command line callback (see process_run_callback() for
    // details).
    //
    template <typename C, typename E>
    sendmail (const C&,
              E&& err,
              const std::string& from,
              const std::string& subject,
              const recipients_type& to);

    template <typename C, typename E>
    sendmail (const C&,
              E&& err,
              const std::string& from,
              const std::string& subject,
              const recipients_type& to,
              const recipients_type& cc);

    template <typename C, typename E, typename... O>
    sendmail (const C&,
              E&& err,
              const std::string& from,
              const std::string& subject,
              const recipients_type& to,
              const recipients_type& cc,
              const recipients_type& bcc,
              O&&... options);

  private:
    void
    headers (const std::string& from,
             const std::string& subj,
             const recipients_type& to,
             const recipients_type& cc,
             const recipients_type& bcc);
  };
}

#include <libbutl/sendmail.ixx>