2015-02-12 15:53:27 +03:00
|
|
|
<?php
|
2018-03-01 23:15:39 +03:00
|
|
|
declare(strict_types=1);
|
2015-02-12 15:53:27 +03:00
|
|
|
/**
|
2016-07-21 18:07:57 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2016-01-12 17:02:16 +03:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2015-03-26 13:44:34 +03:00
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
2015-02-12 15:53:27 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Mail;
|
|
|
|
|
2017-10-18 16:39:22 +03:00
|
|
|
use OCP\Mail\IAttachment;
|
2017-09-15 11:55:27 +03:00
|
|
|
use OCP\Mail\IEMailTemplate;
|
|
|
|
use OCP\Mail\IMessage;
|
2015-02-12 15:53:27 +03:00
|
|
|
use Swift_Message;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Message provides a wrapper around SwiftMail
|
|
|
|
*
|
|
|
|
* @package OC\Mail
|
|
|
|
*/
|
2017-09-15 11:55:27 +03:00
|
|
|
class Message implements IMessage {
|
2015-02-12 15:53:27 +03:00
|
|
|
/** @var Swift_Message */
|
|
|
|
private $swiftMessage;
|
2018-04-16 16:55:39 +03:00
|
|
|
/** @var bool */
|
|
|
|
private $plainTextOnly;
|
2015-02-12 15:53:27 +03:00
|
|
|
|
2018-04-16 16:55:39 +03:00
|
|
|
public function __construct(Swift_Message $swiftMessage, bool $plainTextOnly) {
|
2015-02-12 15:53:27 +03:00
|
|
|
$this->swiftMessage = $swiftMessage;
|
2018-04-16 16:55:39 +03:00
|
|
|
$this->plainTextOnly = $plainTextOnly;
|
2015-02-12 15:53:27 +03:00
|
|
|
}
|
|
|
|
|
2017-10-18 16:39:22 +03:00
|
|
|
/**
|
|
|
|
* @param IAttachment $attachment
|
|
|
|
* @return $this
|
|
|
|
* @since 13.0.0
|
|
|
|
*/
|
2018-03-01 23:15:39 +03:00
|
|
|
public function attach(IAttachment $attachment): IMessage {
|
2017-10-18 16:39:22 +03:00
|
|
|
/** @var Attachment $attachment */
|
|
|
|
$this->swiftMessage->attach($attachment->getSwiftAttachment());
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-02-12 15:53:27 +03:00
|
|
|
/**
|
|
|
|
* SwiftMailer does currently not work with IDN domains, this function therefore converts the domains
|
|
|
|
* FIXME: Remove this once SwiftMailer supports IDN
|
|
|
|
*
|
|
|
|
* @param array $addresses Array of mail addresses, key will get converted
|
|
|
|
* @return array Converted addresses if `idn_to_ascii` exists
|
|
|
|
*/
|
2018-03-01 23:15:39 +03:00
|
|
|
protected function convertAddresses(array $addresses): array {
|
2018-04-06 02:09:08 +03:00
|
|
|
if (!function_exists('idn_to_ascii') || !defined('INTL_IDNA_VARIANT_UTS46')) {
|
2015-02-12 15:53:27 +03:00
|
|
|
return $addresses;
|
|
|
|
}
|
|
|
|
|
2018-03-01 23:15:39 +03:00
|
|
|
$convertedAddresses = [];
|
2015-02-12 15:53:27 +03:00
|
|
|
|
|
|
|
foreach($addresses as $email => $readableName) {
|
|
|
|
if(!is_numeric($email)) {
|
|
|
|
list($name, $domain) = explode('@', $email, 2);
|
2017-08-04 19:06:17 +03:00
|
|
|
$domain = idn_to_ascii($domain, 0, INTL_IDNA_VARIANT_UTS46);
|
2015-02-12 15:53:27 +03:00
|
|
|
$convertedAddresses[$name.'@'.$domain] = $readableName;
|
|
|
|
} else {
|
|
|
|
list($name, $domain) = explode('@', $readableName, 2);
|
2017-08-04 19:06:17 +03:00
|
|
|
$domain = idn_to_ascii($domain, 0, INTL_IDNA_VARIANT_UTS46);
|
2015-02-12 15:53:27 +03:00
|
|
|
$convertedAddresses[$email] = $name.'@'.$domain;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $convertedAddresses;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the from address of this message.
|
|
|
|
*
|
|
|
|
* If no "From" address is used \OC\Mail\Mailer will use mail_from_address and mail_domain from config.php
|
|
|
|
*
|
|
|
|
* @param array $addresses Example: array('sender@domain.org', 'other@domain.org' => 'A name')
|
|
|
|
* @return $this
|
|
|
|
*/
|
2018-03-01 23:15:39 +03:00
|
|
|
public function setFrom(array $addresses): IMessage {
|
2015-02-12 15:53:27 +03:00
|
|
|
$addresses = $this->convertAddresses($addresses);
|
|
|
|
|
|
|
|
$this->swiftMessage->setFrom($addresses);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the from address of this message.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2018-03-01 23:15:39 +03:00
|
|
|
public function getFrom(): array {
|
2015-02-12 15:53:27 +03:00
|
|
|
return $this->swiftMessage->getFrom();
|
|
|
|
}
|
|
|
|
|
2015-04-10 16:05:34 +03:00
|
|
|
/**
|
|
|
|
* Set the Reply-To address of this message
|
|
|
|
*
|
|
|
|
* @param array $addresses
|
|
|
|
* @return $this
|
|
|
|
*/
|
2018-03-01 23:15:39 +03:00
|
|
|
public function setReplyTo(array $addresses): IMessage {
|
2015-04-10 16:05:34 +03:00
|
|
|
$addresses = $this->convertAddresses($addresses);
|
|
|
|
|
|
|
|
$this->swiftMessage->setReplyTo($addresses);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the Reply-To address of this message
|
|
|
|
*
|
2018-03-01 23:15:39 +03:00
|
|
|
* @return string
|
2015-04-10 16:05:34 +03:00
|
|
|
*/
|
2018-03-01 23:15:39 +03:00
|
|
|
public function getReplyTo(): string {
|
2015-04-10 16:05:34 +03:00
|
|
|
return $this->swiftMessage->getReplyTo();
|
|
|
|
}
|
|
|
|
|
2015-02-12 15:53:27 +03:00
|
|
|
/**
|
|
|
|
* Set the to addresses of this message.
|
|
|
|
*
|
|
|
|
* @param array $recipients Example: array('recipient@domain.org', 'other@domain.org' => 'A name')
|
|
|
|
* @return $this
|
|
|
|
*/
|
2018-03-01 23:15:39 +03:00
|
|
|
public function setTo(array $recipients): IMessage {
|
2015-02-12 15:53:27 +03:00
|
|
|
$recipients = $this->convertAddresses($recipients);
|
|
|
|
|
|
|
|
$this->swiftMessage->setTo($recipients);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the to address of this message.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2018-03-01 23:15:39 +03:00
|
|
|
public function getTo(): array {
|
2015-02-12 15:53:27 +03:00
|
|
|
return $this->swiftMessage->getTo();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the CC recipients of this message.
|
|
|
|
*
|
|
|
|
* @param array $recipients Example: array('recipient@domain.org', 'other@domain.org' => 'A name')
|
|
|
|
* @return $this
|
|
|
|
*/
|
2018-03-01 23:15:39 +03:00
|
|
|
public function setCc(array $recipients): IMessage {
|
2015-02-12 15:53:27 +03:00
|
|
|
$recipients = $this->convertAddresses($recipients);
|
|
|
|
|
|
|
|
$this->swiftMessage->setCc($recipients);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the cc address of this message.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2018-03-01 23:15:39 +03:00
|
|
|
public function getCc(): array {
|
2015-02-12 15:53:27 +03:00
|
|
|
return $this->swiftMessage->getCc();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the BCC recipients of this message.
|
|
|
|
*
|
|
|
|
* @param array $recipients Example: array('recipient@domain.org', 'other@domain.org' => 'A name')
|
|
|
|
* @return $this
|
|
|
|
*/
|
2018-03-01 23:15:39 +03:00
|
|
|
public function setBcc(array $recipients): IMessage {
|
2015-02-12 15:53:27 +03:00
|
|
|
$recipients = $this->convertAddresses($recipients);
|
|
|
|
|
|
|
|
$this->swiftMessage->setBcc($recipients);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the Bcc address of this message.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2018-03-01 23:15:39 +03:00
|
|
|
public function getBcc(): array {
|
2015-02-12 15:53:27 +03:00
|
|
|
return $this->swiftMessage->getBcc();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the subject of this message.
|
|
|
|
*
|
2018-03-05 11:06:40 +03:00
|
|
|
* @param string $subject
|
|
|
|
* @return IMessage
|
2015-02-12 15:53:27 +03:00
|
|
|
*/
|
2018-03-05 11:06:40 +03:00
|
|
|
public function setSubject(string $subject): IMessage {
|
2015-02-12 15:53:27 +03:00
|
|
|
$this->swiftMessage->setSubject($subject);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the from subject of this message.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2018-03-01 23:15:39 +03:00
|
|
|
public function getSubject(): string {
|
2015-02-12 15:53:27 +03:00
|
|
|
return $this->swiftMessage->getSubject();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the plain-text body of this message.
|
|
|
|
*
|
|
|
|
* @param string $body
|
|
|
|
* @return $this
|
|
|
|
*/
|
2018-03-01 23:15:39 +03:00
|
|
|
public function setPlainBody(string $body): IMessage {
|
2015-02-12 15:53:27 +03:00
|
|
|
$this->swiftMessage->setBody($body);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the plain body of this message.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2018-03-01 23:15:39 +03:00
|
|
|
public function getPlainBody(): string {
|
2015-02-12 15:53:27 +03:00
|
|
|
return $this->swiftMessage->getBody();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the HTML body of this message. Consider also sending a plain-text body instead of only an HTML one.
|
|
|
|
*
|
|
|
|
* @param string $body
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setHtmlBody($body) {
|
2018-04-16 16:55:39 +03:00
|
|
|
if (!$this->plainTextOnly) {
|
|
|
|
$this->swiftMessage->addPart($body, 'text/html');
|
|
|
|
}
|
2015-02-12 15:53:27 +03:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get's the underlying SwiftMessage
|
|
|
|
* @return Swift_Message
|
|
|
|
*/
|
2018-03-01 23:15:39 +03:00
|
|
|
public function getSwiftMessage(): Swift_Message {
|
2015-02-12 15:53:27 +03:00
|
|
|
return $this->swiftMessage;
|
|
|
|
}
|
2015-12-18 13:56:25 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $body
|
|
|
|
* @param string $contentType
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setBody($body, $contentType) {
|
2018-04-16 16:55:39 +03:00
|
|
|
if (!$this->plainTextOnly || $contentType !== 'text/html') {
|
|
|
|
$this->swiftMessage->setBody($body, $contentType);
|
|
|
|
}
|
2015-12-18 13:56:25 +03:00
|
|
|
return $this;
|
|
|
|
}
|
2017-09-15 11:55:27 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param IEMailTemplate $emailTemplate
|
|
|
|
* @return $this
|
|
|
|
*/
|
2018-03-01 23:15:39 +03:00
|
|
|
public function useTemplate(IEMailTemplate $emailTemplate): IMessage {
|
2017-09-15 11:55:27 +03:00
|
|
|
$this->setSubject($emailTemplate->renderSubject());
|
|
|
|
$this->setPlainBody($emailTemplate->renderText());
|
2018-04-16 16:55:39 +03:00
|
|
|
if (!$this->plainTextOnly) {
|
|
|
|
$this->setHtmlBody($emailTemplate->renderHtml());
|
|
|
|
}
|
2017-09-15 11:55:27 +03:00
|
|
|
return $this;
|
|
|
|
}
|
2015-02-12 15:53:27 +03:00
|
|
|
}
|