diff options
author | Camil Staps | 2016-07-31 09:40:25 +0200 |
---|---|---|
committer | Camil Staps | 2016-07-31 09:40:25 +0200 |
commit | 1ec8d12a7b125d41ecb723c5d83f2afef95408e9 (patch) | |
tree | 4d103ccbab48d2217e1ae13d37c229e4060e9184 /classes | |
parent | Give new files a key (diff) |
0.5.2 emails
Diffstat (limited to 'classes')
-rw-r--r-- | classes/BusinessAdmin.php | 22 | ||||
-rw-r--r-- | classes/Constants.php | 2 | ||||
-rw-r--r-- | classes/Contact.php | 11 | ||||
-rw-r--r-- | classes/Mail.php | 32 | ||||
-rw-r--r-- | classes/Mailer.php | 100 | ||||
-rw-r--r-- | classes/Offer.php | 26 |
6 files changed, 192 insertions, 1 deletions
diff --git a/classes/BusinessAdmin.php b/classes/BusinessAdmin.php index 7c3c76c..83fa4b3 100644 --- a/classes/BusinessAdmin.php +++ b/classes/BusinessAdmin.php @@ -407,6 +407,28 @@ class BusinessAdmin { } /** + * Create a new mail + * + * @param PDO $pdo The database connection + * @param int $contactId The contactId for the new mail + * @param int $offerId The offerId for the new mail + * @param string $subject + * + * @throws PDOException If something went wrong with the database + * + * @return Mail|bool A new instance of the Mail class, or false on failure + */ + public static function createMail($pdo, $contactId, $offerId, $subject) { + $stmt = $pdo->prepare("INSERT INTO `".Constants::db_prefix."mail` (`contactId`, `offerId`, `subject`) VALUES (?,?,?)"); + $stmt->execute([$contactId, $offerId, $subject]); + if ($stmt->rowCount() == 1) { + return new Mail($pdo, $pdo->lastInsertId()); + } else { + return false; + } + } + + /** * Format a date nicely * * @todo implement $relatively = true diff --git a/classes/Constants.php b/classes/Constants.php index fbac6cf..7528630 100644 --- a/classes/Constants.php +++ b/classes/Constants.php @@ -78,5 +78,5 @@ class Constants { const password_cost = 10; /** @const version Version of BusinessAdmin. Don't change this yourself! */ - const version = '0.5.1'; + const version = '0.5.2'; } diff --git a/classes/Contact.php b/classes/Contact.php index 6ab4803..4a82ade 100644 --- a/classes/Contact.php +++ b/classes/Contact.php @@ -73,4 +73,15 @@ class Contact extends Model { throw new Exception($error[2]); } } + + /** + * Make a mailer to send to this contact + * + * @return Mailer The mailer + */ + public function mailer() { + $mailer = new Mailer($this->pdo); + $mailer->addAddress($this->email, $this->name); + return $mailer; + } } diff --git a/classes/Mail.php b/classes/Mail.php new file mode 100644 index 0000000..dcc5cf5 --- /dev/null +++ b/classes/Mail.php @@ -0,0 +1,32 @@ +<?php +/** + * Provides the Mail 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 Model for the mail table + */ +class Mail extends Model { + /** {@inheritDoc} */ + public + $table = 'mail', + $fillable_columns = ['contactId', 'offerId', 'subject']; +} 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; + } +} diff --git a/classes/Offer.php b/classes/Offer.php index 16e0d20..7b0d2fc 100644 --- a/classes/Offer.php +++ b/classes/Offer.php @@ -294,6 +294,32 @@ class Offer extends Model{ } /** + * Make a mailer to send about this offer + * + * @return Mailer The mailer + */ + public function mailer() { + $mailer = new Mailer($this->pdo); + $mailer->setOffer($this); + return $mailer; + } + + /** + * Send the invoice to the contact + * + * @return bool The result of Mailer::send + */ + public function send() { + $mailer = $this->mailer(); + + $mailer->addAttachment($this->getInvoiceFile()->getFilenamePath()); + $mailer->Subject = 'Your invoice'; + $mailer->Body = 'Here is your invoice.'; + + return $mailer->send(); + } + + /** * Make a new assignment linked to this order * * @param string $title The title for this assignment |