用PHP接收电子邮件
以下是从imap服务器接收邮件的PHP方法。
#! /usr/bin/php
<?php
// --------------------------------------------------------------------
/*
imap_get.php
Nov/26/2017
*/
// --------------------------------------------------------------------
fputs (STDERR,"*** 開始 ***\n");
$server = 'imap.mail.yahoo.co.jp';
$port = 993;
$user = 'xxxxxxx';
$password = 'yyyyyy';
$mailBox = imap_open('{' . $server . ':' . $port . '/novalidate-cert/imap/ssl}' . "INBOX", $user, $password);
if (!$mailBox) {
echo "接続失敗\n";
}
$messageIds = imap_search($mailBox, 'ALL');
print_r($messageIds);
foreach($messageIds as $messageId) {
$header = imap_headerinfo($mailBox, $messageId);
$subject = mb_decode_mimeheader($header->subject);
echo "Subject: " . $subject . "\n";
$body = imap_fetchbody($mailBox, $messageId, 1, FT_INTERNAL);
$body = trim($body);
echo "Body: " . $body . "\n";
}
fputs (STDERR,"*** 終了 ***\n");
// --------------------------------------------------------------------
?>
在Arch Linux上安装库的方法
sudo pacman -S php-imap
設定文件的注釋
(省略)
extension=imap
(省略)
在Ubuntu上安装库的方法
sudo apt install php-imap
我在以下环境中确认了操作。
$ uname -a
Linux iwata 5.4.0-42-generic #46-Ubuntu SMP Fri Jul 10 00:24:02 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
$ php --version
PHP 7.4.3 (cli) (built: May 26 2020 12:24:22) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies