/** * @param string $to * @return $this * @throws \InvalidArgumentException */ public function setTo($to) { if (is_string($to) && Email::isEmailAddressValid($to)) { $this->to = $to; return $this; } throw new \InvalidArgumentException('Incorrect email address given.'); }
public function test_it_returns_false_on_invalid_email_address() { $emailHelper = new Email(); $invalidEmails = ['plainaddress', '#@%^%#$@#$@#.com', '@example.com', 'Joe Smith <*****@*****.**>', 'email.example.com', 'email@example@example.com', '*****@*****.**', '*****@*****.**', '*****@*****.**', 'あいうえお@example.com', 'email@example.com (Joe Smith)', 'email@example', '*****@*****.**', 'email@111.222.333.44444', '*****@*****.**', '*****@*****.**', '"(),:;<>[\\]@example.com', 'just"not"*****@*****.**', 'this\\ is\\"really\\"not\\\\allowed@example.com', 123456789, true, false]; foreach ($invalidEmails as $email) { $this->assertFalse($emailHelper->isEmailAddressValid($email), $email . ' was considered valid.'); } }