Passed
Push — develop ( 56acb9...48a588 )
by Daniel
02:27
created

Mailer::setContent()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 19
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 19
ccs 0
cts 9
cp 0
rs 9.6111
c 0
b 0
f 0
cc 5
nc 4
nop 3
crap 30
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
   * @param string $to Empfänger der Nachricht
42
   * @param string $subject Betreff der Nachricht
43
   * @param string|null $from Absender der Nachricht
44
   * @return \Zend\Mail\Message|boolean Mail
45
   */
46
  public function createNewMail($to, $subject, $from = null)
47
  {
48
49
    // prüfen, ob Absender vorhanden ist
50
    if (!isset($from) || empty($from)) {
51
52
      $from = $this->getOptions()->getDefaultFrom();
53
54
      if (!isset($from) || empty($from)) {
55
        $this->setErrorMessage('Es wurde kein Absender angegeben oder konfiguriert.');
56
        return false;
57
      }
58
59
      $from = $this->getOptions()->getDefaultFrom();
60
61
    }
62
63
    // Empfänger prüfen
64
    if (!isset($to) || empty($to)) {
65
      $this->setErrorMessage('Es wurde kein Empfänger angegeben.');
66
      return false;
67
    }
68
69
    // Betreff prüfen
70
    if (!isset($subject) || empty($subject)) {
71
      $this->setErrorMessage('Es wurde kein Betreff angegeben.');
72
      return false;
73
    }
74
75
    $message = $this->getMailMessage();
76
    $message->setFrom($from);
77
    $message->setTo($to);
78
    $message->setSubject($subject);
79
80
    return $message;
81
82
  }
83
84
  /**
85
   * Erstellt den Body der E-Mail, fügt Variablen hinzu und rendert den Inhalt
86
   * @param array $mailContent  Inhaltsvaiablen
87
   * @param string|null $templateText Template für Text E-Mails (optional)
88
   * @param string|null $templateHtml Template für HTML (Multipart) E-Mails (optional)
89
   */
90
  public function setContent($mailContent, $templateText = null, $templateHtml = null)
91
  {
92
93
    $message = $this->getMailMessage();
94
95
    if (isset($templateText) || !empty($templateText)) {
96
      $this->textTemplate = $templateText;
97
    }
98
99
    if (isset($templateHtml) || !empty($templateHtml)) {
100
      $this->htmlTemplate = $templateHtml;
101
    }
102
103
    // var_dump($this->textTemplate, $this->htmlTemplate);
104
    
105
    $renderer = $this->getRenderer();
106
    $body = $renderer->render($this->textTemplate, $mailContent);
107
108
    $message->setBody($body);
109
110
111
112
  }
113
  
114
  
115
116
  public function createTextMail($to, $subject, $templateOrModel, $values = array(), $from, $encoding = 'UTF-8')
117
  {
118
    $renderer = $this->getRenderer();
119
    $body = $renderer->render($templateOrModel, $values);
120
121
    $mail = $this->getDefaultMessage($to, $subject, $body, $from);
122
    $mail->setEncoding($encoding);
123
    
124
    return $mail;
125
  }
126
127
  public function createMultiPartMail($to, $subject, $txtTemplate, $htmlTemplate, $contentValues = array(), $from = null, $encoding = 'UTF-8')
128
  {
129
130
    // Todo: siehe https://github.com/zendframework/zf2/issues/4917
131
    // Todo: Variablen prüfen
132
133
    $renderer = $this->getRenderer();
134
135
    $txtContent = $renderer->render($txtTemplate, $contentValues);
136
    $htmlContent = $renderer->render($htmlTemplate, $contentValues);
137
138
    $text = new MimePart($txtContent);
139
    $text->type = "text/plain";
140
    $text->charset = 'utf-8';
141
142
    $html = new MimePart($htmlContent);
143
    $html->type = "text/html";
144
    $html->charset = 'utf-8';
145
146
    $body = new MimeMessage();
147
    $body->setParts(array($text, $html));
148
149
    $mail = $this->getDefaultMessage($to, $subject, $body, $from);
150
    $mail->getHeaders()->get('content-type')->setType('multipart/alternative');
0 ignored issues
show
Bug introduced by
The method setType() does not exist on Zend\Mail\Header\HeaderInterface. It seems like you code against a sub-type of Zend\Mail\Header\HeaderInterface such as Zend\Mail\Header\ContentType. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

150
    $mail->getHeaders()->get('content-type')->/** @scrutinizer ignore-call */ setType('multipart/alternative');
Loading history...
Bug introduced by
The method setType() does not exist on ArrayIterator. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

150
    $mail->getHeaders()->get('content-type')->/** @scrutinizer ignore-call */ setType('multipart/alternative');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
151
    $mail->setEncoding($encoding);
152
153
    return $mail;
154
155
  }
156
157
  private function getDefaultMessage($to, $subject, $body, $from = null)
158
  {
159
160
    if (!isset($body) || empty($body)) {
161
      return false;
162
    }
163
164
    if (!isset($to) || empty($to)) {
165
      return false;
166
    }
167
168
    $message = $this->getMailMessage();
169
    $message->setSubject($subject)
170
            ->setBody($body)
171
            ->setTo($to);
172
173
    if (isset($from)) {
174
      $message->setFrom($from);
175
    }
176
177
    // if (isset($encoding)) {
178
    //   $message->setEncoding($encoding);
179
    // }
180
181
    // in Factory verschieben und aus config auslesen
182
    $message->getHeaders()->addHeaders(array('X-Mailer' => 'ZfMailer 1.0.1'));
183
184
    return $message;
185
186
  }
187
188
  /**
189
   * Versendet die E-Mails
190
   * @return Boolean Gibt True zurück, wenn die E-Mail erfolgreich versendet wurde, sonst False
191
   */
192
  public function sendEmail()
193
  {
194
195
    try {
196
      $this->getTransport()->send($this->getMailMessage());
197
      return true;
198
    } catch (RuntimeException $e) {
0 ignored issues
show
Bug introduced by
The type ZfMailer\Service\RuntimeException was not found. Did you mean RuntimeException? If so, make sure to prefix the type with \.
Loading history...
199
      //echo 'Exception abgefangen: ',  $e->getMessage(), "\n";
200
      $this->setErrorMessage($e->getMessage());
201
      return false;
202
    }
203
    
204
  }
205
206
  
207
208
}
209