标签 imap 下的文章

baikai caldav carddav 服务端与 roundcube 的插件应用 整合 - imap 验证 和 数据表触发器

目的:让 roundcube 的 日历 和 地址簿 插件,能连上 baikai 的服务端,且正常更新数据。

问题1:用户验证。
回答:使用已有的 IMAP 来验证用户。
参考:https://github.com/sabre-io/Baikal/pull/1292

问题:baikai 里 用户数据的更新,怎么实现?
回答:可以在 vmail.mailbox 里创建 触发器,当数据变更时,同步到 baikai 里的 principals 数据表。
参考:https://sabre.io/dav/principals/
参考的SQL语句:

DELIMITER //

CREATE TRIGGER after_mailbox_insert
AFTER INSERT ON mailbox  -- 直接引用 mailbox 表
FOR EACH ROW
BEGIN
    INSERT INTO baikal.principals (uri, email, displayname)  -- 使用完全限定名
    VALUES (UNHEX(HEX(CONCAT('principals/', NEW.username))), UNHEX(HEX(NEW.username)), NEW.name)
    ON DUPLICATE KEY UPDATE
        displayname = NEW.name;
END //

DELIMITER ;

#########
DELIMITER //

CREATE TRIGGER after_mailbox_update
AFTER UPDATE ON mailbox
FOR EACH ROW
BEGIN
    UPDATE baikal.principals
    SET displayname = NEW.name
    WHERE email = UNHEX(HEX(NEW.username));
END //

DELIMITER ;
#############################
DELIMITER //

CREATE TRIGGER after_mailbox_delete
AFTER DELETE ON mailbox
FOR EACH ROW
BEGIN
    DELETE FROM baikal.principals
    WHERE email = UNHEX(HEX(OLD.username));
END //

DELIMITER ;
########################
#手工执行一次,如果之前有用户数据未同步
INSERT INTO baikal.principals (uri, email, displayname)
SELECT
    UNHEX(HEX(CONCAT('principals/', username))),
    UNHEX(HEX(username)),
    name
FROM
    vmail.mailbox
ON DUPLICATE KEY UPDATE
    displayname = VALUES(displayname);
##########################

通过浏览器访问网址 - 使用imap协议 - 获取邮件内容 - 关键词搜索 - php-imap-reader

需求:通过URL,提交邮箱的用户名和密码,获取到邮件内容。通过关键字搜索邮件。

尝试:用 php-imap-reader 试试。

<?php
date_default_timezone_set('Asia/Shanghai');

require 'src/phpImapReader/Email.php';
require 'src/phpImapReader/EmailAttachment.php';
require 'src/phpImapReader/Reader.php';

use benhall14\phpImapReader\Email;
use benhall14\phpImapReader\EmailAttachment;
use benhall14\phpImapReader\Reader;

// define('IMAP_USERNAME', 'postmaster@lan.anqun.org');
// define('IMAP_PASSWORD', 'mima');
define('IMAP_MAILBOX', '{127.0.0.1:993/imap/ssl/novalidate-cert}'); // For example: {outlook.office365.com:993/imap/ssl/novalidate-cert}
define('ATTACHMENT_PATH', __DIR__ . '/attachments/');

define('IMAP_USERNAME', $_REQUEST['n']);
define('IMAP_PASSWORD', $_REQUEST['p']);

// echo $_REQUEST['n'];
// echo $_REQUEST['p'];
// echo $_SERVER["QUERY_STRING"];

try {
    $imap = new Reader(IMAP_MAILBOX, IMAP_USERNAME, IMAP_PASSWORD, ATTACHMENT_PATH);
    
    $imap->limit(2)->get();

    foreach ($imap->emails() as $email) {
        echo '<div>';
            
        echo '<div>' . $email->fromEmail() . '</div>';
            
        echo '<div>' . $email->subject() . '</div>';
            
        echo '<div>' . $email->date('Y-m-d H:i:s') . '</div>';

        if ($email->hasAttachments()) {
            foreach ($email->attachments() as $attachment) {
                echo '<div>' . $attachment->filePath() . '</div>';
            }
        }
        
        #print_r($email->plain());
        #print_r($email->html());

        #$plainText = $email->plain();
        #$extractedText = substr($plainText, 0, 100); // 提取前100个字符
        #echo $extractedText;

        // 假设 $email 是一个包含邮件信息的对象
        $plainText = $email->plain(); // 获取 plain 属性内容

       // 使用正则表达式来匹配并提取所需文本内容
       // $pattern = '/Password: \w{8}/'; // 用你的实际匹配模式替换 YourPatternHere
        $pattern = '/\b\d{6}\b/'; // 用你的实际匹配模式替换 YourPatternHere
       if (preg_match($pattern, $plainText, $matches)) {
        $extractedText = $matches[0]; // 提取匹配到的文本内容
        echo "Extracted text: " . $extractedText;
    } else {
        echo "Pattern not found in plain text.";
    }

        echo '</div><br/><br/><hr />';
    }
} catch (Exception $e) {
    die($e->getMessage());
}

或者:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>My Web Page</title>
</head>
<body>

<?php
date_default_timezone_set('Asia/Shanghai');

require 'src/phpImapReader/Email.php';
require 'src/phpImapReader/EmailAttachment.php';
require 'src/phpImapReader/Reader.php';

use benhall14\phpImapReader\Email;
use benhall14\phpImapReader\EmailAttachment;
use benhall14\phpImapReader\Reader;

// define('IMAP_USERNAME', 'postmaster@lan.anqun.org');
// define('IMAP_PASSWORD', 'mima888');
define('IMAP_MAILBOX', '{localhost/imap/ssl/novalidate-cert}'); // For example: {outlook.office365.com:993/imap/ssl/novalidate-cert}
define('ATTACHMENT_PATH', __DIR__ . '/attachments/');

define('IMAP_USERNAME', $_REQUEST['n']);
define('IMAP_PASSWORD', $_REQUEST['p']);

// echo $_REQUEST['n'];
//  echo $_REQUEST['p'];
// echo $_SERVER["QUERY_STRING"];

echo "默认显示未读邮件列表及其内容。如果列表为空,可能是没有收到新邮件。";

try {
    $mark_as_read = true;
    $imap = new Reader(IMAP_MAILBOX, IMAP_USERNAME, IMAP_PASSWORD, ATTACHMENT_PATH);
    
   $imap->limit(2)->get();
//    $imap->unseen()->get();

    foreach ($imap->emails() as $email) {
        echo '<div>';
            
        echo '<div>' . $email->fromEmail() . '</div>';

//    echo '<div>' . $email->to() . '</div>';
        $emailsString = implode(', ', array_column($email->to(), 'email'));
        echo $emailsString;
    

            
        echo '<div>' . $email->subject() . '</div>';
            
        echo '<div>' . $email->date('Y-m-d H:i:s') . '</div>';

        if ($email->hasAttachments()) {
            foreach ($email->attachments() as $attachment) {
                echo '<div>' . $attachment->filePath() . '</div>';
            }
        }
        
        print_r($email->plain());
        #print_r($email->html());

        #$plainText = $email->plain();
        #$extractedText = substr($plainText, 0, 100); // 提取前100个字符
        #echo $extractedText;


        echo '</div><br/><br/><hr />';
    }
} catch (Exception $e) {
    die($e->getMessage());
}

?>

</body>
</html>
<?php
header('Content-Type: application/json; charset=UTF-8');
date_default_timezone_set('Asia/Shanghai');

require 'src/phpImapReader/Email.php';
require 'src/phpImapReader/EmailAttachment.php';
require 'src/phpImapReader/Reader.php';

use benhall14\phpImapReader\Email;
use benhall14\phpImapReader\EmailAttachment;
use benhall14\phpImapReader\Reader;

define('IMAP_MAILBOX', '{mail.iredmail.demo.anqun.org/imap/ssl/novalidate-cert}'); // For example: {outlook.office365.com:993/imap/ssl/novalidate-cert}
define('ATTACHMENT_PATH', DIR . '/attachments/');

define('IMAP_USERNAME', $_REQUEST['n']);
define('IMAP_PASSWORD', $_REQUEST['p']);

// echo $_REQUEST['n'];
// echo $_REQUEST['p'];
// echo $_SERVER["QUERY_STRING"];

try {
$mark_as_read = true;
$imap = new Reader(IMAP_MAILBOX, IMAP_USERNAME, IMAP_PASSWORD, ATTACHMENT_PATH);

$imap->limit(5)->get();
// $imap->unseen()->get();

// 创建一个空数组来存储所有邮件的信息
$emailsData = [];

foreach ($imap->emails() as $email) {
// 为当前邮件创建一个关联数组
$emailData = [
'from' => $email->fromEmail(),
'to' => $email->to(),
'subject' => $email->subject(),
'date' => $email->date('Y-m-d H:i:s'),
// 'plainText' => $email->plain(),
'verificationCode' => null // 初始化验证码为 null
];

// 使用正则表达式搜索6位的数字或字母组成的序列
$plainText = $email->plain();
preg_match('/[A-Za-z0-9]{6}/', $plainText, $matches);

// 将当前邮件的信息添加到 $emailsData 数组中
$emailsData[] = $emailData;
}

// 使用 json_encode() 将整个数组转换为 JSON 格式的字符串
$jsonData = json_encode($emailsData, JSON_PRETTY_PRINT);

// 输出 JSON 数据
echo $jsonData;

} catch (Exception $e) {
die($e->getMessage());
}

?>

参考:
https://github.com/benhall14/php-imap-reader