Issues (5)

src/Service/Mailer.php (3 issues)

Labels
Severity
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    3.0.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 1
    if (!isset($from) || empty($from)) {
50
51 1
      $from = $this->getOptions()->getDefaultFrom();
52
53 1
      if (!isset($from) || empty($from)) {
54 1
        $this->setErrorMessage('Es wurde kein Absender angegeben oder konfiguriert.');
55 1
        return false;
56
      }
57
58
    }
59
60 1
    if (!isset($to) || empty($to)) {
61 1
      $this->setErrorMessage('Es wurde kein Empfänger angegeben.');
62 1
      return false;
63
    }
64
65 1
    if (!isset($subject) || empty($subject)) {
66 1
      $this->setErrorMessage('Es wurde kein Betreff angegeben.');
67 1
      return false;
68
    }
69
70 1
    $message = $this->getMailMessage();
71 1
    $message->setFrom($from);
72 1
    $message->setTo($to);
73 1
    $message->setSubject($subject);
74
75 1
    return $message;
76
77
  }
78
79
  /**
80
   * Bereitet die E-Mail vor, fügt die Content-Variablen ein, rendert das Template
81
   * und gibt ein fertiges Zend\Mail\Message Objekt zurück
82
   * 
83
   * @param array $contentValues Array mit Werten, die in deie E-Mail eingefügt werden
84
   * @param string|null $textTemplate Template für eine E-Mail im Textformat
85
   * @return \Zend\Mail\Message|boolean fertiges Mail-Objekt
86
   */
87 1
  public function prepareAsText(array $contentValues, string $textTemplate = null)
88
  {
89
90 1
    $message = $this->getMailMessage();
91
92 1
    if (isset($textTemplate) || !empty($textTemplate)) {
93 1
      $this->textTemplate = $textTemplate;
94
    }
95
96 1
    $renderer = $this->getRenderer();
97 1
    $body = $renderer->render($this->textTemplate, $contentValues);
98 1
    $message->setBody($body);
99
    
100 1
  }
101
102
  /**
103
   * Bereitet die E-Mail vor, fügt die Content-Variablen ein, rendert die Templates
104
   * fügt die Teile für Text- und HTML-Inhalte zusammen und gibt ein fertiges
105
   * Zend\Mail\Message Objekt zurück
106
   * 
107
   * @param array $contentValues Array mit Werten, die in die E-Mail eingefügt werden
108
   * @param string|null $textTemplate Template für den Text-Teil der E-Mail
109
   * @param string|null $htmlTemplate Template für den HTML-Teil der E-Mail
110
   */
111 1
  public function prepareAsMultipart(array $contentValues, string $textTemplate = null, string $htmlTemplate = null)
112
  {
113 1
    $defaultEncoding = $this->getOptions()->getEncoding();
114
115 1
    $message = $this->getMailMessage();
116 1
    $renderer = $this->getRenderer();
117
118 1
    if (isset($textTemplate) || !empty($textTemplate)) {
119 1
      $this->textTemplate = $textTemplate;
120
    }
121
122 1
    if (isset($htmlTemplate) || !empty($htmlTemplate)) {
123 1
      $this->htmlTemplate = $htmlTemplate;
124
    }
125
126 1
    $textContent = $renderer->render($this->textTemplate, $contentValues);
127 1
    $htmlContent = $renderer->render($this->htmlTemplate, $contentValues);
128
129 1
    $textPart = new MimePart($textContent);
130 1
    $textPart->type = Mime::TYPE_TEXT;
131 1
    $textPart->charset = $defaultEncoding;
132 1
    $textPart->encoding = Mime::ENCODING_QUOTEDPRINTABLE;
133
134 1
    $htmlPart = new MimePart($htmlContent);
135 1
    $htmlPart->type = Mime::TYPE_HTML;
136 1
    $htmlPart->charset = $defaultEncoding;
137 1
    $htmlPart->encoding = Mime::ENCODING_QUOTEDPRINTABLE;
138
139 1
    $mailBody = new MimeMessage();
140 1
    $mailBody->setParts([$textPart, $htmlPart]);
141
142 1
    $message->setBody($mailBody);
143 1
    $message->getHeaders()->get('content-type')->setType('multipart/alternative');
0 ignored issues
show
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
    $message->getHeaders()->get('content-type')->/** @scrutinizer ignore-call */ setType('multipart/alternative');
Loading history...
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
    $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...
144
    
145 1
  }
146
147
  /**
148
   * Versendet die E-Mails
149
   * @return Boolean Gibt True zurück, wenn die E-Mail erfolgreich versendet wurde, sonst False
150
   */
151
  public function sendEmail()
152
  {
153
154
    $message = $this->getMailMessage();
155
156
    $xMailer = $this->getOptions()->getXMailer();
157
    $organization = $this->getOptions()->getOrganization();
158
    $returnPath = $this->getOptions()->getReturnPath();
159
    $replyTo = $this->getOptions()->getReplyTo();
160
161
    if (isset($xMailer) && !empty($xMailer)) {
162
      $message->getHeaders()->addHeaders(array('X-Mailer' => $xMailer));
163
    }
164
165
    if (isset($organization) && !empty($organization)) {
166
      $message->getHeaders()->addHeaders(array('Organization' => $organization));
167
    }
168
169
    if (isset($returnPath) && !empty($returnPath)) {
170
      $message->getHeaders()->addHeaders(array('Return-Path' => $returnPath));
171
    }
172
173
    if (isset($replyTo) && !empty($replyTo)) {
174
      $message->getHeaders()->addHeaders(array('Reply-To' => $replyTo));
175
    }
176
177
    try {
178
      $this->getTransport()->send($message);
179
      return true;
180
    } catch (RuntimeException $e) {
0 ignored issues
show
The type ZfMailer\Service\RuntimeException was not found. Did you mean RuntimeException? If so, make sure to prefix the type with \.
Loading history...
181
      $this->setErrorMessage($e->getMessage());
182
      return false;
183
    }
184
    
185
  }
186
187
}
188