summaryrefslogtreecommitdiffstats
path: root/email.c
blob: 7374c341be7c4f06b8420e0d12c6ee8966ea0c5c (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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <syslog.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>


#include "util.h"
#include "email.h"



static void write_complete (int fd, const void *_buf, size_t len)
{
  ssize_t writ;
  const char *buf = _buf;

  while (len) {
    writ = write (fd, buf, len);

    if (writ <= 0) return;

    len -= writ;
    buf += writ;
  }
}


static void write_str (int fd, const char *str)
{
  write_complete (fd, str, strlen (str));
}

void  send_email (const char *to, const char *subject, const char *body)
{
  int pipes[2];

  if (daemonish (0, 1)) return;

  pipe (pipes);


  switch (fork()) {
  case 0:
    close (pipes[1]);
    dup2 (pipes[0], 0);
    close (pipes[0]);

    execl ("/usr/lib/sendmail", "sendmail", to, (char *) 0);
    _exit (1);

  case -1:
    _exit (1);
  }

  close (pipes[0]);

  write_str (pipes[1], "Subject: ");
  write_str (pipes[1], subject);
  write_str (pipes[1], "\n\n");
  write_str (pipes[1], body);
  write_str (pipes[1], "\n\n");
  close (pipes[1]);

  _exit (0);
}