标签 php 下的文章

通过浏览器访问网址 - 使用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

cpanel - 用不同的版本的php命令 - /opt/cpanel/ea-php72/root/usr/bin/php - An exception was raised while creating "Request"

现象:在cPanel环境中执行magento2的命令,如 php bin/magento cache:flush提示出错:

An exception was raised while creating "Request"; no instance returned

尝试:用正确的php版本路径再次执行,如 /opt/cpanel/ea-php72/root/usr/bin/php bin/magento cache:flush

参考:

在新版本WHM (64.0)中安装php5.3

问题:Godaddy的虚拟服务器(VPS)产品中,绑定的系统是CentOS 6 + cPanel,支持php5.5以上的版本,但网站程序必须是php5.3

过程:

  1. 在 whm 里查看多版本的php中,并没有php5.3
    ea-php5.3-1.png
  2. yum groupinstall 'Development Tools' # 安装开发组件
  3. yum install epel-release # 增加软件安装源
  4. yum install sqlite-devel libxml2-devel bzip2-devel libcurl-devel libc-client-devel libmcrypt-devel aspell-devel libedit-devel libtidy-devel pcre-devel # 安装相关的开发包
  5. wget http://cn2.php.net/distributions/php-5.3.29.tar.gz # 下载php5.3源码包
  6. 解压到 /usr/src/php-5.3.29
  7. ./configure --enable-bcmath --enable-calendar --enable-exif --enable-ftp --enable-gd-native-ttf --enable-libxml --enable-mbstring --enable-pdo=shared --enable-sockets --enable-zip --prefix=/opt/php53 --with-bz2 --with-curl=/usr --with-freetype-dir=/usr --with-gd --with-imap=/usr --with-imap-ssl --with-jpeg-dir=/usr --with-kerberos --with-libdir=lib64 --with-libexpat-dir=/usr --with-libxml-dir=/usr --with-mcrypt=/usr --with-mysql=/usr --with-mysql-sock=/var/lib/mysql/mysql.sock --with-mysqli=/usr/bin/mysql_config --with-openssl=/usr --with-openssl-dir=/usr --with-pcre-regex=/usr --with-pdo-mysql=shared --with-pdo-sqlite=shared --with-pic --with-png-dir=/usr --with-sqlite=shared --with-tidy=/usr --with-xmlrpc --with-xpm-dir=/usr --with-zlib --with-zlib-dir=/usr # 配置
    ea-php5.3-4.png
  8. make && make install # 编译并安装(在/opt/php53)
    ea-php5.3-5.png
  9. vi /etc/apache2/conf.d/includes/pre_main_global.conf # 向apache的配置文件添加以下内容,令其默认使用php5.3

    AddHandler application/x-httpd-php53 php
    
    ScriptAlias /local-bin /opt/php53/bin
    Action application/x-httpd-php53 /local-bin/php-cgi
    
    <Directory "/opt/php53/bin">
     Order allow,deny
     Allow from all
    </Directory>

    ea-php5.3-2.png

  10. /etc/init.d/httpd reload # 重新加载apache配置参数
  11. 查看phpinfo输出信息,是php5.3版本了
    ea-php5.3-3.png

参考: