Hướng dẫn barbushin php console - bảng điều khiển php barbushin

PHP IMAP

Ban đầu được phát hành vào tháng 12 năm 2012, hộp thư IMAP PHP là một thư viện nguồn mở và mạnh mẽ để kết nối với hộp thư của POP3, IMAP và NNTP bằng cách sử dụng tiện ích mở rộng IMAP PHP. Thư viện này cho phép bạn tìm nạp email từ máy chủ email của bạn. Mở rộng chức năng hoặc tạo các ứng dụng web mạnh mẽ để xử lý các email đến của bạn.

Đặc trưng

  • Kết nối với Hộp thư bằng POP3/IMAP/NNTP, sử dụng tiện ích mở rộng IMAP PHP
  • Nhận email với các tệp đính kèm và hình ảnh nội tuyến
  • Nhận email được lọc hoặc sắp xếp theo tiêu chí tùy chỉnh
  • Đánh dấu email như đã thấy/không nhìn thấy
  • Xóa email
  • Quản lý thư mục hộp thư

Yêu cầu

Phiên bản PHPPhiên bản PHP-IMAPTrạng thái PHP-IMAP
5.6 3.xCuối đời
7.0 3.xCuối đời
7.1 3.xCuối đời
7.2 3.x, 4.xCuối đời
7.3 3.x, 4.xCuối đời
7.4 3.x, 4.x> 3.0.33, 4.x, 5.x
8.0 3.x, 4.x> 3.0.33, 4.x, 5.x
8.1 Hỗ trợ tích cực> 3.0.33, 4.x, 5.x
  • Hỗ trợ tích cực
  • > 4.3.0, 5.x
  • Php fileinfo mở rộng phải có mặt; Vì vậy, hãy chắc chắn rằng dòng này đang hoạt động trong php.ini của bạn: extension=php_fileinfo.dll
  • Php iconv mở rộng phải có mặt; Vì vậy, hãy chắc chắn rằng dòng này đang hoạt động trong php.ini của bạn:
    $ composer require php-imap/php-imap:dev-master
    
    0
  • Php
    $ composer require php-imap/php-imap:dev-master
    
    1 mở rộng phải có mặt; Vì vậy, hãy chắc chắn rằng dòng này đang hoạt động trong php.ini của bạn:
    $ composer require php-imap/php-imap:dev-master
    
    2

Php
$ composer require php-imap/php-imap:dev-master
3 mở rộng phải có mặt; Vì vậy, hãy chắc chắn rằng dòng này đang hoạt động trong php.ini của bạn:
$ composer require php-imap/php-imap:dev-master
4

Php

$ composer require php-imap/php-imap:dev-master
5 mở rộng phải có mặt; Vì vậy, hãy chắc chắn rằng dòng này đang hoạt động trong php.ini của bạn:
$ composer require php-imap/php-imap:dev-master
6

$ composer require php-imap/php-imap

Cài đặt bởi nhà soạn nhạc

$ composer require php-imap/php-imap:dev-master

Cài đặt bản phát hành mới nhất có sẵn:

Cài đặt mã nguồn ổn định và có sẵn mới nhất từ ​​

$ composer require php-imap/php-imap:dev-master
7, có thể chưa được phát hành / gắn thẻ:

Chạy các bài kiểm tra

Trước khi bạn có thể chạy bất kỳ thử nghiệm nào, bạn có thể cần chạy

$ composer require php-imap/php-imap:dev-master
8 để cài đặt tất cả các phụ thuộc [phát triển].

Chạy tất cả các bài kiểm tra

Bạn có thể chạy tất cả các thử nghiệm có sẵn bằng cách chạy lệnh sau [bên trong thư mục

$ composer require php-imap/php-imap:dev-master
9 đã cài đặt]:
// Create PhpImap\Mailbox instance for all further actions
$mailbox = new PhpImap\Mailbox[
	'{imap.gmail.com:993/imap/ssl}INBOX', // IMAP server and mailbox folder
	'', // Username for the before configured mailbox
	'*********', // Password for the before configured username
	__DIR__, // Directory, where attachments will be saved [optional]
	'UTF-8', // Server encoding [optional]
    true, // Trim leading/ending whitespaces of IMAP path [optional]
    false // Attachment filename mode [optional; false = random filename; true = original filename]
];

// set some connection arguments [if appropriate]
$mailbox->setConnectionArgs[
    CL_EXPUNGE // expunge deleted mails upon mailbox close
    | OP_SECURE // don't do non-secure authentication
];

try {
	// Get all emails [messages]
	// PHP.net imap_search criteria: //php.net/manual/en/function.imap-search.php
	$mailsIds = $mailbox->searchMailbox['ALL'];
} catch[PhpImap\Exceptions\ConnectionException $ex] {
	echo "IMAP connection failed: " . implode[",", $ex->getErrors['all']];
	die[];
}

// If $mailsIds is empty, no emails could be found
if[!$mailsIds] {
	die['Mailbox is empty'];
}

// Get the first message
// If '__DIR__' was defined in the first line, it will automatically
// save all attachments to the specified directory
$mail = $mailbox->getMail[$mailsIds[0]];

// Show, if $mail has one or more attachments
echo "\nMail has attachments? ";
if[$mail->hasAttachments[]] {
	echo "Yes\n";
} else {
	echo "No\n";
}

// Print all information of $mail
print_r[$mail];

// Print all attachements of $mail
echo "\n\nAttachments:\n";
print_r[$mail->getAttachments[]];
0

Chỉ chạy các bài kiểm tra phpunit

  • Bạn có thể chạy tất cả các thử nghiệm PHPUNIT bằng cách chạy lệnh sau [bên trong thư mục
    $ composer require php-imap/php-imap:dev-master
    
    9 đã cài đặt]:
    // Create PhpImap\Mailbox instance for all further actions
    $mailbox = new PhpImap\Mailbox[
    	'{imap.gmail.com:993/imap/ssl}INBOX', // IMAP server and mailbox folder
    	'', // Username for the before configured mailbox
    	'*********', // Password for the before configured username
    	__DIR__, // Directory, where attachments will be saved [optional]
    	'UTF-8', // Server encoding [optional]
        true, // Trim leading/ending whitespaces of IMAP path [optional]
        false // Attachment filename mode [optional; false = random filename; true = original filename]
    ];
    
    // set some connection arguments [if appropriate]
    $mailbox->setConnectionArgs[
        CL_EXPUNGE // expunge deleted mails upon mailbox close
        | OP_SECURE // don't do non-secure authentication
    ];
    
    try {
    	// Get all emails [messages]
    	// PHP.net imap_search criteria: //php.net/manual/en/function.imap-search.php
    	$mailsIds = $mailbox->searchMailbox['ALL'];
    } catch[PhpImap\Exceptions\ConnectionException $ex] {
    	echo "IMAP connection failed: " . implode[",", $ex->getErrors['all']];
    	die[];
    }
    
    // If $mailsIds is empty, no emails could be found
    if[!$mailsIds] {
    	die['Mailbox is empty'];
    }
    
    // Get the first message
    // If '__DIR__' was defined in the first line, it will automatically
    // save all attachments to the specified directory
    $mail = $mailbox->getMail[$mailsIds[0]];
    
    // Show, if $mail has one or more attachments
    echo "\nMail has attachments? ";
    if[$mail->hasAttachments[]] {
    	echo "Yes\n";
    } else {
    	echo "No\n";
    }
    
    // Print all information of $mail
    print_r[$mail];
    
    // Print all attachements of $mail
    echo "\n\nAttachments:\n";
    print_r[$mail->getAttachments[]];
    2

Tích hợp với các khung

Symfony-//github.com/secit-pl/imap-bundle

Bắt đầu ví dụ

// Create PhpImap\Mailbox instance for all further actions
$mailbox = new PhpImap\Mailbox[
	'{imap.gmail.com:993/imap/ssl}INBOX', // IMAP server and mailbox folder
	'', // Username for the before configured mailbox
	'*********', // Password for the before configured username
	__DIR__, // Directory, where attachments will be saved [optional]
	'UTF-8', // Server encoding [optional]
    true, // Trim leading/ending whitespaces of IMAP path [optional]
    false // Attachment filename mode [optional; false = random filename; true = original filename]
];

// set some connection arguments [if appropriate]
$mailbox->setConnectionArgs[
    CL_EXPUNGE // expunge deleted mails upon mailbox close
    | OP_SECURE // don't do non-secure authentication
];

try {
	// Get all emails [messages]
	// PHP.net imap_search criteria: //php.net/manual/en/function.imap-search.php
	$mailsIds = $mailbox->searchMailbox['ALL'];
} catch[PhpImap\Exceptions\ConnectionException $ex] {
	echo "IMAP connection failed: " . implode[",", $ex->getErrors['all']];
	die[];
}

// If $mailsIds is empty, no emails could be found
if[!$mailsIds] {
	die['Mailbox is empty'];
}

// Get the first message
// If '__DIR__' was defined in the first line, it will automatically
// save all attachments to the specified directory
$mail = $mailbox->getMail[$mailsIds[0]];

// Show, if $mail has one or more attachments
echo "\nMail has attachments? ";
if[$mail->hasAttachments[]] {
	echo "Yes\n";
} else {
	echo "No\n";
}

// Print all information of $mail
print_r[$mail];

// Print all attachements of $mail
echo "\n\nAttachments:\n";
print_r[$mail->getAttachments[]];

Dưới đây, bạn sẽ tìm thấy một mã ví dụ về cách bạn có thể sử dụng thư viện này. Để biết thêm thông tin và các ví dụ khác, bạn có thể xem Wiki.

// Call imap_check[] - see //php.net/manual/function.imap-check.php
$info = $mailbox->imap['check'];


// Show current time for the mailbox
$currentServerTime = isset[$info->Date] && $info->Date ? date['Y-m-d H:i:s', strtotime[$info->Date]] : 'Unknown';

echo $currentServerTime;

Theo mặc định, thư viện này sử dụng tên tệp ngẫu nhiên cho các tệp đính kèm làm tên tệp giống hệt nhau từ các email khác sẽ ghi đè lên các tệp đính kèm khác. Nếu bạn muốn giữ tên tệp gốc, bạn có thể đặt chế độ tệp đính kèm thành

// Create PhpImap\Mailbox instance for all further actions
$mailbox = new PhpImap\Mailbox[
	'{imap.gmail.com:993/imap/ssl}INBOX', // IMAP server and mailbox folder
	'', // Username for the before configured mailbox
	'*********', // Password for the before configured username
	__DIR__, // Directory, where attachments will be saved [optional]
	'UTF-8', // Server encoding [optional]
    true, // Trim leading/ending whitespaces of IMAP path [optional]
    false // Attachment filename mode [optional; false = random filename; true = original filename]
];

// set some connection arguments [if appropriate]
$mailbox->setConnectionArgs[
    CL_EXPUNGE // expunge deleted mails upon mailbox close
    | OP_SECURE // don't do non-secure authentication
];

try {
	// Get all emails [messages]
	// PHP.net imap_search criteria: //php.net/manual/en/function.imap-search.php
	$mailsIds = $mailbox->searchMailbox['ALL'];
} catch[PhpImap\Exceptions\ConnectionException $ex] {
	echo "IMAP connection failed: " . implode[",", $ex->getErrors['all']];
	die[];
}

// If $mailsIds is empty, no emails could be found
if[!$mailsIds] {
	die['Mailbox is empty'];
}

// Get the first message
// If '__DIR__' was defined in the first line, it will automatically
// save all attachments to the specified directory
$mail = $mailbox->getMail[$mailsIds[0]];

// Show, if $mail has one or more attachments
echo "\nMail has attachments? ";
if[$mail->hasAttachments[]] {
	echo "Yes\n";
} else {
	echo "No\n";
}

// Print all information of $mail
print_r[$mail];

// Print all attachements of $mail
echo "\n\nAttachments:\n";
print_r[$mail->getAttachments[]];
3, nhưng sau đó bạn cũng cần đảm bảo rằng các tệp đó không bị ghi đè bởi các email khác chẳng hạn.

// If you don't need to grab attachments you can significantly increase performance of your application
$mailbox->setAttachmentsIgnore[true];

// get the list of folders/mailboxes
$folders = $mailbox->getMailboxes['*'];

// loop through mailboxs
foreach[$folders as $folder] {

	// switch to particular mailbox
	$mailbox->switchMailbox[$folder['fullpath']];

	// search in particular mailbox
	$mails_ids[$folder['fullpath']] = $mailbox->searchMailbox['SINCE "1 Jan 2018" BEFORE "28 Jan 2018"'];
}

print_r[$mails_ids];

Phương thức
// Create PhpImap\Mailbox instance for all further actions
$mailbox = new PhpImap\Mailbox[
	'{imap.gmail.com:993/imap/ssl}INBOX', // IMAP server and mailbox folder
	'', // Username for the before configured mailbox
	'*********', // Password for the before configured username
	__DIR__, // Directory, where attachments will be saved [optional]
	'UTF-8', // Server encoding [optional]
    true, // Trim leading/ending whitespaces of IMAP path [optional]
    false // Attachment filename mode [optional; false = random filename; true = original filename]
];

// set some connection arguments [if appropriate]
$mailbox->setConnectionArgs[
    CL_EXPUNGE // expunge deleted mails upon mailbox close
    | OP_SECURE // don't do non-secure authentication
];

try {
	// Get all emails [messages]
	// PHP.net imap_search criteria: //php.net/manual/en/function.imap-search.php
	$mailsIds = $mailbox->searchMailbox['ALL'];
} catch[PhpImap\Exceptions\ConnectionException $ex] {
	echo "IMAP connection failed: " . implode[",", $ex->getErrors['all']];
	die[];
}

// If $mailsIds is empty, no emails could be found
if[!$mailsIds] {
	die['Mailbox is empty'];
}

// Get the first message
// If '__DIR__' was defined in the first line, it will automatically
// save all attachments to the specified directory
$mail = $mailbox->getMail[$mailsIds[0]];

// Show, if $mail has one or more attachments
echo "\nMail has attachments? ";
if[$mail->hasAttachments[]] {
	echo "Yes\n";
} else {
	echo "No\n";
}

// Print all information of $mail
print_r[$mail];

// Print all attachements of $mail
echo "\n\nAttachments:\n";
print_r[$mail->getAttachments[]];
4 cho phép gọi bất kỳ chức năng IMAP PHP nào trong bối cảnh của trường hợp. Thí dụ:

Một số yêu cầu yêu cầu nhiều thời gian và tài nguyên:

Before:

    public function checkMailbox[]
    {
        return $this->imap['check'];
    }

After:

    public function checkMailbox[]: object
    {
        return Imap::check[$this->getImapStream[]];
    }

Nâng cấp từ 3.x

  • Trước 3.1,
    // Create PhpImap\Mailbox instance for all further actions
    $mailbox = new PhpImap\Mailbox[
    	'{imap.gmail.com:993/imap/ssl}INBOX', // IMAP server and mailbox folder
    	'', // Username for the before configured mailbox
    	'*********', // Password for the before configured username
    	__DIR__, // Directory, where attachments will be saved [optional]
    	'UTF-8', // Server encoding [optional]
        true, // Trim leading/ending whitespaces of IMAP path [optional]
        false // Attachment filename mode [optional; false = random filename; true = original filename]
    ];
    
    // set some connection arguments [if appropriate]
    $mailbox->setConnectionArgs[
        CL_EXPUNGE // expunge deleted mails upon mailbox close
        | OP_SECURE // don't do non-secure authentication
    ];
    
    try {
    	// Get all emails [messages]
    	// PHP.net imap_search criteria: //php.net/manual/en/function.imap-search.php
    	$mailsIds = $mailbox->searchMailbox['ALL'];
    } catch[PhpImap\Exceptions\ConnectionException $ex] {
    	echo "IMAP connection failed: " . implode[",", $ex->getErrors['all']];
    	die[];
    }
    
    // If $mailsIds is empty, no emails could be found
    if[!$mailsIds] {
    	die['Mailbox is empty'];
    }
    
    // Get the first message
    // If '__DIR__' was defined in the first line, it will automatically
    // save all attachments to the specified directory
    $mail = $mailbox->getMail[$mailsIds[0]];
    
    // Show, if $mail has one or more attachments
    echo "\nMail has attachments? ";
    if[$mail->hasAttachments[]] {
    	echo "Yes\n";
    } else {
    	echo "No\n";
    }
    
    // Print all information of $mail
    print_r[$mail];
    
    // Print all attachements of $mail
    echo "\n\nAttachments:\n";
    print_r[$mail->getAttachments[]];
    5 đã sử dụng phương pháp "ma thuật" [
    // Create PhpImap\Mailbox instance for all further actions
    $mailbox = new PhpImap\Mailbox[
    	'{imap.gmail.com:993/imap/ssl}INBOX', // IMAP server and mailbox folder
    	'', // Username for the before configured mailbox
    	'*********', // Password for the before configured username
    	__DIR__, // Directory, where attachments will be saved [optional]
    	'UTF-8', // Server encoding [optional]
        true, // Trim leading/ending whitespaces of IMAP path [optional]
        false // Attachment filename mode [optional; false = random filename; true = original filename]
    ];
    
    // set some connection arguments [if appropriate]
    $mailbox->setConnectionArgs[
        CL_EXPUNGE // expunge deleted mails upon mailbox close
        | OP_SECURE // don't do non-secure authentication
    ];
    
    try {
    	// Get all emails [messages]
    	// PHP.net imap_search criteria: //php.net/manual/en/function.imap-search.php
    	$mailsIds = $mailbox->searchMailbox['ALL'];
    } catch[PhpImap\Exceptions\ConnectionException $ex] {
    	echo "IMAP connection failed: " . implode[",", $ex->getErrors['all']];
    	die[];
    }
    
    // If $mailsIds is empty, no emails could be found
    if[!$mailsIds] {
    	die['Mailbox is empty'];
    }
    
    // Get the first message
    // If '__DIR__' was defined in the first line, it will automatically
    // save all attachments to the specified directory
    $mail = $mailbox->getMail[$mailsIds[0]];
    
    // Show, if $mail has one or more attachments
    echo "\nMail has attachments? ";
    if[$mail->hasAttachments[]] {
    	echo "Yes\n";
    } else {
    	echo "No\n";
    }
    
    // Print all information of $mail
    print_r[$mail];
    
    // Print all attachements of $mail
    echo "\n\nAttachments:\n";
    print_r[$mail->getAttachments[]];
    6], với lớp
    // Create PhpImap\Mailbox instance for all further actions
    $mailbox = new PhpImap\Mailbox[
    	'{imap.gmail.com:993/imap/ssl}INBOX', // IMAP server and mailbox folder
    	'', // Username for the before configured mailbox
    	'*********', // Password for the before configured username
    	__DIR__, // Directory, where attachments will be saved [optional]
    	'UTF-8', // Server encoding [optional]
        true, // Trim leading/ending whitespaces of IMAP path [optional]
        false // Attachment filename mode [optional; false = random filename; true = original filename]
    ];
    
    // set some connection arguments [if appropriate]
    $mailbox->setConnectionArgs[
        CL_EXPUNGE // expunge deleted mails upon mailbox close
        | OP_SECURE // don't do non-secure authentication
    ];
    
    try {
    	// Get all emails [messages]
    	// PHP.net imap_search criteria: //php.net/manual/en/function.imap-search.php
    	$mailsIds = $mailbox->searchMailbox['ALL'];
    } catch[PhpImap\Exceptions\ConnectionException $ex] {
    	echo "IMAP connection failed: " . implode[",", $ex->getErrors['all']];
    	die[];
    }
    
    // If $mailsIds is empty, no emails could be found
    if[!$mailsIds] {
    	die['Mailbox is empty'];
    }
    
    // Get the first message
    // If '__DIR__' was defined in the first line, it will automatically
    // save all attachments to the specified directory
    $mail = $mailbox->getMail[$mailsIds[0]];
    
    // Show, if $mail has one or more attachments
    echo "\nMail has attachments? ";
    if[$mail->hasAttachments[]] {
    	echo "Yes\n";
    } else {
    	echo "No\n";
    }
    
    // Print all information of $mail
    print_r[$mail];
    
    // Print all attachements of $mail
    echo "\n\nAttachments:\n";
    print_r[$mail->getAttachments[]];
    7 hiện đang thực hiện mục đích gọi nhiều chức năng
    // Create PhpImap\Mailbox instance for all further actions
    $mailbox = new PhpImap\Mailbox[
    	'{imap.gmail.com:993/imap/ssl}INBOX', // IMAP server and mailbox folder
    	'', // Username for the before configured mailbox
    	'*********', // Password for the before configured username
    	__DIR__, // Directory, where attachments will be saved [optional]
    	'UTF-8', // Server encoding [optional]
        true, // Trim leading/ending whitespaces of IMAP path [optional]
        false // Attachment filename mode [optional; false = random filename; true = original filename]
    ];
    
    // set some connection arguments [if appropriate]
    $mailbox->setConnectionArgs[
        CL_EXPUNGE // expunge deleted mails upon mailbox close
        | OP_SECURE // don't do non-secure authentication
    ];
    
    try {
    	// Get all emails [messages]
    	// PHP.net imap_search criteria: //php.net/manual/en/function.imap-search.php
    	$mailsIds = $mailbox->searchMailbox['ALL'];
    } catch[PhpImap\Exceptions\ConnectionException $ex] {
    	echo "IMAP connection failed: " . implode[",", $ex->getErrors['all']];
    	die[];
    }
    
    // If $mailsIds is empty, no emails could be found
    if[!$mailsIds] {
    	die['Mailbox is empty'];
    }
    
    // Get the first message
    // If '__DIR__' was defined in the first line, it will automatically
    // save all attachments to the specified directory
    $mail = $mailbox->getMail[$mailsIds[0]];
    
    // Show, if $mail has one or more attachments
    echo "\nMail has attachments? ";
    if[$mail->hasAttachments[]] {
    	echo "Yes\n";
    } else {
    	echo "No\n";
    }
    
    // Print all information of $mail
    print_r[$mail];
    
    // Print all attachements of $mail
    echo "\n\nAttachments:\n";
    print_r[$mail->getAttachments[]];
    8 với mã hóa/giải mã chuỗi tự động của các đối số và trả về giá trị:
  • Khuyến khích

Bài Viết Liên Quan

Chủ Đề