function mailstream_send($a, $message_id, $item, $user)
{
    if (!$item['visible']) {
        return;
    }
    if (!$message_id) {
        return;
    }
    require_once dirname(__FILE__) . '/class.phpmailer.php';
    require_once 'include/bbcode.php';
    $attachments = array();
    mailstream_do_images($a, $item, $attachments);
    $frommail = get_config('mailstream', 'frommail');
    if ($frommail == "") {
        $frommail = '*****@*****.**';
    }
    $address = get_pconfig($item['uid'], 'mailstream', 'address');
    if (!$address) {
        $address = $user['email'];
    }
    $mail = new PHPmailer();
    try {
        $mail->XMailer = 'Friendica Mailstream Plugin';
        $mail->SetFrom($frommail, mailstream_sender($item));
        $mail->AddAddress($address, $user['username']);
        $mail->MessageID = $message_id;
        $mail->Subject = mailstream_subject($item);
        if ($item['thr-parent'] != $item['uri']) {
            $mail->addCustomHeader('In-Reply-To: ' . mailstream_generate_id($a, $item['thr-parent']));
        }
        $mail->addCustomHeader('X-Friendica-Mailstream-URI: ' . $item['uri']);
        $mail->addCustomHeader('X-Friendica-Mailstream-Plink: ' . $item['plink']);
        $encoding = 'base64';
        foreach ($attachments as $url => $image) {
            $mail->AddStringEmbeddedImage($image['data'], $image['guid'], $image['filename'], $encoding, $image['type']);
        }
        $mail->IsHTML(true);
        $mail->CharSet = 'utf-8';
        $template = get_markup_template('mail.tpl', 'addon/mailstream/');
        $item['body'] = bbcode($item['body']);
        $item['url'] = $a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $item['id'];
        $mail->Body = replace_macros($template, array('$upstream' => t('Upstream'), '$local' => t('Local'), '$item' => $item));
        mailstream_html_wrap($mail->Body);
        if (!$mail->Send()) {
            throw new Exception($mail->ErrorInfo);
        }
        logger('mailstream_send sent message ' . $mail->MessageID . ' ' . $mail->Subject, LOGGER_DEBUG);
    } catch (phpmailerException $e) {
        logger('mailstream_send PHPMailer exception sending message ' . $message_id . ': ' . $e->errorMessage(), LOGGER_NORMAL);
    } catch (Exception $e) {
        logger('mailstream_send exception sending message ' . $message_id . ': ' . $e->getMessage(), LOGGER_NORMAL);
    }
    // In case of failure, still set the item to completed.  Otherwise
    // we'll just try to send it over and over again and it'll fail
    // every time.
    q('UPDATE `mailstream_item` SET `completed` = now() WHERE `message-id` = "%s"', dbesc($message_id));
}