1
|
|
|
<?php |
2
|
|
|
namespace DERHANSEN\SfEventMgt\Service; |
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\Utility\GeneralUtility; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* EmailService |
21
|
|
|
* |
22
|
|
|
* @author Torben Hansen <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class EmailService |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Mailmessage |
29
|
|
|
* |
30
|
|
|
* @var \TYPO3\CMS\Core\Mail\MailMessage |
31
|
|
|
*/ |
32
|
|
|
protected $mailer = null; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Sends an e-mail, if sender and recipient is an valid e-mail address |
36
|
|
|
* |
37
|
|
|
* @param string $sender The sender |
38
|
|
|
* @param string $recipient The recipient |
39
|
|
|
* @param string $subject The subject |
40
|
|
|
* @param string $body E-Mail body |
41
|
|
|
* @param string $name Optional sendername |
42
|
|
|
* @param array $attachments Array of files (e.g. ['/absolute/path/doc.pdf']) |
43
|
|
|
* |
44
|
|
|
* @return bool TRUE/FALSE if message is sent |
45
|
|
|
*/ |
46
|
4 |
|
public function sendEmailMessage($sender, $recipient, $subject, $body, $name = null, $attachments = []) |
47
|
|
|
{ |
48
|
4 |
|
if (GeneralUtility::validEmail($sender) && GeneralUtility::validEmail($recipient)) { |
49
|
2 |
|
$this->initialize(); |
50
|
2 |
|
$this->mailer->setFrom($sender, $name); |
51
|
2 |
|
$this->mailer->setSubject($subject); |
52
|
2 |
|
$this->mailer->setBody($body, 'text/html'); |
53
|
2 |
|
$this->mailer->setTo($recipient); |
54
|
2 |
|
$this->addAttachments($attachments); |
55
|
2 |
|
$this->mailer->send(); |
56
|
2 |
|
return $this->mailer->isSent(); |
57
|
|
|
} else { |
58
|
2 |
|
return false; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Creates a new mail message |
64
|
|
|
* |
65
|
|
|
* @return void |
66
|
|
|
*/ |
67
|
|
|
protected function initialize() |
68
|
|
|
{ |
69
|
|
|
$this->mailer = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Mail\\MailMessage'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Attaches the given array of files to the email message |
74
|
|
|
* |
75
|
|
|
* @param array $attachments |
76
|
|
|
* @return void |
77
|
|
|
*/ |
78
|
2 |
|
protected function addAttachments($attachments) |
79
|
|
|
{ |
80
|
2 |
|
if (count($attachments) > 0) { |
81
|
1 |
|
foreach ($attachments as $attachment) { |
82
|
1 |
|
if (file_exists($attachment)) { |
83
|
1 |
|
$this->mailer->attach(\Swift_Attachment::fromPath($attachment)); |
84
|
1 |
|
} |
85
|
1 |
|
} |
86
|
1 |
|
} |
87
|
2 |
|
} |
88
|
|
|
} |
89
|
|
|
|