From 4ada86876033fa171e2896d7e3d3c5645d8062db Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 28 Apr 2024 14:46:47 +0200 Subject: Adding upstream version 0.10.0. Signed-off-by: Daniel Baumann --- library/Reporting/Mail.php | 180 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 180 insertions(+) create mode 100644 library/Reporting/Mail.php (limited to 'library/Reporting/Mail.php') diff --git a/library/Reporting/Mail.php b/library/Reporting/Mail.php new file mode 100644 index 0000000..7581f45 --- /dev/null +++ b/library/Reporting/Mail.php @@ -0,0 +1,180 @@ +from)) { + return $this->from; + } + + if (isset($_SERVER['SERVER_ADMIN'])) { + $this->from = $_SERVER['SERVER_ADMIN']; + + return $this->from; + } + + foreach (['HTTP_HOST', 'SERVER_NAME', 'HOSTNAME'] as $key) { + if (isset($_SEVER[$key])) { + $this->from = 'icinga-reporting@' . $_SERVER[$key]; + + return $this->from; + } + } + + $this->from = 'icinga-reporting@localhost'; + + return $this->from; + } + + /** + * Set the from part + * + * @param string $from + * + * @return $this + */ + public function setFrom($from) + { + $this->from = $from; + + return $this; + } + + /** + * Get the subject + * + * @return string + */ + public function getSubject() + { + return $this->subject; + } + + /** + * Set the subject + * + * @param string $subject + * + * @return $this + */ + public function setSubject($subject) + { + $this->subject = $subject; + + return $this; + } + + /** + * Get the mail transport + * + * @return Zend_Mail_Transport_Sendmail + */ + public function getTransport() + { + if (! isset($this->transport)) { + $this->transport = new Zend_Mail_Transport_Sendmail('-f ' . escapeshellarg($this->getFrom())); + } + + return $this->transport; + } + + public function attachCsv($csv, $filename) + { + if (is_array($csv)) { + $csv = Str::putcsv($csv); + } + + $attachment = new Zend_Mime_Part($csv); + + $attachment->type = 'text/csv'; + $attachment->disposition = Zend_Mime::DISPOSITION_ATTACHMENT; + $attachment->encoding = Zend_Mime::ENCODING_BASE64; + $attachment->filename = basename($filename, '.csv') . '.csv'; + + $this->attachments[] = $attachment; + + return $this; + } + + public function attachJson($json, $filename) + { + if (is_array($json)) { + $json = json_encode($json); + } + + $attachment = new Zend_Mime_Part($json); + + $attachment->type = 'application/json'; + $attachment->disposition = Zend_Mime::DISPOSITION_ATTACHMENT; + $attachment->encoding = Zend_Mime::ENCODING_BASE64; + $attachment->filename = basename($filename, '.json') . '.json'; + + $this->attachments[] = $attachment; + + return $this; + } + + public function attachPdf($pdf, $filename) + { + $attachment = new Zend_Mime_Part($pdf); + + $attachment->type = 'application/pdf'; + $attachment->disposition = Zend_Mime::DISPOSITION_ATTACHMENT; + $attachment->encoding = Zend_Mime::ENCODING_BASE64; + $attachment->filename = basename($filename, '.pdf') . '.pdf'; + + $this->attachments[] = $attachment; + + return $this; + } + + public function send($body, $recipient) + { + $mail = new Zend_Mail('UTF-8'); + + $mail->setFrom($this->getFrom()); + $mail->addTo($recipient); + $mail->setSubject($this->getSubject()); + + if (strlen($body) !== strlen(strip_tags($body))) { + $mail->setBodyHtml($body); + } else { + $mail->setBodyText($body); + } + + foreach ($this->attachments as $attachment) { + $mail->addAttachment($attachment); + } + + $mail->send($this->getTransport()); + } +} -- cgit v1.2.3