IMP_Compose::_prepSendMessageEncode PHP Method

_prepSendMessageEncode() protected method

Encode address and do sanity checking on encoded address.
protected _prepSendMessageEncode ( Horde_Mail_Rfc822_List $email, string $charset ) : string
$email Horde_Mail_Rfc822_List The e-mail list to send to.
$charset string The charset to encode to.
return string The encoded $email list.
    protected function _prepSendMessageEncode(Horde_Mail_Rfc822_List $email, $charset)
    {
        global $injector;
        $exception = new IMP_Compose_Exception_Address();
        $hook = true;
        $out = array();
        foreach ($email as $val) {
            /* $email contains address objects that already have the default
             * maildomain appended. Need to encode personal part and encode
             * IDN domain names. */
            $tmp = $val->writeAddress(array('encode' => $charset, 'idn' => true));
            try {
                /* We have written address, but it still may not be valid.
                 * So double-check. Key here is MTA server support for
                 * UTF-8. */
                $utf8 = $injector->getInstance('IMP_Mail')->eai;
                $alist = IMP::parseAddressList($tmp, array('validate' => $utf8 ? 'eai' : true));
                $error = null;
                if ($hook) {
                    try {
                        $error = $injector->getInstance('Horde_Core_Hooks')->callHook('compose_addr', 'imp', array($alist[0]));
                    } catch (Horde_Exception_HookNotSet $e) {
                        $hook = false;
                    }
                }
            } catch (Horde_Mail_Exception $e) {
                $error = array('msg' => sprintf(_("Invalid e-mail address (%s)."), $val));
            }
            if (is_array($error)) {
                switch (isset($error['level']) ? $error['level'] : $exception::BAD) {
                    case $exception::WARN:
                    case 'warn':
                        if (($warn = $this->getMetadata('warn_addr')) && in_array(strval($val), $warn)) {
                            $out[] = $tmp;
                            continue 2;
                        }
                        $warn[] = strval($val);
                        $this->_setMetadata('warn_addr', $warn);
                        $this->changed = 'changed';
                        $level = $exception::WARN;
                        break;
                    default:
                        $level = $exception::BAD;
                        break;
                }
                $exception->addAddress($val, $error['msg'], $level);
            } else {
                $out[] = $tmp;
            }
        }
        if (count($exception)) {
            throw $exception;
        }
        return implode(', ', $out);
    }