source: trunk/Packages/Common/Mail.php

Last change on this file was 63, checked in by chronos, 3 years ago
  • Modified: Used explicit types where possible for better error reporting.
  • Modified: Updated Common packaged to newer version.
  • Modified: Simplified pages title.
  • Added: Simple keyword based spam filter for meet items.
File size: 7.7 KB
Line 
1<?php
2
3define('DISPOSITION_INLINE', 'inline');
4define('DISPOSITION_ATTACHMENT', 'attachment');
5
6define('CHARSET_ASCII', 'us-ascii');
7define('CHARSET_UTF8', 'utf-8');
8
9class Mail
10{
11 public string $Subject;
12 public string $From;
13 public array $Recipients;
14 public array $Bodies;
15 public array $Attachments;
16 public string $AgentIdent;
17 public string $Organization;
18 public string $ReplyTo;
19 public array $Headers;
20 private array $Priorities;
21 private string $Boundary;
22
23 function __construct()
24 {
25 $this->Priorities = array('1 (Highest)', '2 (High)', '3 (Normal)', '4 (Low)', '5 (Lowest)');
26 $this->Boundary = md5(date('r', time()));
27 $this->AgentIdent = 'PHP/Mail';
28 $this->Clear();
29 }
30
31 function Clear(): void
32 {
33 $this->Bodies = array();
34 $this->Attachments = array();
35 $this->Recipients = array();
36 $this->SenderAddress = '';
37 $this->SenderName = '';
38 $this->Subject = '';
39 $this->Organization = '';
40 $this->ReplyTo = '';
41 $this->Headers = array();
42 }
43
44 function AddToCombined(string $Address): void
45 {
46 $this->Recipients[] = array('Address' => $Address, 'Type' => 'SendCombined');
47 }
48
49 function AddTo(string $Address, string $Name): void
50 {
51 $this->Recipients[] = array('Address' => $Address, 'Name' => $Name, 'Type' => 'Send');
52 }
53
54 function AddCc(string $Address, string $Name): void
55 {
56 $this->Recipients[] = array('Address' => $Address, 'Name' => $Name, 'Type' => 'Copy');
57 }
58
59 function AddBcc(string $Address, string $Name): void
60 {
61 $this->Recipients[] = array('Address' => $Address, 'Name' => $Name, 'Type' => 'HiddenCopy');
62 }
63
64 function AddBody(string $Content, string $MimeType = 'text/plain', string $Charset = 'utf-8'): void
65 {
66 $this->Bodies[] = array('Content' => $Content, 'Type' => strtolower($MimeType),
67 'Charset' => strtolower($Charset));
68 }
69
70 function Organization(string $org): void
71 {
72 if (trim($org != '')) $this->xheaders['Organization'] = $org;
73 }
74
75 function Priority(int $Priority): bool
76 {
77 if (!intval($Priority)) return false;
78
79 if (!isset($this->Priorities[$Priority - 1])) return false;
80
81 $this->xheaders['X-Priority'] = $this->Priorities[$Priority - 1];
82 return true;
83 }
84
85 function AttachFile($FileName, $FileType, $Disposition = DISPOSITION_ATTACHMENT): void
86 {
87 $this->Attachments[] = array('FileName' => $FileName, 'FileType' => $FileType,
88 'Disposition' => $Disposition, 'Type' => 'File');
89 }
90
91 function AttachData($FileName, $FileType, $Data, $Disposition = DISPOSITION_ATTACHMENT): void
92 {
93 $this->Attachments[] = array('FileName' => $FileName, 'FileType' => $FileType,
94 'Disposition' => $Disposition, 'Type' => 'Data', 'Data' => $Data);
95 }
96
97 function Send(): bool
98 {
99 if (count($this->Bodies) == 0) throw new Exception(T('Mail message need at least one text body'));
100
101 $Body = $this->BuildAttachment($this->BuildBody());
102
103 $To = '';
104 $this->Headers['CC'] = '';
105 $this->Headers['BCC'] = '';
106 foreach ($this->Recipients as $Index => $Recipient)
107 {
108 if ($Recipient['Type'] == 'SendCombined')
109 {
110 if ($Index > 0) $To .= ', ';
111 $To .= $Recipient['Address'];
112 } else
113 if ($Recipient['Type'] == 'Send')
114 {
115 if ($Index > 0) $To .= ', ';
116 $To .= $Recipient['Name'].' <'.$Recipient['Address'].'>';
117 } else
118 if ($Recipient['Type'] == 'Copy')
119 {
120 if ($Index > 0) $this->Headers['CC'] .= ', ';
121 $this->Headers['CC'] .= $Recipient['Name'].' <'.$To['Address'].'>';
122 } else
123 if ($Recipient['Type'] == 'HiddenCopy')
124 {
125 if ($Index > 0) $this->Headers['BCC'] .= ', ';
126 $this->Headers['BCC'] .= $Recipient['Name'].' <'.$To['Address'].'>';
127 }
128 }
129 if ($To == '') throw new Exception(T('Mail message need at least one recipient address'));
130
131 $this->Headers['Mime-Version'] = '1.0';
132
133 if ($this->AgentIdent != '') $this->Headers['X-Mailer'] = $this->AgentIdent;
134 if ($this->ReplyTo != '') $this->Headers['Reply-To'] = $this->ReplyTo;
135 if ($this->From != '') $this->Headers['From'] = $this->From;
136
137 $Headers = '';
138 foreach ($this->Headers as $Header => $Value)
139 {
140 if (($Header != 'Subject') and ($Value != '')) $Headers .= $Header.': '.$Value."\n";
141 }
142
143 $this->Subject = strtr($this->Subject, "\r\n", ' ');
144
145 if ($this->Subject == '') throw new Exception(T('Mail message missing Subject'));
146
147 $res = mail($To, $this->Subject, $Body, $Headers);
148 return $res;
149 }
150
151 function ValidEmail(string $Address): bool
152 {
153 if (preg_match(".*<(.+)>", $Address, $regs)) $Address = $regs[1];
154 if (preg_match("^[^@ ]+@([a-zA-Z0-9\-]+\.)+([a-zA-Z0-9\-]{2}|net|com|gov|mil|org|edu|int)\$", $Address))
155 return true;
156 else return false;
157 }
158
159 function CheckAdresses(array $Addresses): void
160 {
161 foreach ($Addresses as $Address)
162 {
163 if (!$this->ValidEmail($Address))
164 {
165 throw new Exception(sprintf(T('Mail message invalid address %s'), $Address));
166 }
167 }
168 }
169
170 private function ContentEncoding($Charset): string
171 {
172 if ($Charset != CHARSET_ASCII) return '8bit';
173 else return '7bit';
174 }
175
176 private function BuildAttachment($Body): string
177 {
178 if (count($this->Attachments) > 0)
179 {
180 $this->Headers['Content-Type'] = "multipart/mixed;\n boundary=\"PHP-mixed-".$this->Boundary."\"";
181 $Result = "This is a multi-part message in MIME format.\n".
182 "--PHP-mixed-".$this->Boundary."\n";
183 $Result .= $Body;
184
185 foreach ($this->Attachments as $Attachment)
186 {
187 $FileName = $Attachment['FileName'];
188 $BaseName = basename($FileName);
189 $ContentType = $Attachment['FileType'];
190 $Disposition = $Attachment['Disposition'];
191 if ($Attachment['Type'] == 'File')
192 {
193 if (!file_exists($FileName))
194 throw new Exception(sprintf(T('Mail message attached file %s can\'t be found'), $FileName));
195 $Data = file_get_contents($FileName);
196 } else
197 if ($Attachment['Type'] == 'Data') $Data = $Attachment['Data'];
198 else $Data = '';
199
200 $Result .= "\n".'--PHP-mixed-'.$this->Boundary."\n".
201 "Content-Type: ".$ContentType."; name=\"".$BaseName."\"\n".
202 "Content-Transfer-Encoding: base64\n".
203 "Content-Disposition: ".$Disposition."\n\n";
204 $Result .= chunk_split(base64_encode($Data))."\r\n";
205 }
206 } else $Result = $Body;
207 return $Result;
208 }
209
210 private function BuildBody(): string
211 {
212 $Result = '';
213 if (count($this->Bodies) > 1)
214 {
215 $this->Headers['Content-Type'] = 'multipart/alternative; boundary="PHP-alt-'.$this->Boundary.'"';
216 $Result .= 'Content-Type: multipart/alternative; boundary="PHP-alt-'.$this->Boundary.'"'.
217 "\n\n";
218 } else
219 {
220 $this->Headers['Content-Type'] = $this->Bodies[0]['Type'].'; charset='.$this->Bodies[0]['Charset'];
221 $this->Headers['Content-Transfer-Encoding'] = $this->ContentEncoding($this->Bodies[0]['Charset']);
222 }
223
224 foreach ($this->Bodies as $Body)
225 {
226 if (count($this->Bodies) > 1) $Result .= "\n\n".'--PHP-alt-'.$this->Boundary."\n";
227 $Result .= 'Content-Type: '.$Body['Type'].'; charset='.$Body['Charset'].
228 "\nContent-Transfer-Encoding: ".$this->ContentEncoding($Body['Charset'])."\n\n".$Body['Content']."\n";
229 }
230 return $Result;
231 }
232}
233
234/*
235// Example
236$Mail = new Mail();
237$Mail->AddTo('nobody@nodomain', 'Recipient');
238$Mail->From = 'No reply <noreply@nodomain>';
239$Mail->Subject = 'Test message';
240$Mail->AddBody('This is sample message sent by PHP Mail class');
241$Mail->AddBody('This is sample <strong>HTML</strong> message sent by <i>PHP Mail class</i>', 'text/html');
242$Mail->AttachFile('mailtest.php', 'text/plain');
243$Mail->AttachData('DataFile.txt', 'text/plain', 'Sample text');
244$Mail->Send();
245*/
Note: See TracBrowser for help on using the repository browser.