Чтобы прочитать электронную почту с помощью PHP, вы можете использовать расширение IMAP, которое позволяет вам подключаться к серверу электронной почты и получать сообщения. Вот пример фрагмента кода, который поможет вам начать:
// Server connection settings
$server = '{imap.example.com:993/imap/ssl}INBOX';
$username = 'your_username';
$password = 'your_password';
// Connect to the server
$connection = imap_open($server, $username, $password);
if ($connection) {
// Get the number of emails in the inbox
$numMessages = imap_num_msg($connection);
// Loop through each email
for ($i = 1; $i <= $numMessages; $i++) {
// Get the email headers
$header = imap_header($connection, $i);
// Extract the subject
$subject = $header->subject;
// Print the subject
echo "Subject: $subject<br>";
}
// Close the connection
imap_close($connection);
} else {
echo "Failed to connect to the mail server.";
}
Этот код подключается к серверу IMAP с помощью SSL, получает количество сообщений в папке «Входящие», а затем просматривает каждое электронное письмо, извлекая и печатая тему.