我已经搜索并阅读了很多与此有关的问题。我将收到的电子邮件保存到MySQL数据库中。我正在使用管道到脚本方法,并且已经设置了这些内容,并且在收到电子邮件时文件可以正确读取。
我收到的错误是:
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to server: ' . imap_last_error());
我对$ hostname变量感到困惑,我已经尝试了很多次,但是失败了。
通常,我的服务器是MySQL的本地主机。我尝试过,我的服务器使用SSL。我使用了此localhost:993/imap/ssl}INBOX
,但没有一个工作。该变量的正确服务器是什么?
imap.mydomain.org:993 ???
[请帮助我解决此问题。我受够了。
按照您所说的,您是否尝试过:
{localhost:993/imap/ssl}INBOX
或
{imap.mydomain.org:993/imap/ssl}INBOX
如果不起作用,您可以尝试提供错误输出吗?
真正的错误消息是证书失败,如OP所述。
要解决该问题,请将功能更改为:
$mbox = imap_open('{imap.maydomain.org:993/novalidate-cert}INBOX', '[email protected]', 'userpass') or die('Cannot connect to server: ' . imap_last_error());
绕过验证自签名证书。
这里附有IMAP PHP邮件接收系统的完整源代码
<?php
//The location of the mailbox.
$mailbox = '{imap.myserver.net:993/ssl}';
//The username / email address that we want to login to.
$username = '[email protected]';
//The password for this email address.
$password = '********';
//Attempt to connect using the imap_open function.
$imapResource = imap_open($mailbox, $username, $password);
//If the imap_open function returns a boolean FALSE value,
//then we failed to connect.
if($imapResource === false){
//If it failed, throw an exception that contains
//the last imap error.
throw new Exception(imap_last_error());
}
//If we get to this point, it means that we have successfully
//connected to our mailbox via IMAP.
//Lets get all emails that were received since a given date.
$search = 'SINCE "' . date("j F Y", strtotime("-7 days")) . '"';
$emails = imap_search($imapResource, $search);
//If the $emails variable is not a boolean FALSE value or
//an empty array.
if(!empty($emails)){
//Loop through the emails.
foreach($emails as $email){
//Fetch an overview of the email.
$overview = imap_fetch_overview($imapResource, $email);
$overview = $overview[0];
//Print out the subject of the email.
echo '<b>' . htmlentities($overview->subject) . '</b><br>';
//Print out the sender's email address / from email address.
echo 'From: ' . $overview->from . '<br><br>';
//Get the body of the email.
$message = imap_fetchbody($imapResource, $email, 1, FT_PEEK);
if (preg_match('/^([a-zA-Z0-9]{76} )+[a-zA-Z0-9]{76}$/', $message)) {
$message = base64_decode($message);
}
echo "<div class='faq-tile'>$message</div>";
}
}