Passed
Push — develop ( 48a588...dbabe4 )
by Daniel
01:37
created

Mailer::createTextMail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 9
ccs 0
cts 6
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 6
crap 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A Mailer::prepareAsMultipart() 0 2 1
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;
0 ignored issues
show
introduced by
The private property $htmlTemplate is not used, and could be removed.
Loading history...
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
  public function prepareAsText(array $contentValues, string $textTemplate = null)
95
  {
96
97
    $message = $this->getMailMessage();
98
99
    if (isset($textTemplate) || !empty($textTemplate)) {
100
      $this->textTemplate = $textTemplate;
101
    }
102
103
    $renderer = $this->getRenderer();
104
    $body = $renderer->render($this->textTemplate, $contentValues);
105
    $message->setBody($body);
106
    
107
  }
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
  public function prepareAsMultipart(array $contentValues, string $textTemplate = null, string $htmlTemplate = null)
0 ignored issues
show
Unused Code introduced by
The parameter $htmlTemplate is not used and could be removed. ( Ignorable by Annotation )

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

118
  public function prepareAsMultipart(array $contentValues, string $textTemplate = null, /** @scrutinizer ignore-unused */ string $htmlTemplate = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $contentValues is not used and could be removed. ( Ignorable by Annotation )

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

118
  public function prepareAsMultipart(/** @scrutinizer ignore-unused */ array $contentValues, string $textTemplate = null, string $htmlTemplate = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $textTemplate is not used and could be removed. ( Ignorable by Annotation )

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

118
  public function prepareAsMultipart(array $contentValues, /** @scrutinizer ignore-unused */ string $textTemplate = null, string $htmlTemplate = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
119
  {
120
    # code ...
121
  }
122
123
  public function createMultiPartMail($to, $subject, $txtTemplate, $htmlTemplate, $contentValues = array(), $from = null, $encoding = 'UTF-8')
124
  {
125
126
    $renderer = $this->getRenderer();
127
128
    $txtContent = $renderer->render($txtTemplate, $contentValues);
129
    $htmlContent = $renderer->render($htmlTemplate, $contentValues);
130
131
    $text = new MimePart($txtContent);
132
    $text->type = "text/plain";
133
    $text->charset = 'utf-8';
134
135
    $html = new MimePart($htmlContent);
136
    $html->type = "text/html";
137
    $html->charset = 'utf-8';
138
139
    $body = new MimeMessage();
140
    $body->setParts(array($text, $html));
141
142
    $mail = $this->getDefaultMessage($to, $subject, $body, $from);
143
    $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

143
    $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

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