1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* ZfMailer
|
4
|
|
|
*
|
5
|
|
|
* @author Daniel Wolkenhauer <[email protected]>
|
6
|
|
|
* @copyright Copyright (c) 1997-2019 Daniel Wolkenhauer
|
7
|
|
|
* @link http://dw-labs.de/zfmailer
|
8
|
|
|
* @version 0.1.0
|
9
|
|
|
*/
|
10
|
|
|
|
11
|
|
|
namespace ZfMailer\Service;
|
12
|
|
|
|
13
|
|
|
use Zend\Mime\Message as MimeMessage;
|
14
|
|
|
use Zend\Mime\Mime;
|
15
|
|
|
use Zend\Mime\Part as MimePart;
|
16
|
|
|
|
17
|
|
|
|
18
|
|
|
/**
|
19
|
|
|
* Mailer
|
20
|
|
|
* Klasse für den Versand der E-Mails zuständig
|
21
|
|
|
*
|
22
|
|
|
* @package ZfMailer
|
23
|
|
|
* @subpackage Service
|
24
|
|
|
*/
|
25
|
|
|
class Mailer extends AbstractMailer
|
26
|
|
|
{
|
27
|
|
|
|
28
|
|
|
/**
|
29
|
|
|
* @var string Template für Text E-Mails
|
30
|
|
|
*/
|
31
|
|
|
private $textTemplate = 'mail/default-text';
|
32
|
|
|
|
33
|
|
|
/**
|
34
|
|
|
* @var string|null Template für HTML E-Mails
|
35
|
|
|
*/
|
36
|
|
|
private $htmlTemplate = null;
|
|
|
|
|
37
|
|
|
|
38
|
|
|
/**
|
39
|
|
|
* Neue Nachricht
|
40
|
|
|
*
|
41
|
|
|
* @todo #ZFMAIL-2 weiter Header einfügen: X-Mailer, Org, usw.
|
42
|
|
|
* @param string $to Empfänger der Nachricht
|
43
|
|
|
* @param string $subject Betreff der Nachricht
|
44
|
|
|
* @param string|null $from Absender der Nachricht
|
45
|
|
|
* @return \Zend\Mail\Message|boolean Mail
|
46
|
|
|
*/
|
47
|
1 |
|
public function createNewMail($to, $subject, $from = null)
|
48
|
|
|
{
|
49
|
|
|
|
50
|
|
|
// prüfen, ob Absender vorhanden ist
|
51
|
1 |
|
if (!isset($from) || empty($from)) {
|
52
|
|
|
|
53
|
1 |
|
$from = $this->getOptions()->getDefaultFrom();
|
54
|
|
|
|
55
|
1 |
|
if (!isset($from) || empty($from)) {
|
56
|
1 |
|
$this->setErrorMessage('Es wurde kein Absender angegeben oder konfiguriert.');
|
57
|
1 |
|
return false;
|
58
|
|
|
}
|
59
|
|
|
|
60
|
1 |
|
$from = $this->getOptions()->getDefaultFrom();
|
61
|
|
|
|
62
|
|
|
}
|
63
|
|
|
|
64
|
|
|
// Empfänger prüfen
|
65
|
1 |
|
if (!isset($to) || empty($to)) {
|
66
|
1 |
|
$this->setErrorMessage('Es wurde kein Empfänger angegeben.');
|
67
|
1 |
|
return false;
|
68
|
|
|
}
|
69
|
|
|
|
70
|
|
|
// Betreff prüfen
|
71
|
1 |
|
if (!isset($subject) || empty($subject)) {
|
72
|
1 |
|
$this->setErrorMessage('Es wurde kein Betreff angegeben.');
|
73
|
1 |
|
return false;
|
74
|
|
|
}
|
75
|
|
|
|
76
|
1 |
|
$message = $this->getMailMessage();
|
77
|
1 |
|
$message->setFrom($from);
|
78
|
1 |
|
$message->setTo($to);
|
79
|
1 |
|
$message->setSubject($subject);
|
80
|
|
|
|
81
|
1 |
|
return $message;
|
82
|
|
|
|
83
|
|
|
}
|
84
|
|
|
|
85
|
|
|
/**
|
86
|
|
|
* Bereitet die E-Mail vor, fügt die Content-Variablen ein, rendert das Template
|
87
|
|
|
* und gibt ein fertiges Zend\Mail\Message Objekt zurück
|
88
|
|
|
*
|
89
|
|
|
* @todo #ZFMAIL-3 Datentyp von $contentValues prüfen, falls kein Array, Fehler
|
90
|
|
|
* @param array $contentValues Array mit Werten, die in deie E-Mail eingefügt werden
|
91
|
|
|
* @param string|null $textTemplate Template für eine E-Mail im Textformat
|
92
|
|
|
* @return \Zend\Mail\Message|boolean fertiges Mail-Objekt
|
93
|
|
|
*/
|
94
|
|
|
public function prepareAsText(array $contentValues, string $textTemplate = null)
|
95
|
|
|
{
|
96
|
|
|
|
97
|
|
|
$message = $this->getMailMessage();
|
98
|
|
|
|
99
|
|
|
if (isset($textTemplate) || !empty($textTemplate)) {
|
100
|
|
|
$this->textTemplate = $textTemplate;
|
101
|
|
|
}
|
102
|
|
|
|
103
|
|
|
$renderer = $this->getRenderer();
|
104
|
|
|
$body = $renderer->render($this->textTemplate, $contentValues);
|
105
|
|
|
$message->setBody($body);
|
106
|
|
|
|
107
|
|
|
}
|
108
|
|
|
|
109
|
|
|
/**
|
110
|
|
|
* Bereitet die E-Mail vor, fügt die Content-Variablen ein, rendert die Templates
|
111
|
|
|
* fügt die Teile für Text- und HTML-Inhalte zusammen und gibt ein fertiges
|
112
|
|
|
* Zend\Mail\Message Objekt zurück
|
113
|
|
|
*
|
114
|
|
|
* @param array $contentValues Array mit Werten, die in die E-Mail eingefügt werden
|
115
|
|
|
* @param string|null $textTemplate Template für den Text-Teil der E-Mail
|
116
|
|
|
* @param string|null $htmlTemplate Template für den HTML-Teil der E-Mail
|
117
|
|
|
*/
|
118
|
|
|
public function prepareAsMultipart(array $contentValues, string $textTemplate = null, string $htmlTemplate = null)
|
|
|
|
|
119
|
|
|
{
|
120
|
|
|
# code ...
|
121
|
|
|
}
|
122
|
|
|
|
123
|
|
|
public function createMultiPartMail($to, $subject, $txtTemplate, $htmlTemplate, $contentValues = array(), $from = null, $encoding = 'UTF-8')
|
124
|
|
|
{
|
125
|
|
|
|
126
|
|
|
$renderer = $this->getRenderer();
|
127
|
|
|
|
128
|
|
|
$txtContent = $renderer->render($txtTemplate, $contentValues);
|
129
|
|
|
$htmlContent = $renderer->render($htmlTemplate, $contentValues);
|
130
|
|
|
|
131
|
|
|
$text = new MimePart($txtContent);
|
132
|
|
|
$text->type = "text/plain";
|
133
|
|
|
$text->charset = 'utf-8';
|
134
|
|
|
|
135
|
|
|
$html = new MimePart($htmlContent);
|
136
|
|
|
$html->type = "text/html";
|
137
|
|
|
$html->charset = 'utf-8';
|
138
|
|
|
|
139
|
|
|
$body = new MimeMessage();
|
140
|
|
|
$body->setParts(array($text, $html));
|
141
|
|
|
|
142
|
|
|
$mail = $this->getDefaultMessage($to, $subject, $body, $from);
|
143
|
|
|
$mail->getHeaders()->get('content-type')->setType('multipart/alternative');
|
|
|
|
|
144
|
|
|
$mail->setEncoding($encoding);
|
145
|
|
|
|
146
|
|
|
return $mail;
|
147
|
|
|
|
148
|
|
|
}
|
149
|
|
|
|
150
|
|
|
private function getDefaultMessage($to, $subject, $body, $from = null)
|
151
|
|
|
{
|
152
|
|
|
|
153
|
|
|
if (!isset($body) || empty($body)) {
|
154
|
|
|
return false;
|
155
|
|
|
}
|
156
|
|
|
|
157
|
|
|
if (!isset($to) || empty($to)) {
|
158
|
|
|
return false;
|
159
|
|
|
}
|
160
|
|
|
|
161
|
|
|
$message = $this->getMailMessage();
|
162
|
|
|
$message->setSubject($subject)
|
163
|
|
|
->setBody($body)
|
164
|
|
|
->setTo($to);
|
165
|
|
|
|
166
|
|
|
if (isset($from)) {
|
167
|
|
|
$message->setFrom($from);
|
168
|
|
|
}
|
169
|
|
|
|
170
|
|
|
// in Factory verschieben und aus config auslesen
|
171
|
|
|
$message->getHeaders()->addHeaders(array('X-Mailer' => 'ZfMailer 1.0.1'));
|
172
|
|
|
|
173
|
|
|
return $message;
|
174
|
|
|
|
175
|
|
|
}
|
176
|
|
|
|
177
|
|
|
/**
|
178
|
|
|
* Versendet die E-Mails
|
179
|
|
|
* @return Boolean Gibt True zurück, wenn die E-Mail erfolgreich versendet wurde, sonst False
|
180
|
|
|
*/
|
181
|
|
|
public function sendEmail()
|
182
|
|
|
{
|
183
|
|
|
|
184
|
|
|
try {
|
185
|
|
|
$this->getTransport()->send($this->getMailMessage());
|
186
|
|
|
return true;
|
187
|
|
|
} catch (RuntimeException $e) {
|
|
|
|
|
188
|
|
|
//echo 'Exception abgefangen: ', $e->getMessage(), "\n";
|
189
|
|
|
$this->setErrorMessage($e->getMessage());
|
190
|
|
|
return false;
|
191
|
|
|
}
|
192
|
|
|
|
193
|
|
|
}
|
194
|
|
|
|
195
|
|
|
|
196
|
|
|
|
197
|
|
|
}
|
198
|
|
|
|