summaryrefslogtreecommitdiff
path: root/src/build_email.php
blob: 530110615a46f3938dd56cf7689c568e70b8e59d (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
138
139
140
<?php

require_once(__DIR__ . '/Util.php');

function build_text($text, $edit) {
  if ($edit) {
    $edit_notice = <<<EOF
[This message has been edited.]


EOF;

    $text = $edit_notice . $text;
  }

  return $text;
}

function build_footer($postId, $forumURL) {
  $postURL = "$forumURL/viewtopic.php?p=$postId#p$postId";
  $footer = <<<EOF

_______________________________________________
Read this topic online here:
$postURL
EOF;

  return $footer;
}

function build_body(array &$headers, $text, $attachments, $footer) {
  $body = null;

  # Handle attachements, if any
  if (empty($attachments)) {
    # No attachments, send a plain email
    $body = $text . "\n" . $footer;
    $headers['Content-Type'] = 'text/plain; charset=UTF-8; format=flowed';
    $headers['Content-Transfer-Encoding'] = '8bit';
  }
  else {
    # Attachments, build a MIME email
    require_once('Mail/mimePart.php');

    $headers['MIME-Version'] = '1.0';

    $params = array('content_type' => 'multipart/mixed');
    $mime = new Mail_mimePart('', $params);

    # Build the main body
    build_text_part($mime, $text);

    # Build each attachment
    foreach ($attachments as $a) {
      $bytes = file_get_contents($a['path']);
      if ($bytes === false) {
        throw new Exception('failed to read file: ' . $a['path']);
      }

      build_attachment(
        $mime,
        $adata['mimetype'],
        $adata['real_filename'],
        $adata['attach_comment'],
        $bytes
      );
    }

    # Build footer
    build_text_part($mime, $footer);

    # Encode the message
    $msg = $mime->encode();
    $headers = array_merge($headers, $msg['headers']);
    $body = $msg['body'];
  }

  return $body;
}

function build_headers($userName, $userEmail, $to, $sender, $subject, $edit,
                       $time, $messageId, $forumURL, $inReplyTo, $references) {

  $from = sprintf('%s <%s>', utf8_quote_non_ascii($userName), $userEmail);
  $subject = utf8_quote_non_ascii($subject);
  $date = date(DATE_RFC2822, $time);

  if ($edit) {
    $edit_header = 'Edit: ';
    $subject = $edit_header . $subject;
  }

  $headers = array(
    'To'          => $to,
    'From'        => $from,
    'Sender'      => $sender,
    'Subject'     => $subject,
    'Date'        => $date,
    'Message-ID'  => $messageId,
    'X-BeenThere' => $forumURL
  );

  if ($inReplyTo !== null) {
    $headers['In-Reply-To'] = $inReplyTo;
  }
  
  if ($references !== null) {
    $headers['References'] = $references;
  }

  return $headers;
}

function build_text_part(Mail_mimePart $mime, $text) {
  $params = array(
    'content_type' => 'text/plain',
    'charset'      => 'utf-8',
    'encoding'     => '8bit',
    'disposition'  => 'inline'
  );
  $mime->addSubPart($text, $params);
}

function build_attachment(Mail_mimePart $mime, $type,
                          $filename, $descr, $data) {
  $params = array( 
    'content_type' => $type,
    'encoding'     => 'base64',
    'disposition'  => 'attachment',
    'dfilename'    => $filename,
    'description'  => $descr
  );
  $mime->addSubPart($data, $params);
}

function build_message_id($postId, $editId, $time, $forumHost) {
  return "<$time.$postId.$editId.bridge@$forumHost>";
}

?>