aboutsummaryrefslogtreecommitdiff
path: root/classes/Mailer.php
blob: 33068b7edf01cff80e5d436bad7c5be00cf6ab8e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?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;
		if (defined('SMTP_AUTH_TYPE'))
			$this->AuthType = SMTP_AUTH_TYPE;
		$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;
	}
}