aboutsummaryrefslogtreecommitdiff
path: root/libbutl/sendmail.ixx
blob: 105c1af96e5840c144006faf9f3380511748baa6 (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
// file      : libbutl/sendmail.ixx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

LIBBUTL_MODEXPORT namespace butl //@@ MOD Clang needs this for some reason.
{
  template <typename E, typename... O>
  inline sendmail::
  sendmail (E&& err,
            const std::string& from,
            const std::string& subj,
            const recipients_type& to,
            const recipients_type& cc,
            const recipients_type& bcc,
            O&&... options)
      : sendmail ([] (const char* [], std::size_t) {},
                  std::forward<E> (err),
                  from,
                  subj,
                  to,
                  cc,
                  bcc,
                  std::forward<O> (options)...)
  {
  }

  template <typename E>
  inline sendmail::
  sendmail (E&& err,
            const std::string& from,
            const std::string& subj,
            const recipients_type& to,
            const recipients_type& cc)
      : sendmail (err, from, subj, to, cc, recipients_type ())
  {
  }

  template <typename E>
  inline sendmail::
  sendmail (E&& err,
            const std::string& from,
            const std::string& subj,
            const recipients_type& to)
      : sendmail (err, from, subj, to, recipients_type ())
  {
  }

  template <typename C, typename E, typename... O>
  inline sendmail::
  sendmail (const C& cmdc,
            E&& err,
            const std::string& from,
            const std::string& subj,
            const recipients_type& to,
            const recipients_type& cc,
            const recipients_type& bcc,
            O&&... options)
  {
    fdpipe pipe (fdopen_pipe ()); // Text mode seems appropriate.

    process& p (*this);
    p = process_start_callback (cmdc,
                                pipe,
                                2, // No output expected so redirect to stderr.
                                std::forward<E> (err),
                                "sendmail",
                                "-i", // Don't treat '.' as the end of input.
                                "-t", // Read recipients from headers.
                                std::forward<O> (options)...);

    // Close the reading end of the pipe not to block on writing if sendmail
    // terminates before that.
    //
    pipe.in.close ();

    out.open (std::move (pipe.out));

    // Write headers.
    //
    // Note that if this throws, then the ofdstream will be closed first
    // (which should signal to the process we are done). Then the process's
    // destructor will wait for its termination ignoring any errors.
    //
    headers (from, subj, to, cc, bcc);
  }

  template <typename C, typename E>
  inline sendmail::
  sendmail (const C& cmdc,
            E&& err,
            const std::string& from,
            const std::string& subj,
            const recipients_type& to,
            const recipients_type& cc)
      : sendmail (cmdc, err, from, subj, to, cc, recipients_type ())
  {
  }

  template <typename C, typename E>
  inline sendmail::
  sendmail (const C& cmdc,
            E&& err,
            const std::string& from,
            const std::string& subj,
            const recipients_type& to)
      : sendmail (cmdc, err, from, subj, to, recipients_type ())
  {
  }
}