Hier werden die Unterschiede zwischen zwei Versionen gezeigt.
Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
php:imap [2020/10/18 17:11] webproducer angelegt |
php:imap [2022/08/17 10:49] (aktuell) webproducer SSL ergänzt |
||
---|---|---|---|
Zeile 15: | Zeile 15: | ||
private $username = ''; | private $username = ''; | ||
private $password = ''; | private $password = ''; | ||
+ | private $port = 143; | ||
+ | private $type = 'IMAP'; | ||
+ | private $ssl = true; | ||
/** | /** | ||
Zeile 23: | Zeile 26: | ||
public function getEmails( $onlyUnread = false ) | public function getEmails( $onlyUnread = false ) | ||
{ | { | ||
- | $mailbox = sprintf( "{%s:143}INBOX", $this->server ); | + | $ssl = ''; |
+ | |||
+ | if( $this->ssl ) | ||
+ | { | ||
+ | $ssl = '/ssl'; | ||
+ | } | ||
+ | |||
+ | $mailbox = sprintf( "{%s:%d/%s%s/novalidate-cert}INBOX", $this->server, $this->port, $this->type, $ssl ); | ||
$this->stream = imap_open( $mailbox, $this->username, $this->password ); | $this->stream = imap_open( $mailbox, $this->username, $this->password ); | ||
$MC = imap_check( $this->stream ); | $MC = imap_check( $this->stream ); | ||
Zeile 46: | Zeile 56: | ||
$body = imap_fetchbody( $this->stream, $uid, 1 ); | $body = imap_fetchbody( $this->stream, $uid, 1 ); | ||
return imap_qprint( $body ); | return imap_qprint( $body ); | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * @param string $uid | ||
+ | * @return bool|string | ||
+ | */ | ||
+ | public function getAttachments( $uid ) | ||
+ | { | ||
+ | $structure = imap_fetchstructure( $this->stream, $uid ); | ||
+ | |||
+ | foreach( $structure->parts as $key=>$part ) | ||
+ | { | ||
+ | if( isset( $part->disposition ) && $part->disposition === 'attachment' ) | ||
+ | { | ||
+ | $attachment = imap_fetchbody( $this->stream, $uid, $key+1, FT_INTERNAL ); | ||
+ | return str_replace( '=0A=', '', $attachment ); // Zeilenumbrueche | ||
+ | } | ||
+ | } | ||
+ | |||
+ | return false; | ||
} | } | ||
public function close() | public function close() | ||
{ | { | ||
- | imap_close( $this->stream ); | + | if( $this->stream !== false ) |
+ | { | ||
+ | imap_close( $this->stream ); | ||
+ | } | ||
} | } | ||
} | } | ||
Zeile 74: | Zeile 107: | ||
Mit [[php:preg|regulären Ausdrücken]] wäre es jetzt z. B. Möglich, den Inhalt der E-Mails nach bestimmten Inhalten zu parsen. | Mit [[php:preg|regulären Ausdrücken]] wäre es jetzt z. B. Möglich, den Inhalt der E-Mails nach bestimmten Inhalten zu parsen. | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== Beispiel für Dateianhänge (Attachments) ===== | ||
+ | |||
+ | <code php> | ||
+ | $imap = new imap(); | ||
+ | $emails = $imap->getEmails( true ); | ||
+ | |||
+ | foreach( $emails as $email ) | ||
+ | { | ||
+ | $attachment = $imap->getAttachments( $email->uid ); | ||
+ | |||
+ | if( $attachment !== false ) | ||
+ | { | ||
+ | echo "Anhang gefunden: " . $attachment; | ||
+ | } | ||
+ | } | ||
+ | </code> |