Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add method to copy messages #176

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/Fetch/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -794,4 +794,24 @@ public function moveToMailBox($mailbox)

return $returnValue;
}

/**
* Copy a message to the given mailbox.
*
* @param $mailbox
*
* @return bool
*/
public function copyToMailBox($mailbox)
{
$currentBox = $this->imapConnection->getMailBox();
$this->imapConnection->setMailBox($this->mailbox);

$returnValue = imap_mail_copy($this->imapStream, $this->uid, $mailbox, CP_UID);

$this->imapConnection->setMailBox($currentBox);

return $returnValue;
}

}
35 changes: 35 additions & 0 deletions tests/Fetch/Test/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,41 @@ public function testMoveToMailbox()
$this->assertEquals($sentFolderNumStart + 1, $server->numMessages(), 'Message moved into Sent Folder.');
}

public function testCopyToMailbox()
{
$server = ServerTest::getServer();

// Testing by copying message from "Test Folder" to "Sent"

// Count Test Folder
$testFolderNumStart = $server->numMessages('Test Folder');
$server->setMailbox('Test Folder');
$this->assertEquals($testFolderNumStart, $server->numMessages(), 'Server presents consistent information between numMessages when mailbox set and directly queried for number of messages');

// Get message from Test Folder
$message = current($server->getMessages(1));
$this->assertInstanceOf('\Fetch\Message', $message, 'Server returned Message.');

// Switch to Sent folder, count messages
$sentFolderNumStart = $server->numMessages('Sent');
$server->setMailbox('Sent');
$this->assertEquals($sentFolderNumStart, $server->numMessages(), 'Server presents consistent information between numMessages when mailbox set and directly queried for number of messages');

// Switch to "Flagged" folder in order to test that function properly returns to it
$this->assertTrue($server->setMailBox('Flagged Email'));
// Move the message!
$this->assertTrue($message->copyToMailBox('Sent'));
// Make sure we're still in the same folder
$this->assertEquals('Flagged Email', $server->getMailBox(), 'Returned Server back to right mailbox.');
$this->assertAttributeEquals('Test Folder', 'mailbox', $message, 'Message mailbox remained unchanged.');
// Make sure Test Folder didn't lose a message
$this->assertTrue($server->setMailBox('Test Folder'));
$this->assertEquals($testFolderNumStart, $server->numMessages(), 'Message copied from Test Folder.');
// Make sure Sent folder gains one
$this->assertTrue($server->setMailBox('Sent'));
$this->assertEquals($sentFolderNumStart + 1, $server->numMessages(), 'Message copied into Sent folder.');
}

public function testDecode()
{
$quotedPrintableDecoded = "Now's the time for all folk to come to the aid of their country.";
Expand Down