Elgg  Version 3.0
Email.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg;
4 
5 use ElggEntity;
6 use ElggUser;
10 use Zend\Mime\Part;
11 
15 final class Email {
16 
20  protected $from;
21 
25  protected $to;
26 
30  protected $subject;
31 
35  protected $body;
36 
40  protected $params = [];
41 
45  protected $headers = [];
46 
50  protected $attachments = [];
51 
64  public static function factory(array $options = []) {
65  $from = elgg_extract('from', $options);
66  $to = elgg_extract('to', $options);
67  $subject = elgg_extract('subject', $options);
68  $body = elgg_extract('body', $options);
69  $params = elgg_extract('params', $options, []);
70  $headers = elgg_extract('headers', $options, []);
71 
72  $email = new self();
73  $email->setFrom(self::prepareFrom($from));
74  $email->setTo(self::prepareTo($to));
75  $email->setSubject($subject);
76  $email->setBody($body);
77  $email->setParams($params);
78  $email->setHeaders($headers);
79 
80  if (isset($params['attachments']) && is_array($params['attachments'])) {
81  foreach ($params['attachments'] as $attachment) {
82  $email->addAttachment($attachment);
83  }
84  }
85 
86  return $email;
87  }
88 
95  public function setFrom(Address $from) {
96  $this->from = $from;
97  return $this;
98  }
99 
104  public function getFrom() {
105  return $this->from;
106  }
107 
114  public function setTo(Address $to) {
115  $this->to = $to;
116  return $this;
117  }
118 
123  public function getTo() {
124  return $this->to;
125  }
126 
133  public function setSubject($subject = '') {
134  $this->subject = $subject;
135  return $this;
136  }
137 
142  public function getSubject() {
143  return $this->subject;
144  }
145 
152  public function setBody($body = '') {
153  $this->body = $body;
154  return $this;
155  }
156 
161  public function getBody() {
162  return $this->body;
163  }
164 
171  public function setParams(array $params = []) {
172  $this->params = $params;
173  return $this;
174  }
175 
180  public function getParams() {
181  return $this->params;
182  }
183 
191  public function addHeader($name, $value) {
192  $this->headers[$name] = $value;
193  return $this;
194  }
195 
202  public function setHeaders(array $headers = []) {
203  $this->headers = $headers;
204  return $this;
205  }
206 
211  public function getHeaders() {
212  return $this->headers;
213  }
214 
224  public function addAttachment($attachment) {
225 
226  if ($attachment instanceof Part) {
227  $this->attachments[] = $attachment;
228  return $this;
229  }
230 
231  if ($attachment instanceof \ElggFile) {
232  $this->attachments[] = Attachment::fromElggFile($attachment);
233  return $this;
234  }
235 
236  $attachment = Attachment::factory($attachment);
237  if (!empty($attachment)) {
238  $this->attachments[] = $attachment;
239  }
240 
241  return $this;
242  }
243 
249  public function getAttachments() {
250  return $this->attachments;
251  }
252 
260  protected static function prepareFrom($from) {
261  if (empty($from)) {
262  // get the site email address
264  $from = new Address($site->getEmailAddress(), $site->getDisplayName());
265  } else if ($from instanceof ElggEntity) {
266  // If there's an email address, use it - but only if it's not from a user.
267  if (!$from instanceof ElggUser && $from->email) {
268  $from = new Address($from->email, $from->getDisplayName());
269  } else {
270  // get the site email address
272  $from = new Address($site->getEmailAddress(), $site->getDisplayName());
273  }
274  } elseif (is_string($from)) {
275  $from = Address::fromString($from);
276  }
277 
278  if (!$from instanceof Address) {
279  throw new InvalidParameterException("From address is not in a valid format");
280  }
281 
282  return $from;
283  }
284 
292  protected static function prepareTo($to) {
293  if ($to instanceof ElggEntity) {
294  $to = new Address($to->email, $to->getDisplayName());
295  } elseif (is_string($to)) {
296  $to = Address::fromString($to);
297  }
298 
299  if (!$to instanceof Address) {
300  throw new InvalidParameterException("To address is not in a valid format");
301  }
302 
303  return $to;
304  }
305 }
setFrom(Address $from)
Sets sender address.
Definition: Email.php:95
getTo()
Returns recipient address.
Definition: Email.php:123
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
$params
Saves global plugin settings.
Definition: save.php:13
setParams(array $params=[])
Sets additional params.
Definition: Email.php:171
static prepareFrom($from)
Converts mixed input to an instance of Zend addres.
Definition: Email.php:260
getParams()
Returns additional params.
Definition: Email.php:180
setHeaders(array $headers=[])
Replaces header bag.
Definition: Email.php:202
static factory(array $options=[])
Create an email instance form an array of options.
Definition: Email.php:64
$email
Definition: register.php:18
setSubject($subject= '')
Sets email subject.
Definition: Email.php:133
setBody($body= '')
Sets the email message body.
Definition: Email.php:152
$options
Elgg admin footer.
Definition: footer.php:6
Configuration exception.
Email message.
Definition: Email.php:15
getSubject()
Returns the subject.
Definition: Email.php:142
addAttachment($attachment)
Add an attachment.
Definition: Email.php:224
elgg ElggUser
Definition: ElggUser.js:12
setTo(Address $to)
Sets recipient address.
Definition: Email.php:114
$body
Definition: useradd.php:60
elgg_get_site_entity()
Get the current site entity.
Definition: entities.php:130
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif(is_callable([$item, 'getType']))
Definition: item.php:39
getBody()
Returns email body.
Definition: Email.php:161
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:1131
$value
Definition: debugging.php:7
$attachments
Outputs attachments.
Definition: attachments.php:9
getHeaders()
Returns headers.
Definition: Email.php:211
Email address.
Definition: Address.php:15
getFrom()
Returns sender address.
Definition: Email.php:104
static prepareTo($to)
Converts mixed input to an instance of Zend addres.
Definition: Email.php:292
addHeader($name, $value)
Adds/replaces an HTTP/IMF header.
Definition: Email.php:191
elgg ElggEntity
Definition: ElggEntity.js:15
$subject
Definition: useradd.php:59
getAttachments()
Get all attachments.
Definition: Email.php:249