Passed
Push — develop ( 977ef2...7ca042 )
by Daniel
01:54
created

Mailer::createNewMail()   B

Complexity

Conditions 9
Paths 7

Size

Total Lines 33
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 9

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 33
ccs 17
cts 17
cp 1
rs 8.0555
c 0
b 0
f 0
cc 9
nc 7
nop 3
crap 9
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
    }
60
61
    // Empfänger prüfen
62 1
    if (!isset($to) || empty($to)) {
63 1
      $this->setErrorMessage('Es wurde kein Empfänger angegeben.');
64 1
      return false;
65
    }
66
67
    // Betreff prüfen
68 1
    if (!isset($subject) || empty($subject)) {
69 1
      $this->setErrorMessage('Es wurde kein Betreff angegeben.');
70 1
      return false;
71
    }
72
73 1
    $message = $this->getMailMessage();
74 1
    $message->setFrom($from);
75 1
    $message->setTo($to);
76 1
    $message->setSubject($subject);
77
78 1
    return $message;
79
80
  }
81
82
  /**
83
   * Bereitet die E-Mail vor, fügt die Content-Variablen ein, rendert das Template
84
   * und gibt ein fertiges Zend\Mail\Message Objekt zurück
85
   * 
86
   * @param array $contentValues Array mit Werten, die in deie E-Mail eingefügt werden
87
   * @param string|null $textTemplate Template für eine E-Mail im Textformat
88
   * @return \Zend\Mail\Message|boolean fertiges Mail-Objekt
89
   */
90 1
  public function prepareAsText(array $contentValues, string $textTemplate = null)
91
  {
92
93 1
    $message = $this->getMailMessage();
94
95 1
    if (isset($textTemplate) || !empty($textTemplate)) {
96 1
      $this->textTemplate = $textTemplate;
97
    }
98
99 1
    $renderer = $this->getRenderer();
100 1
    $body = $renderer->render($this->textTemplate, $contentValues);
101 1
    $message->setBody($body);
102
    
103 1
  }
104
105
  /**
106
   * Bereitet die E-Mail vor, fügt die Content-Variablen ein, rendert die Templates
107
   * fügt die Teile für Text- und HTML-Inhalte zusammen und gibt ein fertiges
108
   * Zend\Mail\Message Objekt zurück
109
   * 
110
   * @param array $contentValues Array mit Werten, die in die E-Mail eingefügt werden
111
   * @param string|null $textTemplate Template für den Text-Teil der E-Mail
112
   * @param string|null $htmlTemplate Template für den HTML-Teil der E-Mail
113
   */
114 1
  public function prepareAsMultipart(array $contentValues, string $textTemplate = null, string $htmlTemplate = null)
115
  {
116 1
    $defaultEncoding = $this->getOptions()->getEncoding();
117
118 1
    $message = $this->getMailMessage();
119 1
    $renderer = $this->getRenderer();
120
121 1
    if (isset($textTemplate) || !empty($textTemplate)) {
122 1
      $this->textTemplate = $textTemplate;
123
    }
124
125 1
    if (isset($htmlTemplate) || !empty($htmlTemplate)) {
126 1
      $this->htmlTemplate = $htmlTemplate;
127
    }
128
129 1
    $textContent = $renderer->render($this->textTemplate, $contentValues);
130 1
    $htmlContent = $renderer->render($this->htmlTemplate, $contentValues);
131
132 1
    $textPart = new MimePart($textContent);
133 1
    $textPart->type = Mime::TYPE_TEXT;
134 1
    $textPart->charset = $defaultEncoding;
135 1
    $textPart->encoding = Mime::ENCODING_QUOTEDPRINTABLE;
136
137 1
    $htmlPart = new MimePart($htmlContent);
138 1
    $htmlPart->type = Mime::TYPE_HTML;
139 1
    $htmlPart->charset = $defaultEncoding;
140 1
    $htmlPart->encoding = Mime::ENCODING_QUOTEDPRINTABLE;
141
142 1
    $mailBody = new MimeMessage();
143 1
    $mailBody->setParts([$textPart, $htmlPart]);
144
145 1
    $message->setBody($mailBody);
146 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

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

146
    $message->getHeaders()->get('content-type')->/** @scrutinizer ignore-call */ setType('multipart/alternative');
Loading history...
147
    
148 1
  }
149
150
  /**
151
   * Versendet die E-Mails
152
   * @return Boolean Gibt True zurück, wenn die E-Mail erfolgreich versendet wurde, sonst False
153
   */
154
  public function sendEmail()
155
  {
156
157
    $message = $this->getMailMessage();
158
159
    $xMailer = $this->getOptions()->getXMailer();
160
    $organization = $this->getOptions()->getOrganization();
161
    $returnPath = $this->getOptions()->getReturnPath();
162
    $replyTo = $this->getOptions()->getReplyTo();
163
164
    if (isset($xMailer) && !empty($xMailer)) {
165
      $message->getHeaders()->addHeaders(array('X-Mailer' => $xMailer));
166
    }
167
168
    if (isset($organization) && !empty($organization)) {
169
      $message->getHeaders()->addHeaders(array('Organization' => $organization));
170
    }
171
172
    if (isset($returnPath) && !empty($returnPath)) {
173
      $message->getHeaders()->addHeaders(array('Return-Path' => $returnPath));
174
    }
175
176
    if (isset($replyTo) && !empty($replyTo)) {
177
      $message->getHeaders()->addHeaders(array('Reply-To' => $replyTo));
178
    }
179
180
    try {
181
      $this->getTransport()->send($message);
182
      return true;
183
    } 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...
184
      $this->setErrorMessage($e->getMessage());
185
      return false;
186
    }
187
    
188
  }
189
190
}
191