Passed
Push — develop ( 918c75...3b30cc )
by Daniel
01:42
created

Mailer::createNewMail()   B

Complexity

Conditions 9
Paths 7

Size

Total Lines 35
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 35
ccs 0
cts 18
cp 0
rs 8.0555
c 0
b 0
f 0
cc 9
nc 7
nop 3
crap 90
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
   * Neue Nachricht
30
   *
31
   * @param string $to Empfänger der Nachricht
32
   * @param string $subject Betreff der Nachricht
33
   * @param string $from Absender der Nachricht
34
   * @return Zend\Mail\Message Mail
0 ignored issues
show
Bug introduced by
The type ZfMailer\Service\Zend\Mail\Message was not found. Did you mean Zend\Mail\Message? If so, make sure to prefix the type with \.
Loading history...
35
   */
36
  public function createNewMail($to, $subject, $from = null)
37
  {
38
39
    // prüfen, ob Absender vorhanden ist
40
    if (!isset($from) || empty($from)) {
41
42
      $from = $this->getOptions()->getDefaultFrom();
43
44
      if (!isset($from) || empty($from)) {
45
        $this->setErrorMessage('Es wurde kein Absender angegeben oder konfiguriert.');
46
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type ZfMailer\Service\Zend\Mail\Message.
Loading history...
47
      }
48
49
      $from = $this->getOptions()->getDefaultFrom();
50
51
    }
52
53
    // Empfänger prüfen
54
    if (!isset($to) || empty($to)) {
55
      $this->setErrorMessage('Es wurde kein Empfänger angegeben.');
56
      return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type ZfMailer\Service\Zend\Mail\Message.
Loading history...
57
    }
58
59
    // Betreff prüfen
60
    if (!isset($subject) || empty($subject)) {
61
      $this->setErrorMessage('Es wurde kein Betreff angegeben.');
62
      return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type ZfMailer\Service\Zend\Mail\Message.
Loading history...
63
    }
64
65
    $message = $this->getMailMessage();
66
    $message->setFrom($from);
67
    $message->setTo($to);
68
    $message->setSubject($subject);
69
70
    return $message;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $message returns the type Zend\Mail\Message which is incompatible with the documented return type ZfMailer\Service\Zend\Mail\Message.
Loading history...
71
72
  }
73
  
74
  private function getDefaultMessage($to, $subject, $body, $from = null)
75
  {
76
77
    if (!isset($body) || empty($body)) {
78
      return false;
79
    }
80
81
    if (!isset($to) || empty($to)) {
82
      return false;
83
    }
84
85
    $message = $this->getMailMessage();
86
    $message->setSubject($subject)
87
            ->setBody($body)
88
            ->setTo($to);
89
90
    if (isset($from)) {
91
      $message->setFrom($from);
92
    }
93
94
    // if (isset($encoding)) {
95
    //   $message->setEncoding($encoding);
96
    // }
97
98
    // Todo: in Factory verschieben und aus config auslesen
99
    $message->getHeaders()->addHeaders(array('X-Mailer' => 'ZfMailer 1.0.1'));
100
101
    return $message;
102
103
  }
104
105
  public function createTextMail($to, $subject, $templateOrModel, $values = array(), $from, $encoding = 'UTF-8')
106
  {
107
    $renderer = $this->getRenderer();
108
    $body = $renderer->render($templateOrModel, $values);
109
110
    $mail = $this->getDefaultMessage($to, $subject, $body, $from);
111
    $mail->setEncoding($encoding);
112
    
113
    return $mail;
114
  }
115
116
  public function createMultiPartMail($to, $subject, $txtTemplate, $htmlTemplate, $contentValues = array(), $from = null, $encoding = 'UTF-8')
117
  {
118
119
    // Todo: siehe https://github.com/zendframework/zf2/issues/4917
120
    // Todo: Variablen prüfen
121
122
    $renderer = $this->getRenderer();
123
124
    $txtContent = $renderer->render($txtTemplate, $contentValues);
125
    $htmlContent = $renderer->render($htmlTemplate, $contentValues);
126
127
    $text = new MimePart($txtContent);
128
    $text->type = "text/plain";
129
    $text->charset = 'utf-8';
130
131
    $html = new MimePart($htmlContent);
132
    $html->type = "text/html";
133
    $html->charset = 'utf-8';
134
135
    $body = new MimeMessage();
136
    $body->setParts(array($text, $html));
137
138
    $mail = $this->getDefaultMessage($to, $subject, $body, $from);
139
    $mail->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

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

139
    $mail->getHeaders()->get('content-type')->/** @scrutinizer ignore-call */ setType('multipart/alternative');
Loading history...
140
    $mail->setEncoding($encoding);
141
142
    return $mail;
143
144
  }
145
146
  public function createMailWithAttachment()
147
  {
148
    return 'Das ist eine Mail mit Anhang';
149
  }
150
151
  public function sendEmail()
152
  {
153
154
    try {
155
      $this->getTransport()->send($this->getMailMessage());
156
      return true;
157
    } 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...
158
      //echo 'Exception abgefangen: ',  $e->getMessage(), "\n";
159
      $this->setErrorMessage($e->getMessage());
160
      return false;
161
    }
162
    
163
  }
164
165
  
166
167
}
168