public function testInvalidEmails() { $this->assertFalse(Message::isValidEmail('müller@example.com'), 'Umlauts in the local part are not allowed'); $this->assertFalse(Message::isValidEmail('umlaut@müller.com'), 'Umlauts etc. in the domain part should only be accepted punycode encoded'); $this->assertFalse(Message::isValidEmail('trololo'), 'simple strings'); $this->assertFalse(Message::isValidEmail(''), 'empty strings'); $this->assertFalse(Message::isValidEmail(null), 'null'); $this->assertFalse(Message::isValidEmail(false), 'boolean false'); $this->assertFalse(Message::isValidEmail(true), 'boolean true'); $this->assertFalse(Message::isValidEmail(array()), 'empty array'); $this->assertFalse(Message::isValidEmail(new \stdClass()), 'standard class instance'); $this->assertFalse(Message::isValidEmail('@'), 'single @ character'); $this->assertFalse(Message::isValidEmail('a@b'), 'a@b'); $this->assertFalse(Message::isValidEmail('<foo>@example.com'), 'Characters < and > are not valid in email addresses'); $this->assertFalse(Message::isValidEmail('*****@*****.**'), 'Domain names longer than 63 characters are invalid.'); $this->assertFalse(Message::isValidEmail('example123example123example123example123example123example123example123iexample123example123' . 'example123example123example123example123example123example123example123example123example1234' . '56789012@example1example.example123example123example123example123example123.example123examp' . 'le123example123example123example123example123.com'), '320 octets/bytes are the maximum allowed length according to RFC 5322 and RFC 5321 valid emails'); $this->assertFalse(Message::isValidEmail('Someone other <*****@*****.**>'), 'Display names with email addresses may be valid, but are not support by us'); $this->assertFalse(Message::isValidEmail('"Someone other" <*****@*****.**>'), 'Quoted display names with email addresses may be valid, but are not support by us'); // this should be invalid according to SMTP, but is not according to PHPs email filter: //$this->assertFalse( // Message::isValidEmail('*****@*****.**'), // '64 characters are valid according to SMTP in the local part' //); }
/** * @param mixed $value email string or array with emails to check * @param string $setting_name * @param string $mailer_name * @param AgaviXmlConfigDomDocument $document * @param string $fieldname fieldname like 'to' etc. if it is a nested setting like 'address_overrides' * * @return void * * @throws ConfigError if given value is not a valid email or array of valid emails */ protected function validateEmailValue($value, $setting_name, $mailer_name, $document, $fieldname = null) { if (is_array($value)) { foreach ($value as $email) { if (!is_string($email) || empty($email) || !Message::isValidEmail($email)) { throw new ConfigError(sprintf('Configuration file "%s" specifies an invalid email address for ' . 'the "%s" setting %s on mailer "%s": %s', $document->documentURI, $setting_name, empty($fieldname) ? '' : "of the {$fieldname} field ", $mailer_name, $email)); } } } else { if (empty($value) || !Message::isValidEmail($value)) { throw new ConfigError(sprintf('Configuration file "%s" specifies an invalid email address for ' . 'the "%s" setting %s on mailer "%s": %s', $document->documentURI, $setting_name, empty($fieldname) ? '' : "of the {$fieldname} field ", $mailer_name, $value)); } } }