забираем письма с Gmail (IMAP)

Проверка, отправка, получение, подтверждение регистраций и всё что касается почты.
Ответить
AoTD
Сообщения: 5
Зарегистрирован: 20 апр 2009, 19:15

забираем письма с Gmail (IMAP)

Сообщение AoTD » 29 окт 2009, 12:23

Метод вытягивает тело письма из gmail ящика. Для работы функции необходимо чтобы в настройках аккаунта был активирован IMAP

Код: Выделить всё

function gmail($mail, $password, $from='', $partno='2.1'){
	$mbox = imap_open('{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX', $mail, $password) or die('Failed with error: '.imap_last_error());
	$messages = imap_sort($mbox, SORTDATE, 1);

	foreach ($messages as $message) {
      $header = imap_header($mbox, $message); 
		if ($from=='')
			$msg[] = $message;
		else if ($from==$header->fromaddress)
			$msg[] = $message;
   }
	$sss = imap_fetchbody($mbox, $msg[0], $partno);
	imap_close($mbox);
	return $sss;
}
Возвращает текст последнего письма от $from (либо просто последнее письмо, если $from не задан).

Пример:

Код: Выделить всё

echo gmail('some_test_mail@gmail.com', 'password', 'support@microsoft.com');
Параметр $partno указывает какой кусок тела письма забирать (т.к. письмо может включать текстовую, html части, прикрепленные файлы...) выдержка из мануала:
(empty) - Entire message
0 - Message header
1 - Body text

With an email message that is a multi-part message in MIME format, and contains the message text in plain text and HTML, and has a file.ext attachment, imap-fetchbody() will return something like the following for each requested part number:

(empty) - Entire message
0 - Message header
1 - MULTIPART/ALTERNATIVE
1.1 - TEXT/PLAIN
1.2 - TEXT/HTML
2 - file.ext

Now if you attach the above email to an email with the message text in plain text and HTML, imap_fetchbody() will use this type of part number system:

(empty) - Entire message
0 - Message header
1 - MULTIPART/ALTERNATIVE
1.1 - TEXT/PLAIN
1.2 - TEXT/HTML
2 - MESSAGE/RFC822 (entire attached message)
2.0 - Attached message header
2.1 - TEXT/PLAIN
2.2 - TEXT/HTML
2.3 - file.ext
Если кто напишет функцию работы с Gmail через POP SSL, буду безмерно благодарен ;)[/code]

cathderay
Сообщения: 32
Зарегистрирован: 09 апр 2009, 23:05

Сообщение cathderay » 30 окт 2009, 19:09

Код: Выделить всё

$box->server("pop.gmail.com","995","pop3/ssl/novalidate-cert");
может так?

Ответить