diff options
Diffstat (limited to 'classes/Mailer.php')
-rw-r--r-- | classes/Mailer.php | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/classes/Mailer.php b/classes/Mailer.php new file mode 100644 index 0000000..782afc3 --- /dev/null +++ b/classes/Mailer.php @@ -0,0 +1,100 @@ +<?php +/** + * Provides the Mailer class + * + * @author Camil Staps + * + * BusinessAdmin: administrative software for small companies + * Copyright (C) 2015 Camil Staps (ViviSoft) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +/** + * An extension to PHPMailer to set some defaults + */ +class Mailer extends PHPMailer { + /** + * {@inheritDoc} + * + * @param PDO $_pdo A PDO instance for database connection + * @param mixed $exceptions For PHPMailer::__construct() + */ + public function __construct($_pdo, $exceptions = null) { + parent::__construct($exceptions); + + $this->pdo = $_pdo; + + $this->isSMTP(); + $this->Host = SMTP_HOST; + $this->SMTPAuth = SMTP_AUTH; + $this->Username = SMTP_USERNAME; + $this->Password = SMTP_PASSWORD; + $this->SMTPSecure = SMTP_SECURE; + $this->Port = SMTP_PORT; + + if (defined('SMTP_OPTIONS')) + $this->SMTPOptions = json_decode(SMTP_OPTIONS, true); + + $from = explode(';', MAILER_FROM); + $this->setFrom($from[0], $from[1]); + + if (defined('MAILER_REPLY_TO')) { + $replyto = explode(';', MAILER_REPLY_TO); + $this->addReplyTo($replyto[0], $replyto[1]); + } + + if (defined('MAILER_CC')) + foreach (explode(';', MAILER_CC) as $cc) + $this->addCC($cc); + if (defined('MAILER_BCC')) + foreach (explode(';', MAILER_BCC) as $bcc) + $this->addBCC($bcc); + } + + /** + * Set the contact this mail should be sent to + * + * @param Contact $contact The contact + */ + public function setContact($contact) { + $this->addAddress($contact->email, $contact->name); + $this->contactId = $contact->id; + } + + /** + * Set the offer this email is about + * + * @param Offer $offer The offer + */ + public function setOffer($offer) { + $this->setContact($offer->getContact()); + $this->offerId = $offer->id; + } + + /** + * {@inheritDoc} + * + * Then store it in the database. + */ + public function send() { + if (!parent::send()) { + return false; + } + + BusinessAdmin::createMail($this->pdo, $this->contactId, $this->offerId, $this->Subject); + + return true; + } +} |