aboutsummaryrefslogtreecommitdiff
path: root/include/ajax-email-offer.php
blob: 0f3476b7e7b2d171419f28c553f7ee323c2af45d (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
<?php
/**
 * 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/>.
 */

require_once(__DIR__ . '/../login-ajax.php');

try {
	$_offer = new Offer($_pdo, $_REQUEST['id']);
	$_mailer = $_offer->mailer();
} catch (Exception $e) {
	http_response_code(500);
	die($e->getMessage());
}

function format_email($address) {
	if ($address[1] != '') {
		return "{$address[1]} <{$address[0]}>";
	} else {
		return $address[0];
	}
}

if ($_SERVER['REQUEST_METHOD'] === 'GET') {
?>
	<form action='#' class='form-horizontal' method='post'>
		<input type='hidden' name='id' value='<?=$_offer->id?>'/>
		<div class='form-group'>
			<label class='col-sm-2 control-label'>From</label>
			<div class='col-sm-10'><input class='form-control input-sm' type='text' readonly='readonly' name='from' value='<?=$_mailer->FromName?> <<?=$_mailer->From?>>'/></div>
		</div>
		<?php foreach ($_mailer->getReplyToAddresses() as $addr) { ?>
		<div class='form-group'>
			<label class='col-sm-2 control-label'>Reply-To</label>
			<div class='col-sm-10'><input class='form-control input-sm' type='text' readonly='readonly' name='replyto[]' value='<?=format_email($addr)?>'/></div>
		</div>
		<?php } foreach ($_mailer->getBccAddresses() as $addr) { ?>
		<div class='form-group'>
			<label class='col-sm-2 control-label'>BCC</label>
			<div class='col-sm-10'><input class='form-control input-sm' type='text' readonly='readonly' name='bcc[]' value='<?=format_email($addr)?>'/></div>
		</div>
		<?php } foreach ($_mailer->getCcAddresses() as $addr) { ?>
		<div class='form-group'>
			<label class='col-sm-2 control-label'>CC</label>
			<div class='col-sm-10'><input class='form-control input-sm' type='text' readonly='readonly' name='cc[]' value='<?=format_email($addr)?>'/></div>
		</div>
		<?php } foreach ($_mailer->getToAddresses() as $addr) { ?>
		<div class='form-group'>
			<label class='col-sm-2 control-label'>To</label>
			<div class='col-sm-10'><input class='form-control input-sm' type='text' readonly='readonly' name='to[]' value='<?=format_email($addr)?>'/></div>
		</div>
		<?php } ?>
		<div class='form-group'>
			<label class='col-sm-2 control-label'>Subject</label>
			<div class='col-sm-10'><input class='form-control input-sm' type='text' name='subject' value='<?=$_mailer->Subject?>'/></div>
		</div>
		<div class='form-group'>
			<label class='col-sm-2 control-label'>Body</label>
			<div class='col-sm-10'><textarea class='form-control input-sm' rows='10' name='body'><?=$_mailer->Body?></textarea></div>
		</div>
	</form>
<?php
} elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {
	$_mailer->Subject = $_POST['subject'];
	$_mailer->Body = $_POST['body'];
	try {
		if ($_mailer->send()) {
			echo 'OK';
		} else {
			http_response_code(500);
			echo 'Sending the email failed:<br/>' . $_mailer->ErrorInfo;
		}
	} catch (Exception $e) {
		http_response_code(500);
		echo "Sending the email failed with an exception ({$e->getCode()}): {$e->getMessage()}<br/>" . $e->getTraceAsString();
	}
}