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
|
|
|
* Neue Nachricht
|
30
|
|
|
*
|
31
|
|
|
* @param string $to Empfänger der Nachricht
|
32
|
|
|
* @param string $subject Betreff der Nachricht
|
33
|
|
|
* @param string $from Absender der Nachricht
|
34
|
|
|
* @return Zend\Mail\Message Mail
|
|
|
|
|
35
|
|
|
*/
|
36
|
|
|
public function createNewMail($to, $subject, $from = null)
|
37
|
|
|
{
|
38
|
|
|
|
39
|
|
|
// prüfen, ob Absender vorhanden ist
|
40
|
|
|
if (!isset($from) || empty($from)) {
|
41
|
|
|
|
42
|
|
|
$from = $this->getOptions()->getDefaultFrom();
|
43
|
|
|
|
44
|
|
|
if (!isset($from) || empty($from)) {
|
45
|
|
|
$this->setErrorMessage('Es wurde kein Absender angegeben oder konfiguriert.');
|
46
|
|
|
return false;
|
|
|
|
|
47
|
|
|
}
|
48
|
|
|
|
49
|
|
|
$from = $this->getOptions()->getDefaultFrom();
|
50
|
|
|
|
51
|
|
|
}
|
52
|
|
|
|
53
|
|
|
// Empfänger prüfen
|
54
|
|
|
if (!isset($to) || empty($to)) {
|
55
|
|
|
$this->setErrorMessage('Es wurde kein Empfänger angegeben.');
|
56
|
|
|
return false;
|
|
|
|
|
57
|
|
|
}
|
58
|
|
|
|
59
|
|
|
// Betreff prüfen
|
60
|
|
|
if (!isset($subject) || empty($subject)) {
|
61
|
|
|
$this->setErrorMessage('Es wurde kein Betreff angegeben.');
|
62
|
|
|
return false;
|
|
|
|
|
63
|
|
|
}
|
64
|
|
|
|
65
|
|
|
$message = $this->getMailMessage();
|
66
|
|
|
$message->setFrom($from);
|
67
|
|
|
$message->setTo($to);
|
68
|
|
|
$message->setSubject($subject);
|
69
|
|
|
|
70
|
|
|
return $message;
|
|
|
|
|
71
|
|
|
|
72
|
|
|
}
|
73
|
|
|
|
74
|
|
|
private function getDefaultMessage($to, $subject, $body, $from = null)
|
75
|
|
|
{
|
76
|
|
|
|
77
|
|
|
if (!isset($body) || empty($body)) {
|
78
|
|
|
return false;
|
79
|
|
|
}
|
80
|
|
|
|
81
|
|
|
if (!isset($to) || empty($to)) {
|
82
|
|
|
return false;
|
83
|
|
|
}
|
84
|
|
|
|
85
|
|
|
$message = $this->getMailMessage();
|
86
|
|
|
$message->setSubject($subject)
|
87
|
|
|
->setBody($body)
|
88
|
|
|
->setTo($to);
|
89
|
|
|
|
90
|
|
|
if (isset($from)) {
|
91
|
|
|
$message->setFrom($from);
|
92
|
|
|
}
|
93
|
|
|
|
94
|
|
|
// if (isset($encoding)) {
|
95
|
|
|
// $message->setEncoding($encoding);
|
96
|
|
|
// }
|
97
|
|
|
|
98
|
|
|
// Todo: in Factory verschieben und aus config auslesen
|
99
|
|
|
$message->getHeaders()->addHeaders(array('X-Mailer' => 'ZfMailer 1.0.1'));
|
100
|
|
|
|
101
|
|
|
return $message;
|
102
|
|
|
|
103
|
|
|
}
|
104
|
|
|
|
105
|
|
|
public function createTextMail($to, $subject, $templateOrModel, $values = array(), $from, $encoding = 'UTF-8')
|
106
|
|
|
{
|
107
|
|
|
$renderer = $this->getRenderer();
|
108
|
|
|
$body = $renderer->render($templateOrModel, $values);
|
109
|
|
|
|
110
|
|
|
$mail = $this->getDefaultMessage($to, $subject, $body, $from);
|
111
|
|
|
$mail->setEncoding($encoding);
|
112
|
|
|
|
113
|
|
|
return $mail;
|
114
|
|
|
}
|
115
|
|
|
|
116
|
|
|
public function createMultiPartMail($to, $subject, $txtTemplate, $htmlTemplate, $contentValues = array(), $from = null, $encoding = 'UTF-8')
|
117
|
|
|
{
|
118
|
|
|
|
119
|
|
|
// Todo: siehe https://github.com/zendframework/zf2/issues/4917
|
120
|
|
|
// Todo: Variablen prüfen
|
121
|
|
|
|
122
|
|
|
$renderer = $this->getRenderer();
|
123
|
|
|
|
124
|
|
|
$txtContent = $renderer->render($txtTemplate, $contentValues);
|
125
|
|
|
$htmlContent = $renderer->render($htmlTemplate, $contentValues);
|
126
|
|
|
|
127
|
|
|
$text = new MimePart($txtContent);
|
128
|
|
|
$text->type = "text/plain";
|
129
|
|
|
$text->charset = 'utf-8';
|
130
|
|
|
|
131
|
|
|
$html = new MimePart($htmlContent);
|
132
|
|
|
$html->type = "text/html";
|
133
|
|
|
$html->charset = 'utf-8';
|
134
|
|
|
|
135
|
|
|
$body = new MimeMessage();
|
136
|
|
|
$body->setParts(array($text, $html));
|
137
|
|
|
|
138
|
|
|
$mail = $this->getDefaultMessage($to, $subject, $body, $from);
|
139
|
|
|
$mail->getHeaders()->get('content-type')->setType('multipart/alternative');
|
|
|
|
|
140
|
|
|
$mail->setEncoding($encoding);
|
141
|
|
|
|
142
|
|
|
return $mail;
|
143
|
|
|
|
144
|
|
|
}
|
145
|
|
|
|
146
|
|
|
public function createMailWithAttachment()
|
147
|
|
|
{
|
148
|
|
|
return 'Das ist eine Mail mit Anhang';
|
149
|
|
|
}
|
150
|
|
|
|
151
|
|
|
public function sendEmail()
|
152
|
|
|
{
|
153
|
|
|
|
154
|
|
|
try {
|
155
|
|
|
$this->getTransport()->send($this->getMailMessage());
|
156
|
|
|
return true;
|
157
|
|
|
} catch (RuntimeException $e) {
|
|
|
|
|
158
|
|
|
//echo 'Exception abgefangen: ', $e->getMessage(), "\n";
|
159
|
|
|
$this->setErrorMessage($e->getMessage());
|
160
|
|
|
return false;
|
161
|
|
|
}
|
162
|
|
|
|
163
|
|
|
}
|
164
|
|
|
|
165
|
|
|
|
166
|
|
|
|
167
|
|
|
}
|
168
|
|
|
|