[英]Move mail from Trash to Inbox Gmail PHP Imap
在使用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);
}
}
我發現,如果我連接到垃圾箱,然后使用星號獲取最后一條消息,則該消息將按預期工作。
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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.