Ich versuche, über IMAP eine Verbindung zu Google Mail herzustellen, wobei PHP in Apache ausgeführt wird. Dies ist auf einem Ubuntu 9.04-System. Ich habe ein Problem mit der Konfiguration von PHP, das die Arbeit davon abhält. Zuerst habe ich IMAP für PHP eingerichtet:
Sudo apt-get install libc-client2007b libc-client2007b-dev
Sudo apt-get install php5-imap
Sudo /etc/init.d/Apache2 start
Wenn ich phpinfo () starte, erhalte ich die folgenden Imap-Werte:
IMAP c-Client Version: 2004
SSL Support: enabled
Kerberos Support: enabled
Hier ist mein Beispielcode:
<?php
$connect_to = '{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX';
$user = 'my gmail address';
$password = 'my gmail password';
$connection = imap_open($connect_to, $user, $password)
or die("Can't connect to '$connect_to': " . imap_last_error());
imap_close($connection);
?>
Wenn ich diesen Code ausführen, erhalte ich folgende Ausgabe:
Warning: imap_open() [function.imap-open]: Couldn't open stream {imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX in /var/www/clint/gmail/gmail.php on line 10
Can't connect to '{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX': TLS/SSL failure for imap.gmail.com: SSL context failed
Beachten Sie, dass ich von diesem Computer aus imap.gmail.com:993 telnet. Ich kann Evolution (Mailreader) auch über IMAP an Google Mail anschließen und Mail ohne Probleme abrufen. Ich glaube also nicht, dass dies ein Problem mit der Firewall ist. Ich bin mir ziemlich sicher, dass ich etwas in PHP nicht richtig eingerichtet habe.
Irgendwelche Ideen?
Eine weitere zusätzliche Funktion, die Sie in PHP aktivieren müssen, ist die OpenSSL-Erweiterung . Es scheint, dass die IMAP-Client-Bibliothek (mit SSL) davon abhängig ist.
Es spielt keine Rolle, ob das OpenSSL-Modul in Apache aktiviert ist, da dies verarbeitet/verarbeitet wird, bevor die Anforderung an PHP übergeben wird.
Der folgende Diskussionsthread kann helfen, etwas Licht zu erzeugen:
Das hat nach langer Anstrengung für mich funktioniert:
$ServerName = "{imap.gmail.com:993/imap/ssl/novalidate-cert/norsh}Inbox";
Ich hatte das gleiche Problem. Ich benutze Windows und Wamp und meine Wamp-Erweiterung "openSSl" ist aktiviert.
Ich habe dieses Problem mit den folgenden Schritten entfernt. Ich hoffe, das funktioniert auch für Sie.
1) Über den Browser bei Google Mail angemeldet.
2) Öffnen Sie diese URL " https://www.google.com/settings/security/lesssecureapps "
3) Klicken Sie auf "Einschalten".
4) versuche folgenden Code
<?php
set_time_limit(4000);
// Connect to gmail
//$imapPath = '{imap.gmail.com:993/imap/ssl}INBOX';
$imapPath = '{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX';
$username = '[email protected]';
$password = 'Your-password';
// try to connect
$inbox = imap_open($imapPath,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
/* ALL - return all messages matching the rest of the criteria
ANSWERED - match messages with the \\ANSWERED flag set
BCC "string" - match messages with "string" in the Bcc: field
BEFORE "date" - match messages with Date: before "date"
BODY "string" - match messages with "string" in the body of the message
CC "string" - match messages with "string" in the Cc: field
DELETED - match deleted messages
FLAGGED - match messages with the \\FLAGGED (sometimes referred to as Important or Urgent) flag set
FROM "string" - match messages with "string" in the From: field
KEYWORD "string" - match messages with "string" as a keyword
NEW - match new messages
OLD - match old messages
ON "date" - match messages with Date: matching "date"
RECENT - match messages with the \\RECENT flag set
SEEN - match messages that have been read (the \\SEEN flag is set)
SINCE "date" - match messages with Date: after "date"
SUBJECT "string" - match messages with "string" in the Subject:
TEXT "string" - match messages with text "string"
TO "string" - match messages with "string" in the To:
UNANSWERED - match messages that have not been answered
UNDELETED - match messages that are not deleted
UNFLAGGED - match messages that are not flagged
UNKEYWORD "string" - match messages that do not have the keyword "string"
UNSEEN - match messages which have not been read yet*/
// search and get unseen emails, function will return email ids
$emails = imap_search($inbox,'UNSEEN');
$output = '';
foreach($emails as $mail) {
$headerInfo = imap_headerinfo($inbox,$mail);
$output .= $headerInfo->subject.'<br/>';
$output .= $headerInfo->toaddress.'<br/>';
$output .= $headerInfo->date.'<br/>';
$output .= $headerInfo->fromaddress.'<br/>';
$output .= $headerInfo->reply_toaddress.'<br/>';
$emailStructure = imap_fetchstructure($inbox,$mail);
//var_dump($emailStructure->parts);
if(isset($emailStructure->parts)) {
$output .= imap_body($inbox, $mail, FT_PEEK);
} else {
//
}
echo $output;
$output = '';
}
// colse the connection
imap_expunge($inbox);
imap_close($inbox);
?>
VIEL GLÜCK. :)
hatte das gleiche Problem mit der persönlichen Domain bei Google Apps. Problem wurde gelöst, indem der Anwendungszugriff auf das Konto geändert wurde. Folgen Sie einfach per Link und aktivieren Sie den Zugang zum Konto.
Führen Sie Ihren Code über die Befehlszeile aus und prüfen Sie, ob PHP andere Fehler ausspuckt:
php -f gmail.php
Auf meinem Ubuntu habe ich gemacht:
Sudo apt-get install php5-imap
Und das System installiert: libc-client2007b mlock libc-client2007b mlock php5-imap
Wie wäre es dann, wenn Sie php5 deinstallieren und erneut ordnungsgemäß installieren.
Speziell für die IMAP-Server von Google Mail:
Google hat damit begonnen, ungültige Zertifikate für TLSv13-Verbindungen zurückzugeben, die keinen SNI-Hostnamen bereitstellen. Dies führt zu einer Regression, wenn eine PHP-IMAP-Erweiterung für eine aktualisierte OpenSSL-Version (libssl) erstellt wird. Die Probleme wurden bereits hier gemeldet:
Letzteres ist ziemlich neu und ich habe es bei den Ubuntu-Betreuern eingereicht, um es für sie sichtbar zu machen und hoffentlich mehr Aufmerksamkeit zu erregen, da es Ubuntu Bionic betrifft, das eine LTS-Version ist.
Bitte stimmen Sie ab und/oder kommentieren Sie sie, um die Sichtbarkeit zu verbessern. Denken Sie auch daran: Wenn Sie diese Antwort nützlich und es ist auch genug positiv bewertet, werden die oben genannten Probleme noch sichtbarer.
In der Zwischenzeit scheint mir die einzige Problemumgehung, bis der Fehler behoben ist, das Deaktivieren der Zertifikatüberprüfung mithilfe des Flags /novalidate-cert
Beim Aufrufen von imap_open () , was ein Sicherheitsrisiko darstellt .
Aktivieren Sie zunächst weniger sichere Apps in Ihrem Google Mail-Konto: https://myaccount.google.com/lesssecureapps
Verwenden Sie diese Konfiguration zum Erstellen einer IMAP-Verbindung:
$imap_connection = imap_open('{imap.gmail.com:993/imap/ssl/novalidate-
cert}INBOX', 'YOUR GMAIL USER', 'YOUR GMAIL PASSWORD');
Hinweis : INBOX ist Ihre Haupt-Imbox. Sie können z. B. auf gesendete Elemente zugreifen, indem Sie: INBOX.Sent in Ihrer Verbindung verwenden.
Wenn Sie weiterhin Probleme mit Google Mail haben, aktivieren Sie auf der Seite mit den Sicherheitseinstellungen für Google-Konten die Option "Zugriff für weniger sichere Apps zulassen".
Überprüfen Sie Ihr Setup mit phpinfo()
und stellen Sie sicher, dass --with-imap-ssl
aufgelistet ist.