在使用IMAP和PHP之前,我从未玩过提取和移动电子邮件,所以我很快就会将发送到垃圾箱的移动电子邮件发送回收件箱。
好的,我正确地从Gmail中取出我的电子邮件,然后我可以删除它们,或者将它们移到垃圾箱中。
当我尝试将它们移回收件箱时,我收到以下错误:
注意:未知:[TRYCREATE]第0行的未知中没有文件夹[Gmail] /收件箱(失败)(errflg = 2)
所以显然有一些我不太了解的东西,而且我一直在网上和stackoverflow搜索最后几个小时没有运气的答案。有谁知道为什么这个特定的动作没有成功?
任何帮助都像往常一样非常感激,我很乐意掌握这一点并让自己成为一个小小的电子邮件应用程序。
以下是我所做的课程,请参阅方法undo(),这就是弹出此特定警告的地方。
class Gmail {
public $emailsexists = false;
public $emailarray = '';
private $username;
private $password;
private $inbox;
public function __construct($username,$password) {
/* connect to gmail using imap */
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$this->username = $username;
$this->password = $password;
/* try to connect using username and password set in core/init.php (gmail_credentials) */
$this->inbox = imap_open($hostname,$this->username,$this->password) or die('Cannot connect to Gmail: ' . imap_last_error());
}
public function delete($email) {
if ($email !== '' && is_numeric($email)) {
if (imap_mail_move($this->inbox, $email, '[Gmail]/Trash')) {
return true;
} else {
return false;
}
} else {
return false;
}
imap_close($this->inbox);
}
public function undo($email) {
if ($email !== '' && is_numeric($email)) {
if (imap_mail_move($this->inbox, $email, '[Gmail]/Inbox')) { // FIX Throws back a warning
return true;
} else {
return false;
}
} else {
return false;
}
imap_close($this->inbox);
}
public function unseen($number = false) {
/* grab emails */
$emails = imap_search($this->inbox,'UNSEEN');
/* if emails are returned, cycle through each... */
if($emails) {
/* statement that tells that emails exist */
$this->emailsexists = true;
/* put the newest emails on top */
rsort($emails);
/* Start the counter */
$x = 0;
/* Open up the accordion */
$this->emailarray.= '<div id="accordion">';
/* for every email... */
foreach($emails as $email) {
/* get information specific to this email */
$overview = imap_fetch_overview($this->inbox,$email,0);
$structure = imap_fetchstructure($this->inbox, $email);
$message = imap_fetchbody($this->inbox,$email,1,FT_PEEK);
if(isset($structure->parts) && is_array($structure->parts) && isset($structure->parts[1])) {
$part = $structure->parts[1];
$message = imap_fetchbody($this->inbox,$email,2,FT_PEEK);
}
/* escape and decode the mimeheaders */
$subject = escape(str_replace("_"," ", mb_decode_mimeheader($overview[0]->subject)));
/* decode and put the subject into the accordion header */
$this->emailarray.= '<h3>'.html_entity_decode(escape($subject)).'</h3>';
$this->emailarray.= '<div>';
$this->emailarray.= '<div class="btn-group pull-right">';
$this->emailarray.= '<a href="#" title="Reply" class="btn btn-default btn-xs"><span class="glyphicon glyphicon-share-alt text-success"></span></a>';
$this->emailarray.= '<a href="#" title="Forward" class="btn btn-default btn-xs"><span class="glyphicon glyphicon-send text-success"></span></a>';
$this->emailarray.= '<a href="index.php?task=delete&id='.$email.'" title="Delete" class="btn btn-default btn-xs"><span class="glyphicon glyphicon-fire text-danger"></span></a>';
$this->emailarray.= '</div>';
$this->emailarray.= '<p>';
/* decode the email body using the built in quoted_printable_decode function */
$this->emailarray.= quoted_printable_decode($message);
$this->emailarray.= '</p>';
$this->emailarray.= '</div>';
/* if the counter reaches a certain number, break out and continue the script */
$x++;
if ($number) {
if ($x >= $number) {
break;
}
}
}
/* close off the accordion */
$this->emailarray.= '</div>';
/* If no emails are found return a message */
} else {
echo "No emails";
}
/* close the imap connection */
imap_close($this->inbox);
}
}
答案 0 :(得分:0)
我发现如果我连接到垃圾箱然后使用星号来获取最后一条消息,它就会按预期工作。
public function undo($email) {
// check if message id exists and is numeric
if ($email !== '' && is_numeric($email)) {
// set hostname to trash
$hostname = '{imap.gmail.com:993/imap/ssl}[Gmail]/Trash';
$this->inbox = imap_open($hostname,$this->username,$this->password) or die('Cannot connect to Gmail: ' . imap_last_error());
// return true if the message was moved
if (imap_mail_move($this->inbox, '*', 'Inbox')) {
return true;
} else {
return false;
}
} else {
return false;
}
imap_close($this->inbox,CL_EXPUNGE);
}