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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
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
402
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
|
<?php
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\Part\DataPart;
/**
* StudipMail.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
{
public const DEBUG_TRANSPORTER = 'debug';
public const SENDMAIL_TRANSPORTER = 'sendmail';
public const NULL_TRANSPORTER = 'null';
public const NATIVE_TRANSPORTER = 'php';
public const SMTP_TRANSPORTER = 'smtp';
private ?Mailer $mailer;
private static string $transporter;
private string $body_text;
private string $body_html;
private string $subject;
/**
* Array of all attachments, name ist key
*
*/
private array $attachments = [];
/**
* Array of attachments that are related to the content
*
*/
private array $related_attachments = [];
private array $sender;
/**
* Array of all recipients, mail is key
*/
private array $recipients = [];
/**
* @var array
*/
private array $reply_to;
/**
* Sets the default transporter used in StudipMail::send()
* @param string $transporter
* @return void
*/
public static function setDefaultTransporter(string $transporter)
{
self::$transporter = $transporter;
}
/**
* gets the default transporter used in StudipMail::send()
*
* @return string
*/
public static function getDefaultTransporter()
{
return self::$transporter;
}
/**
* Gets the configured abuse mail contact
*
* @return string
*/
public static function getAbuseEmail(): string
{
$mail_localhost = $GLOBALS['MAIL_LOCALHOST'] ?: $_SERVER['SERVER_NAME'];
return $GLOBALS['MAIL_ABUSE'] ?: "abuse@{$mail_localhost}";
}
/**
* 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|null $html HTML version of the message (optional).
* @return bool
*/
public static function sendMessage(string $recipient, string $subject, string $text, string $html = ''): bool
{
$mail = new StudipMail();
// Add Stud.IP logo as "pseudo" attachment - this will be embedded in the mail via Content-ID.
$mail->addRelatedAttachment(
$GLOBALS['STUDIP_BASE_PATH'] . '/public/assets/images/logos/logo-hires.png',
'studip-logo.png',
'image/png',
'studiplogo'
);
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): bool
{
$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.
*
*/
public function __construct($data = null)
{
$dsn = match (self::getDefaultTransporter()) {
self::SENDMAIL_TRANSPORTER => 'sendmail://default',
self::NATIVE_TRANSPORTER => 'native://default',
self::NULL_TRANSPORTER => 'null://null',
default => $this->buildSmtpDsn()
};
if (self::getDefaultTransporter() === self::DEBUG_TRANSPORTER) {
$transport = new StudipDebugTransport(
$GLOBALS['TMP_PATH'] . '/' .
($GLOBALS['DEBUG_MAIL_LOG_FILE_NAME'] ?? 'studip-mail-debug.log')
);
} else {
$transport = Transport::fromDsn($dsn);
}
$this->mailer = new Mailer($transport);
$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);
}
}
private function buildSmtpDsn(): string
{
$host = $GLOBALS['MAIL_HOST_NAME'] ?: 'localhost';
$port = $GLOBALS['MAIL_SMTP_OPTIONS']['port'] ?? 25;
$user = $GLOBALS['MAIL_SMTP_OPTIONS']['user'] ?? '';
$pass = $GLOBALS['MAIL_SMTP_OPTIONS']['password'] ?? '';
$query = [];
if (!empty($GLOBALS['MAIL_SMTP_OPTIONS']['ssl'])) {
$query['encryption'] = 'ssl';
} elseif (!empty($GLOBALS['MAIL_SMTP_OPTIONS']['start_tls'])) {
$query['encryption'] = 'tls';
}
if (!empty($GLOBALS['MAIL_SMTP_OPTIONS']['authentication_mechanism'])) {
$query['auth_mode'] = $GLOBALS['MAIL_SMTP_OPTIONS']['authentication_mechanism'];
}
$mail_localhost = $GLOBALS['MAIL_LOCALHOST'] ?: $_SERVER['SERVER_NAME'];
if ($mail_localhost) {
$query['local_domain'] = $mail_localhost;
}
$credentials = '';
if ($user !== '') {
$credentials = urlencode($user);
if ($pass !== '') {
$credentials .= ':' . urlencode($pass);
}
$credentials .= '@';
}
$qs = $query ? '?' . http_build_query($query) : '';
return "smtp://{$credentials}{$host}:{$port}{$qs}";
}
/**
* @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 string
*/
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 string
*/
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 string
*/
public function getReplyToName()
{
return $this->reply_to['name'] ?? '';
}
/**
* @param $subject
* @return StudipMail provides fluent interface
*/
public function setSubject($subject)
{
$this->subject = $subject;
return $this;
}
/**
* @return string
*/
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 $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 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;
}
public function addRelatedAttachment(string $file_name, string $name, string $type, string $content_id): void
{
$this->related_attachments[$name] = [
'FileName' => $file_name,
'Name' => $name,
'Content-Type' => $type,
'Disposition' => 'inline',
'Content-ID' => $content_id
];
}
/**
* @return array
*/
public function getAttachments()
{
return $this->attachments;
}
/**
* @param $body
* @return StudipMail provides fluent interface
*/
public function setBodyText($body)
{
$this->body_text = $body;
return $this;
}
/**
* @return string
*/
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 string
*/
public function getBodyHtml()
{
return $this->body_html;
}
/**
* send the mail
*
* @return bool
*/
public function send(): bool
{
try {
$email = new Email();
$email->returnPath($this->getSenderEmail());
$email->from(
new Address(
$this->getSenderEmail(),
$this->getSenderName()
)
);
if ($this->getReplyToEmail()) {
$email->replyTo(
new Address(
$this->getReplyToEmail(),
$this->getReplyToName()
)
);
}
foreach ($this->getRecipients() as $recipient) {
$address = new Address(
$recipient['mail'],
$recipient['name']
);
switch ($recipient['type']) {
case 'Cc':
$email->cc($address);
break;
case 'Bcc':
$email->bcc($address);
break;
default:
$email->to($address);
}
}
$email->subject($this->getSubject());
if ($this->getBodyHtml()) {
$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.'
);
}
$email->text($text_message);
$email->html($this->getBodyHtml());
foreach ($this->related_attachments as $attachment) {
$part = new DataPart(
fopen($attachment['FileName'], 'r'),
$attachment['Content-ID'],
$attachment['Content-Type']
);
$part->asInline();
$email->addPart($part);
}
} else {
$email->text($this->getBodyText());
}
foreach ($this->getAttachments() as $attachment) {
if (!empty($attachment['file_name'])) {
$email->attachFromPath(
$attachment['file_name'],
$attachment['name'],
$attachment['type'] !== 'automatic/name' ? $attachment['type'] : null
);
} elseif (!empty($attachment['data'])) {
$email->attach(
$attachment['data'],
$attachment['name'],
$attachment['type'] !== 'automatic/name' ? $attachment['type'] : null
);
}
}
$this->mailer->send($email);
return true;
} catch (\Throwable $e) {
Log::error('StudipMail::send - ' . $e->getMessage());
return false;
}
}
public function toArray()
{
return get_object_vars($this);
}
public function setData($data)
{
foreach ($data as $name => $value) {
$this->$name = $value;
}
}
}
|