|
1
|
|
|
<?php |
|
2
|
|
|
namespace DERHANSEN\SfEventMgt\Utility; |
|
3
|
|
|
|
|
4
|
|
|
/* |
|
5
|
|
|
* This file is part of the TYPO3 CMS project. |
|
6
|
|
|
* |
|
7
|
|
|
* It is free software; you can redistribute it and/or modify it under |
|
8
|
|
|
* the terms of the GNU General Public License, either version 2 |
|
9
|
|
|
* of the License, or any later version. |
|
10
|
|
|
* |
|
11
|
|
|
* For the full copyright and license information, please read the |
|
12
|
|
|
* LICENSE.txt file that was distributed with this source code. |
|
13
|
|
|
* |
|
14
|
|
|
* The TYPO3 project - inspiring people to share! |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
use TYPO3\CMS\Core\Mail\MailMessage; |
|
18
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* MailUtility |
|
22
|
|
|
* |
|
23
|
|
|
* @author Torben Hansen <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class MailUtility |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* Sends an e-mail, if sender and recipient is an valid e-mail address |
|
29
|
|
|
* |
|
30
|
|
|
* @param string $sender The sender |
|
31
|
|
|
* @param string $recipient The recipient |
|
32
|
|
|
* @param string $subject The subject |
|
33
|
|
|
* @param string $body E-Mail body |
|
34
|
|
|
* @param string $name Optional sendername |
|
35
|
|
|
* @param array $attachments Array of files (e.g. ['/absolute/path/doc.pdf']) |
|
36
|
|
|
* |
|
37
|
|
|
* @return bool TRUE/FALSE if message is sent |
|
38
|
|
|
*/ |
|
39
|
|
|
public static function sendEmailMessage($sender, $recipient, $subject, $body, $name = null, $attachments = []) |
|
40
|
|
|
{ |
|
41
|
|
|
if (GeneralUtility::validEmail($sender) && GeneralUtility::validEmail($recipient)) { |
|
42
|
|
|
$message = GeneralUtility::makeInstance(MailMessage::class); |
|
43
|
|
|
$message->setFrom($sender, $name); |
|
44
|
|
|
$message->setSubject($subject); |
|
45
|
|
|
$message->setBody($body, 'text/html'); |
|
46
|
|
|
$message->setTo($recipient); |
|
47
|
|
|
self::addAttachments($message, $attachments); |
|
48
|
|
|
$message->send(); |
|
49
|
|
|
return $message->isSent(); |
|
50
|
|
|
} else { |
|
51
|
|
|
return false; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Adds given attachments to the given message |
|
57
|
|
|
* |
|
58
|
|
|
* @param MailMessage $message |
|
59
|
|
|
* @param array $attachments |
|
60
|
|
|
* return void |
|
61
|
|
|
*/ |
|
62
|
|
|
protected static function addAttachments(&$message, $attachments) |
|
63
|
|
|
{ |
|
64
|
|
|
if (count($attachments) > 0) { |
|
65
|
|
|
foreach ($attachments as $attachment) { |
|
66
|
|
|
if (file_exists($attachment)) { |
|
67
|
|
|
$message->attach(\Swift_Attachment::fromPath($attachment)); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
} |