Elgg  Version 3.0
Address.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Email;
4 
5 use Zend\Mail\Address as ZendAddress;
7 use Zend\Validator\EmailAddress as EmailAddressValidator;
9 
15 class Address extends ZendAddress {
16 
26  public function setEmail($email) {
27 
28  if (!is_string($email) || empty($email)) {
29  throw new InvalidArgumentException('Email must be a valid email address');
30  }
31 
32  if (preg_match("/[\r\n]/", $email)) {
33  throw new InvalidArgumentException('CRLF injection detected');
34  }
35 
36  $emailAddressValidator = new EmailAddressValidator(Hostname::ALLOW_DNS | Hostname::ALLOW_LOCAL);
37  if (!$emailAddressValidator->isValid($email)) {
38  $invalidMessages = $emailAddressValidator->getMessages();
39  throw new InvalidArgumentException(array_shift($invalidMessages));
40  }
41 
42  $this->email = $email;
43  }
44 
54  public function setName($name) {
55 
56  if (!is_string($name)) {
57  throw new InvalidArgumentException('Name must be a string');
58  }
59 
60  if (preg_match("/[\r\n]/", $name)) {
61  throw new InvalidArgumentException('CRLF injection detected');
62  }
63 
64  $this->name = $name;
65  }
66 
73  public function unsetName() {
74  $this->name = null;
75  }
76 
89  public static function fromString($contact, $ignored = null) {
90  $containsName = preg_match('/<(.*)>/', $contact, $matches) == 1;
91  if ($containsName) {
92  $name = trim(substr($contact, 0, strpos($contact, '<')));
93  return new self($matches[1], $name);
94  } else {
95  return new self(trim($contact));
96  }
97  }
98 
111  public static function getFormattedEmailAddress($email, $name = null) {
112  $mail = new self($email, $name);
113  return $mail->toString();
114  }
115 }
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
static fromString($contact, $ignored=null)
Parses strings like "Evan <evan@elgg.org>" into name/email objects.
Definition: Address.php:89
$email
Definition: register.php:18
static getFormattedEmailAddress($email, $name=null)
Format an email address and name into a formatted email address.
Definition: Address.php:111
setName($name)
Set the name.
Definition: Address.php:54
$site name
unsetName()
Clear the name from the email address.
Definition: Address.php:73
Email address.
Definition: Address.php:15
setEmail($email)
Set the email address.
Definition: Address.php:26
$site email