diff options
author | Urban Müller | 2022-03-11 14:57:35 +0100 |
---|---|---|
committer | Urban Müller | 2022-03-11 14:57:35 +0100 |
commit | 1bded8a016bb64221fff4003e28947f50e9e00d9 (patch) | |
tree | 405ae2a1c7140e228513fd71b67fe5d816fa5962 /it_mail.class | |
parent | d97a23f7af099998470b00f5f76363cb2b1bcab9 (diff) | |
download | itools-1bded8a016bb64221fff4003e28947f50e9e00d9.tar.gz itools-1bded8a016bb64221fff4003e28947f50e9e00d9.tar.bz2 itools-1bded8a016bb64221fff4003e28947f50e9e00d9.zip |
format check for email addresses on sending
Diffstat (limited to 'it_mail.class')
-rw-r--r-- | it_mail.class | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/it_mail.class b/it_mail.class index a355b28..6394e5f 100644 --- a/it_mail.class +++ b/it_mail.class @@ -56,6 +56,8 @@ class it_mail var $flags = ""; var $charset; + static $addr_regex = "(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])"; + /** * Construct a new email message. Headers and body can be added later. * Note: Headers To, Cc, Bcc can be arrays @@ -179,6 +181,7 @@ function add_file($filename, $p = array(), $legacy_name = null) /** * Send this email message * @param $p['forcemail'] Send mail even if on twin or devel machine + * @param $p['it_error'] Error handling params for bad email addresses * @return True if mail was accepted for delivery */ function send($p = array()) @@ -195,12 +198,13 @@ function send($p = array()) for ($i = 0; $i < count($this->header_names); $i++) $headers[] = $this->header_names[$i] . ': ' . $this->header_values[$i]; + foreach (array_merge((array)$this->to, (array)$this->cc, (array)$this->bcc) as $addr) + if (($error = self::address_error($addr))) + it::error((array)$p['it_error'] + ['title' => "address error '$error' in $addr", 'body' => $this]); /* Automatically add doctype if none given */ if ($this->body[IT_MAIL_HTML] && !preg_match('/^<!doctype/i', $this->body[IT_MAIL_HTML])) - { $this->body[IT_MAIL_HTML] = '<!doctype html public "-//w3c//dtd html 4.01 transitional//en">' . "\n" . $this->body[IT_MAIL_HTML]; - } $headers[] = "MIME-Version: 1.0"; @@ -290,7 +294,7 @@ function send($p = array()) if (it::is_live() || EDC('forcemail') || $p['forcemail']) { if (($result = mail($to, $this->header_escape($this->subject), $text, implode("\n", $headers), $this->flags)) === false) - it::error(array('title' => "failed sending mail to $to subject $this->subject", 'body' => D($text, $headers, $this->flags))); + it::error(['title' => "failed sending mail to $to subject $this->subject", 'body' => ['text' => $text, 'headers' => $headers, 'flags' => $this->flags]]); return $result; } else @@ -402,6 +406,13 @@ static function send_smtp_cmd($fp, $cmd, &$answer, $timeoutok = false, $failcode return $result; } +/* Return errors found with email address, null otherwise + */ +static function address_error($email) +{ + return it::match(self::$addr_regex, $email) ? null : "invalid format"; +} + /** * Check if given email address is valid (syntax, MX record). If you set |