Newer
Older
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
<?php
/**
* StudipMail.class.php
*
* class for constructing and sending emails in Stud.IP
*
*
* @author André Noack <noack@data-quest>, Suchi & Berg GmbH <info@data-quest.de>
* @version 1
* @license GPL2 or any later version
* @copyright 2009 authors
*/
class StudipMail
{
/**
* @var email_message_class
* @static
*/
private static $transporter;
/**
* @var string
*/
private $body_text;
/**
* @var string
*/
private $body_html;
/**
* @var string
*/
private $subject;
/**
* Array of all attachments, name ist key
* @var array
*/
private $attachments = [];
/**
*/
private $sender;
/**
* Array of all recipients, mail is key
* @var array
*/
private $recipients = [];
/**
*/
private $reply_to;
/**
* Sets the default transporter used in StudipMail::send()
* @param email_message_class $transporter
* @return void
*/
public static function setDefaultTransporter(email_message_class $transporter)
{
self::$transporter = $transporter;
}
/**
* gets the default transporter used in StudipMail::send()
*
* @return email_message_class
*/
public static function getDefaultTransporter()
{
return self::$transporter;
}
/**
* Gets the configured abuse mail contact
*
* @return string
*/
public static function getAbuseEmail()
{
return $GLOBALS['MAIL_ABUSE'] ?: "abuse@{$mail_localhost}";
}
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
/**
* convenience method for sending a qick, text based email message
*
* @param string $recipient
* @param string $subject
* @param string $text Plain text version of the message (required).
* @param string $html HTML version of the message (optional).
* @return bool
*/
public static function sendMessage($recipient, $subject, $text, $html = null)
{
$mail = new StudipMail();
return $mail->setSubject($subject)
->addRecipient($recipient)
->setBodyText($text)
->setBodyHtml($html)
->send();
}
/**
* convenience method for sending a qick, text based email message
* to the configured abuse adress
*
* @param string $subject
* @param string $text
* @return bool
*/
public static function sendAbuseMessage($subject, $text)
{
$mail = new StudipMail();
$abuse = self::getAbuseEmail();
return $mail->setSubject($subject)
->addRecipient($abuse)
->setBodyText($text)
->send();
}
/**
* sets some default values for sender and reply to from
* configuration settings.
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
185
*
*/
public function __construct($data = null)
{
$mail_localhost = $GLOBALS['MAIL_LOCALHOST'] ?: $_SERVER['SERVER_NAME'];
$this->setSenderEmail($GLOBALS['MAIL_ENV_FROM'] ?: "wwwrun@{$mail_localhost}");
$this->setSenderName($GLOBALS['MAIL_FROM'] ?: 'Stud.IP - ' . Config::get()->UNI_NAME_CLEAN);
if ($data) {
$this->setData($data);
}
}
/**
* @param string $mail
* @return StudipMail provides fluent interface
*/
public function setSenderEmail($mail)
{
$this->sender['mail'] = $mail;
return $this;
}
/**
* @return string
*/
public function getSenderEmail()
{
return $this->sender['mail'];
}
/**
* @param string $name
* @return StudipMail provides fluent interface
*/
public function setSenderName($name)
{
$this->sender['name'] = $name;
return $this;
}
/**
* @return unknown_type
*/
public function getSenderName()
{
return $this->sender['name'];
}
/**
* @param $mail
* @return StudipMail provides fluent interface
*/
public function setReplyToEmail($mail)
{
$this->reply_to['mail'] = $mail;
return $this;
}
/**
* @return unknown_type
*/
public function getReplyToEmail()
{
return $this->reply_to['mail'] ?? '';
}
/**
* @param $name
* @return StudipMail provides fluent interface
*/
public function setReplyToName($name)
{
$this->reply_to['name'] = $name;
return $this;
}
/**
* @return unknown_type
*/
public function getReplyToName()
{
return $this->reply_to['name'] ?? '';
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
}
/**
* @param $subject
* @return StudipMail provides fluent interface
*/
public function setSubject($subject)
{
$this->subject = $subject;
return $this;
}
/**
* @return unknown_type
*/
public function getSubject()
{
return $this->subject;
}
/**
* @param $mail
* @param $name
* @param $type
* @return StudipMail provides fluent interface
*/
public function addRecipient($mail, $name = '', $type = 'To')
{
$type = ucfirst($type);
$type = in_array($type, ['To', 'Cc', 'Bcc']) ? $type : 'To';
if (!isset($this->recipients[$mail]) || $this->recipients[$mail]['type'] !== 'To') {
$this->recipients[$mail] = compact('mail', 'name', 'type');
}
return $this;
}
/**
* @param $mail
* @return StudipMail provides fluent interface
*/
public function removeRecipient($mail)
{
unset($this->recipients[$mail]);
return $this;
}
/**
* @return array
*/
public function getRecipients()
{
return $this->recipients;
}
/**
* @param $mail
* @return unknown_type
*/
public function isRecipient($mail)
{
return isset($this->recipients[$mail]);
}
/**
* @param $file_name
* @param $name
* @param $type
* @param $disposition
* @return StudipMail provides fluent interface
*/
public function addFileAttachment($file_name, $name = '', $type = 'automatic/name', $disposition = 'attachment')
{
$name = $name ?: basename($file_name);
$this->attachments[$name] = compact('file_name', 'name', 'type', 'disposition');
return $this;
}
/**
* @param $data
* @param $name
* @param $type
* @param $disposition
* @return StudipMail provides fluent interface
*/
public function addDataAttachment($data, $name, $type = 'automatic/name', $disposition = 'attachment')
{
$this->attachments[$name] = compact('data', 'name', 'type', 'disposition');
return $this;
}
/**
* @param FileRef $file_ref The FileRef object of a file that shall be added to a mail
* @return StudipMail provides fluent interface
*/
public function addStudipAttachment(FileRef $file_ref)
{
if (!$file_ref->isNew()) {
$this->addFileAttachment(
$file_ref->file->getPath(),
$file_ref->name
);
}
return $this;
}
/**
* @param $name
* @return StudipMail provides fluent interface
*/
public function removeAttachment($name)
{
unset($this->attachments[$name]);
return $this;
}
/**
* @return array
*/
public function getAttachments()
{
return $this->attachments;
}
/**
* @param $name
* @return unknown_type
*/
public function isAttachment($name)
{
return isset($this->attachments[$name]);
}
/**
* @param $body
* @return StudipMail provides fluent interface
*/
public function setBodyText($body)
{
$this->body_text = $body;
return $this;
}
/**
* @return unknown_type
*/
public function getBodyText()
{
return $this->body_text;
}
/**
* @param $body
* @return StudipMail provides fluent interface
*/
public function setBodyHtml($body)
{
$this->body_html = $body;
return $this;
}
/**
* @return unknown_type
*/
public function getBodyHtml()
{
return $this->body_html;
}
/**
* quotes the given string if it contains any characters
* reserved for special interpretation in RFC 2822.
*/
protected static function quoteString($string)
{
// list of reserved characters in RFC 2822
if (strcspn($string, '()<>[]:;@\\,.') < mb_strlen($string)) {
$string = '"' . addcslashes($string, "\r\"\\") . '"';
}
return $string;
}
/**
* send the mail using the given transporter object, or the
* set default transporter
*
* @param email_message_class $transporter
* @return bool
*/
public function send(email_message_class $transporter = null)
{
if ($transporter === null) {
$transporter = self::$transporter;
}
if ($transporter === null) {
throw new Exception('no mail transport defined');
}
$transporter->ResetMessage();
$transporter->SetHeader('Return-Path', $this->getSenderEmail());
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
$transporter->SetEncodedEmailHeader('From', $this->getSenderEmail(), self::quoteString($this->getSenderName()));
if($this->getReplyToEmail()){
$transporter->SetEncodedEmailHeader('Reply-To', $this->getReplyToEmail(), self::quoteString($this->getReplyToName()));
}
foreach($this->getRecipients() as $recipient) {
$recipients_by_type[$recipient['type']][$recipient['mail']] = self::quoteString($recipient['name']);
}
foreach($recipients_by_type as $type => $recipients){
$transporter->SetMultipleEncodedEmailHeader($type, $recipients);
}
$transporter->SetEncodedHeader('Subject', $this->getSubject());
if($this->getBodyHtml()){
$html_part = '';
$transporter->CreateQuotedPrintableHTMLPart($this->getBodyHtml(), "", $html_part);
$text_part = '';
$text_message = $this->getBodyText();
if(!$text_message){
$text_message = _('Diese Nachricht ist im HTML-Format verfasst. Sie benötigen eine E-Mail-Anwendung, die das HTML-Format anzeigen kann.');
}
$transporter->CreateQuotedPrintableTextPart($transporter->WrapText($text_message), "", $text_part);
$part = [$text_part, $html_part];
$transporter->AddAlternativeMultipart($part);
} else {
$transporter->AddQuotedPrintableTextPart($this->getBodyText());
}
foreach($this->getAttachments() as $attachment){
$part = [
'FileName' => $attachment['file_name'],
'Data' => $attachment['data'],
'Name' => $attachment['name'],
'Content-Type' => $attachment['type'],
'Disposition' => $attachment['disposition'],
];
$transporter->addFilePart($part);
}
$error = $transporter->Send();
if (mb_strlen($error) === 0) {
return true;
} else {
Log::error(get_class($transporter) . '::Send() - ' . $error);
return false;
}
}
public function toArray()
{
return get_object_vars($this);
}
public function setData($data)
{
foreach ($data as $name => $value) {
$this->$name = $value;
}
}
}