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
|
|
|
use Zend\View\Renderer\RendererInterface;
|
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
|
26
|
|
|
{
|
27
|
|
|
|
28
|
|
|
protected $errorMessage = null;
|
29
|
|
|
protected $renderer = null;
|
30
|
|
|
protected $mailMessage = null;
|
31
|
|
|
protected $transport = null;
|
32
|
|
|
|
33
|
|
|
private function getDefaultMessage($to, $subject, $body, $from = null)
|
|
|
|
|
34
|
|
|
{
|
35
|
|
|
|
36
|
|
|
if (!isset($body) || empty($body)) {
|
37
|
|
|
return false;
|
38
|
|
|
}
|
39
|
|
|
|
40
|
|
|
if (!isset($to) || empty($to)) {
|
41
|
|
|
return false;
|
42
|
|
|
}
|
43
|
|
|
|
44
|
|
|
$message = $this->getMailMessage();
|
45
|
|
|
$message->setSubject($subject)
|
46
|
|
|
->setBody($body)
|
47
|
|
|
->setTo($to);
|
48
|
|
|
|
49
|
|
|
if (isset($from)) {
|
50
|
|
|
$message->setFrom($from);
|
51
|
|
|
}
|
52
|
|
|
|
53
|
|
|
// if (isset($encoding)) {
|
54
|
|
|
// $message->setEncoding($encoding);
|
55
|
|
|
// }
|
56
|
|
|
|
57
|
|
|
// Todo: in Factory verschieben und aus config auslesen
|
58
|
|
|
$message->getHeaders()->addHeaders(array('X-Mailer' => 'ZfMailer 1.0.1'));
|
59
|
|
|
|
60
|
|
|
return $message;
|
61
|
|
|
|
62
|
|
|
}
|
63
|
|
|
|
64
|
|
|
public function createTextMail($to, $subject, $templateOrModel, $values = array(), $from, $encoding = 'UTF-8')
|
|
|
|
|
65
|
|
|
{
|
66
|
|
|
$renderer = $this->getRenderer();
|
67
|
|
|
$body = $renderer->render($templateOrModel, $values);
|
68
|
|
|
|
69
|
|
|
$mail = $this->getDefaultMessage($to, $subject, $body, $from);
|
70
|
|
|
$mail->setEncoding($encoding);
|
71
|
|
|
|
72
|
|
|
return $mail;
|
73
|
|
|
}
|
74
|
|
|
|
75
|
|
|
public function createMultiPartMail($to, $subject, $txtTemplate, $htmlTemplate, $contentValues = array(), $from = null, $encoding = 'UTF-8')
|
|
|
|
|
76
|
|
|
{
|
77
|
|
|
|
78
|
|
|
// Todo: siehe https://github.com/zendframework/zf2/issues/4917
|
79
|
|
|
// Todo: Variablen prüfen
|
80
|
|
|
|
81
|
|
|
$renderer = $this->getRenderer();
|
82
|
|
|
|
83
|
|
|
$txtContent = $renderer->render($txtTemplate, $contentValues);
|
84
|
|
|
$htmlContent = $renderer->render($htmlTemplate, $contentValues);
|
85
|
|
|
|
86
|
|
|
$text = new MimePart($txtContent);
|
87
|
|
|
$text->type = "text/plain";
|
88
|
|
|
$text->charset = 'utf-8';
|
89
|
|
|
|
90
|
|
|
$html = new MimePart($htmlContent);
|
91
|
|
|
$html->type = "text/html";
|
92
|
|
|
$html->charset = 'utf-8';
|
93
|
|
|
|
94
|
|
|
$body = new MimeMessage();
|
95
|
|
|
$body->setParts(array($text, $html));
|
96
|
|
|
|
97
|
|
|
$mail = $this->getDefaultMessage($to, $subject, $body, $from);
|
98
|
|
|
$mail->getHeaders()->get('content-type')->setType('multipart/alternative');
|
99
|
|
|
$mail->setEncoding($encoding);
|
100
|
|
|
|
101
|
|
|
return $mail;
|
102
|
|
|
|
103
|
|
|
}
|
104
|
|
|
|
105
|
|
|
public function createMailWithAttachment()
|
106
|
|
|
{
|
107
|
|
|
return 'Das ist eine Mail mit Anhang';
|
108
|
|
|
}
|
109
|
|
|
|
110
|
|
|
public function sendEmail()
|
|
|
|
|
111
|
|
|
{
|
112
|
|
|
|
113
|
|
|
try {
|
114
|
|
|
$this->getTransport()->send($this->getMailMessage());
|
115
|
|
|
return true;
|
116
|
|
|
} catch (RuntimeException $e) {
|
|
|
|
|
117
|
|
|
//echo 'Exception abgefangen: ', $e->getMessage(), "\n";
|
118
|
|
|
$this->setErrorMessage($e->getMessage());
|
119
|
|
|
return false;
|
120
|
|
|
}
|
121
|
|
|
|
122
|
|
|
}
|
123
|
|
|
|
124
|
|
|
public function setRenderer(RendererInterface $renderer)
|
125
|
|
|
{
|
126
|
|
|
$this->renderer = $renderer;
|
127
|
|
|
return $this;
|
128
|
|
|
}
|
129
|
|
|
|
130
|
|
|
public function getRenderer()
|
131
|
|
|
{
|
132
|
|
|
return $this->renderer;
|
133
|
|
|
}
|
134
|
|
|
|
135
|
|
|
public function setMailMessage($mailMessage)
|
136
|
|
|
{
|
137
|
|
|
$this->mailMessage = $mailMessage;
|
138
|
|
|
return $this;
|
139
|
|
|
}
|
140
|
|
|
|
141
|
|
|
public function getMailMessage()
|
|
|
|
|
142
|
|
|
{
|
143
|
|
|
return $this->mailMessage;
|
144
|
|
|
}
|
145
|
|
|
|
146
|
|
|
public function setTransport($transport)
|
147
|
|
|
{
|
148
|
|
|
$this->transport = $transport;
|
149
|
|
|
return $this;
|
150
|
|
|
}
|
151
|
|
|
|
152
|
|
|
public function getTransport()
|
|
|
|
|
153
|
|
|
{
|
154
|
|
|
return $this->transport;
|
155
|
|
|
}
|
156
|
|
|
|
157
|
|
|
public function setErrorMessage($errorMessage)
|
158
|
|
|
{
|
159
|
|
|
$this->errorMessage = $errorMessage;
|
160
|
|
|
return $this;
|
161
|
|
|
}
|
162
|
|
|
|
163
|
|
|
public function getErrorMessage()
|
|
|
|
|
164
|
|
|
{
|
165
|
|
|
return $this->errorMessage;
|
166
|
|
|
}
|
167
|
|
|
|
168
|
|
|
}
|
169
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.