Passed
Push — develop ( dbabe4...e979a2 )
by Daniel
01:46
created

Mailer::createMultiPartMail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 24
ccs 0
cts 16
cp 0
rs 9.7666
c 0
b 0
f 0
cc 1
nc 1
nop 7
crap 2
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 Template für HTML E-Mails
35
   */
36
  private $htmlTemplate = 'mail/default-html';
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 1
  public function prepareAsText(array $contentValues, string $textTemplate = null)
95
  {
96
97 1
    $message = $this->getMailMessage();
98
99 1
    if (isset($textTemplate) || !empty($textTemplate)) {
100 1
      $this->textTemplate = $textTemplate;
101
    }
102
103 1
    $renderer = $this->getRenderer();
104 1
    $body = $renderer->render($this->textTemplate, $contentValues);
105 1
    $message->setBody($body);
106
    
107 1
  }
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 1
  public function prepareAsMultipart(array $contentValues, string $textTemplate = null, string $htmlTemplate = null)
119
  {
120
121 1
    $message = $this->getMailMessage();
122 1
    $renderer = $this->getRenderer();
123
124 1
    if (isset($textTemplate) || !empty($textTemplate)) {
125 1
      $this->textTemplate = $textTemplate;
126
    }
127
128 1
    if (isset($htmlTemplate) || !empty($htmlTemplate)) {
129 1
      $this->htmlTemplate = $htmlTemplate;
130
    }
131
132 1
    $textContent = $renderer->render($this->textTemplate, $contentValues);
133 1
    $htmlContent = $renderer->render($this->htmlTemplate, $contentValues);
134
135 1
    $textPart = new MimePart($textContent);
136 1
    $textPart->type = Mime::TYPE_TEXT;
137 1
    $textPart->charset = 'utf-8';
138 1
    $textPart->encoding = Mime::ENCODING_QUOTEDPRINTABLE;
139
140 1
    $htmlPart = new MimePart($htmlContent);
141 1
    $htmlPart->type = Mime::TYPE_HTML;
142 1
    $htmlPart->charset = 'utf-8';
143 1
    $htmlPart->encoding = Mime::ENCODING_QUOTEDPRINTABLE;
144
145 1
    $mailBody = new MimeMessage();
146 1
    $mailBody->setParts([$textPart, $htmlPart]);
147
148 1
    $message->setBody($mailBody);
149 1
    $message->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

149
    $message->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

149
    $message->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...
150
    
151 1
  }
152
153
  
154
155
  private function getDefaultMessage($to, $subject, $body, $from = null)
0 ignored issues
show
Unused Code introduced by
The method getDefaultMessage() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
156
  {
157
158
    if (!isset($body) || empty($body)) {
159
      return false;
160
    }
161
162
    if (!isset($to) || empty($to)) {
163
      return false;
164
    }
165
166
    $message = $this->getMailMessage();
167
    $message->setSubject($subject)
168
            ->setBody($body)
169
            ->setTo($to);
170
171
    if (isset($from)) {
172
      $message->setFrom($from);
173
    }
174
175
    // in Factory verschieben und aus config auslesen
176
    $message->getHeaders()->addHeaders(array('X-Mailer' => 'ZfMailer 1.0.1'));
177
178
    return $message;
179
180
  }
181
182
  /**
183
   * Versendet die E-Mails
184
   * @return Boolean Gibt True zurück, wenn die E-Mail erfolgreich versendet wurde, sonst False
185
   */
186
  public function sendEmail()
187
  {
188
189
    try {
190
      $this->getTransport()->send($this->getMailMessage());
191
      return true;
192
    } 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...
193
      //echo 'Exception abgefangen: ',  $e->getMessage(), "\n";
194
      $this->setErrorMessage($e->getMessage());
195
      return false;
196
    }
197
    
198
  }
199
200
  
201
202
}
203