Passed
Push — develop ( c4b4be...977ef2 )
by Daniel
01:53
created

Mailer::prepareAsMultipart()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 33
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 5

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 33
ccs 22
cts 22
cp 1
rs 9.2728
c 0
b 0
f 0
cc 5
nc 4
nop 3
crap 5
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
   * @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 1
  public function createNewMail($to, $subject, $from = null)
47
  {
48
49
    // prüfen, ob Absender vorhanden ist
50 1
    if (!isset($from) || empty($from)) {
51
52 1
      $from = $this->getOptions()->getDefaultFrom();
53
54 1
      if (!isset($from) || empty($from)) {
55 1
        $this->setErrorMessage('Es wurde kein Absender angegeben oder konfiguriert.');
56 1
        return false;
57
      }
58
59
      // ToDo: prüfen, ob das hier nötig ist!
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
    $xMailer = $this->getOptions()->getXMailer();
82 1
    $organization = $this->getOptions()->getOrganization();
83 1
    $returnPath = $this->getOptions()->getReturnPath();
84 1
    $replyTo = $this->getOptions()->getReplyTo();
85
86 1
    if (isset($xMailer) && !empty($xMailer)) {
87 1
      $message->getHeaders()->addHeaders(array('X-Mailer' => $xMailer));
88
    }
89
90 1
    if (isset($organization) && !empty($organization)) {
91
      $message->getHeaders()->addHeaders(array('Organization' => $organization));
92
    }
93
94 1
    if (isset($returnPath) && !empty($returnPath)) {
95
      $message->getHeaders()->addHeaders(array('Return-Path' => $returnPath));
96
    }
97
98 1
    if (isset($replyTo) && !empty($replyTo)) {
99
      $message->getHeaders()->addHeaders(array('Reply-To' => $replyTo));
100
    }
101
102 1
    return $message;
103
104
  }
105
106
  /**
107
   * Bereitet die E-Mail vor, fügt die Content-Variablen ein, rendert das Template
108
   * und gibt ein fertiges Zend\Mail\Message Objekt zurück
109
   * 
110
   * @todo #ZFMAIL-3 Datentyp von $contentValues prüfen, falls kein Array, Fehler
111
   * @param array $contentValues Array mit Werten, die in deie E-Mail eingefügt werden
112
   * @param string|null $textTemplate Template für eine E-Mail im Textformat
113
   * @return \Zend\Mail\Message|boolean fertiges Mail-Objekt
114
   */
115 1
  public function prepareAsText(array $contentValues, string $textTemplate = null)
116
  {
117
118 1
    $message = $this->getMailMessage();
119
120 1
    if (isset($textTemplate) || !empty($textTemplate)) {
121 1
      $this->textTemplate = $textTemplate;
122
    }
123
124 1
    $renderer = $this->getRenderer();
125 1
    $body = $renderer->render($this->textTemplate, $contentValues);
126 1
    $message->setBody($body);
127
    
128 1
  }
129
130
  /**
131
   * Bereitet die E-Mail vor, fügt die Content-Variablen ein, rendert die Templates
132
   * fügt die Teile für Text- und HTML-Inhalte zusammen und gibt ein fertiges
133
   * Zend\Mail\Message Objekt zurück
134
   * 
135
   * @param array $contentValues Array mit Werten, die in die E-Mail eingefügt werden
136
   * @param string|null $textTemplate Template für den Text-Teil der E-Mail
137
   * @param string|null $htmlTemplate Template für den HTML-Teil der E-Mail
138
   */
139 1
  public function prepareAsMultipart(array $contentValues, string $textTemplate = null, string $htmlTemplate = null)
140
  {
141 1
    $defaultEncoding = $this->getOptions()->getEncoding();
142
143 1
    $message = $this->getMailMessage();
144 1
    $renderer = $this->getRenderer();
145
146 1
    if (isset($textTemplate) || !empty($textTemplate)) {
147 1
      $this->textTemplate = $textTemplate;
148
    }
149
150 1
    if (isset($htmlTemplate) || !empty($htmlTemplate)) {
151 1
      $this->htmlTemplate = $htmlTemplate;
152
    }
153
154 1
    $textContent = $renderer->render($this->textTemplate, $contentValues);
155 1
    $htmlContent = $renderer->render($this->htmlTemplate, $contentValues);
156
157 1
    $textPart = new MimePart($textContent);
158 1
    $textPart->type = Mime::TYPE_TEXT;
159 1
    $textPart->charset = $defaultEncoding;
160 1
    $textPart->encoding = Mime::ENCODING_QUOTEDPRINTABLE;
161
162 1
    $htmlPart = new MimePart($htmlContent);
163 1
    $htmlPart->type = Mime::TYPE_HTML;
164 1
    $htmlPart->charset = $defaultEncoding;
165 1
    $htmlPart->encoding = Mime::ENCODING_QUOTEDPRINTABLE;
166
167 1
    $mailBody = new MimeMessage();
168 1
    $mailBody->setParts([$textPart, $htmlPart]);
169
170 1
    $message->setBody($mailBody);
171 1
    $message->getHeaders()->get('content-type')->setType('multipart/alternative');
0 ignored issues
show
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

171
    $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...
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

171
    $message->getHeaders()->get('content-type')->/** @scrutinizer ignore-call */ setType('multipart/alternative');
Loading history...
172
    
173 1
  }
174
175
  /**
176
   * Versendet die E-Mails
177
   * @return Boolean Gibt True zurück, wenn die E-Mail erfolgreich versendet wurde, sonst False
178
   */
179
  public function sendEmail()
180
  {
181
182
    try {
183
      $this->getTransport()->send($this->getMailMessage());
184
      return true;
185
    } 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...
186
      //echo 'Exception abgefangen: ',  $e->getMessage(), "\n";
187
      $this->setErrorMessage($e->getMessage());
188
      return false;
189
    }
190
    
191
  }
192
193
}
194