summaryrefslogtreecommitdiff
path: root/src/build_email.php
blob: 6881acce703480f946f24b75ce22b34cd6372c21 (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php

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

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


EOF;

    $text = $edit_notice . $text;
  }

  return $text;
}

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

_______________________________________________
Read this topic online here:
$postURL
EOF;

  return $footer;
}

function build_email_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_email_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_email_attachment(
        $mime,
        $a['mimetype'],
        $a['real_filename'],
        $a['attach_comment'],
        $bytes
      );
    }

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

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

  return $body;
}

function build_email_from($name, $email) {
  $qname = ''; 

  if (is_ascii($name)) {
    if (has_rfc822_specials($name)) {
      $qname = rfc822_quote($name);
    }
    else {
      $qname = $name;
    }
  }
  else {
    // base64-encode if we have non-ASCII chars
    $qname = utf8_quote($name);
  }

  return sprintf('%s <%s>', $qname, $email);
}

function build_email_subject($forumtag, $subject, $reply, $edit) {
  if ($reply) {
    // strip leading sequences of Re-equivalents
    $re = '/^(?:(?:RE|AW|SV|VS)(?:\\[\\d+\\])?:\\s*)+/i';
    if (preg_match($re, $subject, $m)) {
      $subject = substr($subject, strlen($m[0]));
    }
  }

  // ensure nonempty subject
  $subject = trim($subject);
  if ($subject == '') {
    $subject = '(no subject)';
  }

  $subject = $forumtag . ' ' . $subject;

  if ($reply) {
    $subject = 'Re: ' . $subject;
  }

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

  return utf8_quote_non_ascii($subject);
}

function build_email_headers(
  $userName, $userEmail, $to, $sender, $subject, $time,
  $messageId, $forumURL, $inReplyTo, $references)
{
  $from = build_email_from($userName, $userEmail); 
  $subject = utf8_quote_non_ascii($subject);
  $date = date(DATE_RFC2822, $time);

  $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_email_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_email_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_email_message_id($postId, $editId, $time, $forumHost) {
  return "<$time.$postId.$editId.bridge@$forumHost>";
}

?>