$log->addLine("Checking mail for user : {$oneUserDir}");
 // process all emails in Maildir/new directory
 foreach (scandir($oneUserDirPath . "/Maildir/new/") as $oneEmail) {
     $oneEmailPath = $oneUserDirPath . "/Maildir/new/" . $oneEmail;
     if (is_file($oneEmailPath)) {
         // Reading return address
         $emailFile = file($oneEmailPath);
         unset($address);
         unset($returnpath);
         foreach ($emailFile as $line) {
             $line = trim($line);
             if (!isset($returnpath) && substr($line, 0, 12) == 'Return-Path:') {
                 $returnpath = extract_email($line);
             }
             if (!isset($address) && substr($line, 0, 5) == 'From:' && strstr($line, "@")) {
                 $address = extract_email($line);
             }
             if (!isset($subject) && substr($line, 0, 8) == 'Subject:') {
                 $subject = substr($line, strpos($line, ':') + 1);
             }
         }
         if (!filter_var($address, FILTER_VALIDATE_EMAIL)) {
             //						$log->addLine("Invalid From address: $address");
             if (filter_var($returnpath, FILTER_VALIDATE_EMAIL)) {
                 $address = $returnpath;
             } else {
                 $log->addLine("Invalid Reply-To address: {$returnpath}");
             }
         }
         // skip email if there is no properly formatted return address
         if (!filter_var($address, FILTER_VALIDATE_EMAIL)) {
Example #2
0
/**
 * Save mail callback
 * Accepts an email received from a client during the DATA command.
 * This email is processed, the recipient host is verified, the body is
 * decoded, then saved to the database.
 * This is an example save mail function.
 * Of course, yours can be better! Eg. add buffered writes, place email on message queue, encryption, etc
 *
 * @param string $email
 * @param        $rcpt_to
 * @param        $helo
 * @param        $helo_ip
 *
 * @return array, with the following elements array($hash, $recipient)
 * where the $hash is a unique id for this email.
 */
function my_save_email($email, $rcpt_to, $helo, $helo_ip)
{
    global $GM_ERROR;
    $mimemail = null;
    $hash = '';
    $email .= "\r\n";
    list($to, $from, $subject) = get_email_headers($email, array('To', 'From', 'Subject'));
    $rcpt_to = extract_rcpt_email($rcpt_to);
    $from = extract_email($from);
    list($mail_user, $mail_host) = explode('@', $rcpt_to);
    if (is_host_allowed($mail_host)) {
        $db = get_db_link();
        if ($db === false) {
            // could not get a db connection
            $GM_ERROR = 'could not get a db connection';
            return array(false, false);
        }
        $to = $mail_user . '@' . GM_PRIMARY_MAIL_HOST;
        // change it to the primary host
        if (GSMTP_VERIFY_USERS) {
            // Here we can verify that the recipient is actually in the database.
            // Note that guerrillamail.com does not do this - you can send email
            // to a non-existing email address, and set to this email later.
            // just an example:
            if (array_pop(explode('@', $to)) !== 'sharklasers.com') {
                // check the user against our user database
                $user = array_shift(explode('@', $to));
                $sql = "SELECT * FROM `gm2_address` WHERE `address_email`=" . $db->quote($user . '@guerrillamailblock.com') . " ";
                $stmt = $db->query($sql);
                if ($stmt->rowCount() == 0) {
                    $GM_ERROR = 'could not verify user';
                    return false;
                    // no such address
                }
            }
        }
        $hash = md5($to . $from . $subject . microtime());
        // add 'received' headers
        $add_head = '';
        $add_head .= "Delivered-To: " . $to . "\r\n";
        $add_head .= "Received: from " . $helo . " (" . $helo . "  [" . $helo_ip . "])\r\n";
        $add_head .= "\tby " . GSMTP_HOST_NAME . " with SMTP id " . $hash . "@" . GSMTP_HOST_NAME . ";\r\n";
        $add_head .= "\t" . gmdate('r') . "\r\n";
        $email = $add_head . $email;
        $body = 'gzencode';
        $charset = '';
        $has_attach = '';
        $content_type = '';
        $email = gzcompress($email, 6);
        $redis = getRedis();
        if (is_object($redis)) {
            // send it of to Redis
            try {
                if ($redis->setex($hash, 3600, $email) === true) {
                    $body = 'redis';
                    $email = '';
                    //log_line("saved to redis $hash $to");
                } else {
                    log_line("save failed");
                }
            } catch (\RedisException $e) {
                log_line("redis exeption" . var_dump($e, true));
            }
        } else {
            log_line("no redis for you\n");
        }
        $sql = "INSERT INTO " . GM_MAIL_TABLE . " (\n                `date`,\n                `to`,\n                `from`,\n                `subject`,\n                `body`,\n                `charset`,\n                `mail`,\n                `spam_score`,\n                `hash`,\n                `content_type`,\n                `recipient`,\n                `has_attach`,\n                `ip_addr`\n            )\n            VALUES (\n                '" . gmdate('Y-m-d H:i:s') . "',\n                " . $db->quote($to) . ",\n                " . $db->quote($from) . ",\n                " . $db->quote($subject) . ",\n                " . $db->quote($body) . ",\n                " . $db->quote($charset) . ",\n                " . $db->quote($email) . ",\n                0 " . ",\n                " . $db->quote($hash) . ",\n                " . $db->quote($content_type) . ",\n                " . $db->quote($to) . ",\n                " . $db->quote($has_attach) . ",\n                " . $db->quote($helo_ip) . "\n            ) ";
        try {
            $db->query($sql);
            $id = $db->lastInsertId();
            if ($id) {
                $sql = "UPDATE\n                            gm2_setting\n                       SET\n                            `setting_value` = `setting_value`+1\n                       WHERE\n                            `setting_name`='received_emails'\n                            LIMIT 1";
                $db->query($sql);
            }
        } catch (\PDOException $e) {
            $GM_ERROR = 'save error ' . $e->getMessage();
            log_line('Failed to save email From:' . $from . ' To:' . $to . ' ' . $e->getMessage() . ' ' . $sql, 1);
        }
    } else {
        $GM_ERROR = " -{$mail_host}- not in allowed hosts:" . $mail_host . " ";
    }
    return array($hash, $to);
}