/
Message.php
179 lines (166 loc) · 5.75 KB
/
Message.php
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
/**
* Copyright 2021 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
*/
namespace Google\AppEngine\Api\Mail;
use google\appengine\base\VoidProto;
use Google\AppEngine\Runtime\ApiProxy;
use Google\AppEngine\Runtime\ApplicationError;
/**
* Allows users to send mail using the App Engine mail APIs.
*/
final class Message extends BaseMessage {
/**
* Adds a "bcc" address, or array of addresses, to the mail object.
*
* @param mixed $emails String email of individual recipient, or array of
* emails of recipients.
* @throws \InvalidArgumentException If any of the input emails are not
* correctly formatted email addresses.
*/
public function addBcc($emails) {
$email_array = $this->validEmailsArray($emails, "'bcc' recipient");
foreach($email_array as $email) {
$this->message->addBcc($email);
}
}
/**
* Adds a "cc" address, or array of addresses, to the mail object.
*
* @param mixed $emails String email of individual recipient, or array of
* emails of recipients.
* @throws \InvalidArgumentException If any of the input emails are not
* correctly formatted email addresses.
*/
public function addCc($emails) {
$email_array = $this->validEmailsArray($emails, "'cc' recipient");
foreach($email_array as $email) {
$this->message->addCc($email);
}
}
/**
* Adds a "to" address, or array of addresses, to the mail object.
*
* @param mixed $emails String email of individual recipient, or array of
* emails of recipients.
* @throws \InvalidArgumentException If any of the input emails are not
* correctly formatted email addresses.
*/
public function addTo($emails) {
$email_array = $this->validEmailsArray($emails, "'to' recipient");
foreach($email_array as $email) {
$this->message->addTo($email);
}
}
/**
* Clears all "bcc" addresses from the mail object.
*/
public function clearBcc() {
$this->message->clearBcc();
}
/**
* Clears all "cc" addresses from the mail object.
*/
public function clearCc() {
$this->message->clearCc();
}
/**
* Clear reply-to address from the mail object.
*/
public function clearReplyTo() {
$this->message->clearReplyto();
}
/**
* Clears all "to" addresses from the mail object.
*/
public function clearTo() {
$this->message->clearTo();
}
/**
* Returns the class variable $set_functions array, or the corresponding
* value in that array if a key is provided.
*
* @return mixed $set_functions array, or a string value from that array.
*/
protected function getFunctionArray() {
return self::$set_functions;
}
/**
* Send the pre-formed email from the Message object.
*
* @throws \InvalidArgumentException If a required field (sender, recipient
* [to, cc or bcc], subject, body [plain or html]) is missing, or if an
* ApplicationError was thrown by the RPC call due to an unauthorized
* sender, an invalid attachment type, or an invalid header name.
* @throws \RuntimeException If an ApplicationError was thrown by the RPC call
* due to an internal error or bad request.
* @throws ApplicationError If there was an unknown error in the RPC call.
*/
public function send() {
if (!$this->message->hasSender()) {
throw new \InvalidArgumentException(
"Required field sender is not provided.");
} else if ($this->message->getToSize() == 0 &&
$this->message->getCcSize() == 0 &&
$this->message->getBccSize() == 0) {
throw new \InvalidArgumentException(
"Neither to, cc or bcc is set - at least one is required.");
} else if (!$this->message->hasSubject()) {
throw new \InvalidArgumentException(
"Required field subject is not provided.");
} else if (!$this->message->hasTextbody() &&
!$this->message->hasHtmlbody()) {
throw new \InvalidArgumentException(
"Neither a plain-text nor HTML body is provided - at least one is " .
"required.");
}
$response = new VoidProto();
try {
ApiProxy::makeSyncCall(
'mail', 'Send', $this->message, $response);
} catch(ApplicationError $e) {
$this->handleApplicationError($e);
}
}
/**
* Checks that an email input is either:
* - An array, and each array member is a valid email
* - A single valid email string
* And subsequently returns an array of the valid emails.
*
* @param mixed $emails An array of emails string, or a single email string
* @return An array of emails.
* @throws \InvalidArgumentException If any of the input emails are not
* correctly formatted email addresses.
*/
protected function validEmailsArray($email_list, $err_str) {
if (is_string($email_list) && $this->checkValidEmail($email_list)) {
return array($email_list);
} else if (!is_array($email_list)) {
$error = sprintf("Invalid %s: %s", $err_str, $email_list);
throw new \InvalidArgumentException(htmlspecialchars($error));
} else {
foreach($email_list as $email) {
if (!$this->checkValidEmail($email)) {
$error = sprintf("Invalid %s: %s", $err_str, $email);
throw new \InvalidArgumentException(htmlspecialchars($error));
}
}
return $email_list;
}
}
}