Beispiel #1
0
 public function testMsgHandleMail()
 {
     $server = new Server('', 0);
     $server->setLog(new Logger('test_application'));
     $server->init();
     $client = new Client();
     $client->setServer($server);
     $client->setId(1);
     $msg = $client->msgHandle('MAIL FROM:<*****@*****.**>');
     $this->assertEquals('500 Syntax error, command unrecognized' . Client::MSG_SEPARATOR, $msg);
     $msg = $client->msgHandle('HELO localhost.localdomain');
     $this->assertEquals('250 localhost.localdomain' . Client::MSG_SEPARATOR, $msg);
     $msg = $client->msgHandle('MAIL');
     $this->assertEquals('501 Syntax error in parameters or arguments' . Client::MSG_SEPARATOR, $msg);
     $msg = $client->msgHandle('MAIL FROM:<*****@*****.**>');
     $this->assertEquals('250 OK' . Client::MSG_SEPARATOR, $msg);
     $msg = $client->msgHandle('RCPT');
     $this->assertEquals('501 Syntax error in parameters or arguments' . Client::MSG_SEPARATOR, $msg);
     $msg = $client->msgHandle('RCPT TO:<*****@*****.**>');
     $this->assertEquals('250 OK' . Client::MSG_SEPARATOR, $msg);
     $msg = $client->msgHandle('RCPT TO:<*****@*****.**>');
     $this->assertEquals('250 OK' . Client::MSG_SEPARATOR, $msg);
     $msg = $client->msgHandle('DATA');
     $this->assertEquals('354 Start mail input; end with <CRLF>.<CRLF>' . Client::MSG_SEPARATOR, $msg);
     $msg = $client->msgHandle('From: TheFox <*****@*****.**>');
     $this->assertEquals('', $msg);
     $msg = $client->msgHandle('To: TheFox <*****@*****.**>');
     $this->assertEquals('', $msg);
     $msg = $client->msgHandle('Subject: Test');
     $this->assertEquals('', $msg);
     $msg = $client->msgHandle('');
     $this->assertEquals('', $msg);
     $msg = $client->msgHandle('Body');
     $this->assertEquals('', $msg);
     $msg = $client->msgHandle('.');
     $this->assertEquals('250 OK' . Client::MSG_SEPARATOR, $msg);
     $msg = $client->msgHandle('QUIT');
     $this->assertEquals('221 localhost.localdomain Service closing transmission channel' . Client::MSG_SEPARATOR, $msg);
 }
Beispiel #2
0
 public function testEvent()
 {
     $server = new Server('', 0);
     $server->setLog(new Logger('test_application'));
     $server->init();
     $testData = 21;
     $event1 = new Event(Event::TRIGGER_MAIL_NEW, null, function ($event, $from, $rcpt, $mail) use(&$testData) {
         #fwrite(STDOUT, 'my function: '.$event->getTrigger().', '.$testData."\n");
         $testData = 24;
         $this->assertEquals('*****@*****.**', $from);
         $this->assertEquals(array('*****@*****.**', '*****@*****.**', '*****@*****.**', '*****@*****.**'), $rcpt);
         $current = array();
         foreach ($mail->getTo() as $n => $address) {
             $current[] = $address->toString();
         }
         $this->assertEquals(array('Joe User <*****@*****.**>', '<*****@*****.**>'), $current);
         $this->assertEquals('Here is the subject', $mail->getSubject());
         return 42;
     });
     $server->eventAdd($event1);
     $mail = '';
     $mail .= 'Date: Thu, 31 Jul 2014 22:18:51 +0200' . Client::MSG_SEPARATOR;
     $mail .= 'To: Joe User <*****@*****.**>, to2@example.com' . Client::MSG_SEPARATOR;
     $mail .= 'From: Mailer <*****@*****.**>' . Client::MSG_SEPARATOR;
     $mail .= 'Cc: cc@example.com' . Client::MSG_SEPARATOR;
     $mail .= 'Reply-To: Information <*****@*****.**>' . Client::MSG_SEPARATOR;
     $mail .= 'Subject: Here is the subject' . Client::MSG_SEPARATOR;
     $mail .= 'MIME-Version: 1.0' . Client::MSG_SEPARATOR;
     $mail .= 'Content-Type: text/plain; charset=iso-8859-1' . Client::MSG_SEPARATOR;
     $mail .= 'Content-Transfer-Encoding: 8bit' . Client::MSG_SEPARATOR;
     $mail .= '' . Client::MSG_SEPARATOR;
     $mail .= 'This is the message body.' . Client::MSG_SEPARATOR;
     $mail .= 'END' . Client::MSG_SEPARATOR;
     $zmail = Message::fromString($mail);
     $rcpt = array('*****@*****.**', '*****@*****.**', '*****@*****.**', '*****@*****.**');
     $server->mailNew('*****@*****.**', $rcpt, $zmail);
     $this->assertEquals(24, $testData);
     $this->assertEquals(42, $event1->getReturnValue());
 }
Beispiel #3
0
 public function testEventAuthWithAllTrue()
 {
     $server = new Server('', 0);
     $server->setLog(new Logger('test_application'));
     $server->init();
     $username = '******';
     $password = '******';
     $phpunit = $this;
     $event1 = new Event(Event::TRIGGER_AUTH_ATTEMPT, null, function ($event, $method, $credentials) {
         return true;
     });
     $server->eventAdd($event1);
     $testObj = new TestObj();
     $event2 = new Event(Event::TRIGGER_AUTH_ATTEMPT, $testObj, 'test2');
     $server->eventAdd($event2);
     $method = 'LOGIN';
     $credentials = array('user' => base64_encode($username), 'password' => base64_encode($password));
     $authenticated = $server->authenticateUser($method, $credentials);
     $this->assertTrue($authenticated);
 }
Beispiel #4
0
$dn = array('countryName' => 'UK', 'stateOrProvinceName' => 'Isle Of Wight', 'localityName' => 'Cowes', 'organizationName' => 'Open Sauce Systems', 'organizationalUnitName' => 'Dev', 'commonName' => '127.0.0.1', 'emailAddress' => '*****@*****.**');
// Generate certificate
$privkey = openssl_pkey_new();
$cert = openssl_csr_new($dn, $privkey);
$cert = openssl_csr_sign($cert, null, $privkey, 365);
// Generate PEM file
$pem = array();
openssl_x509_export($cert, $pem[0]);
openssl_pkey_export($privkey, $pem[1]);
$pem = implode($pem);
// Save PEM file
$pemfile = __DIR__ . '/server.pem';
file_put_contents($pemfile, $pem);
$contextOptions = array('ssl' => array('verify_peer' => false, 'local_cert' => $pemfile, 'allow_self_signed' => true));
$server = new Server('127.0.0.1', 20025);
$server->init();
$server->listen($contextOptions);
$sendEvent = new Event(Event::TRIGGER_MAIL_NEW, null, function ($event, $from, $rcpts, $mail) {
    // Do stuff: DNS lookup the MX record for the recipient's domain, ...
    // For example, use PHPMailer to reply the mail through mail servers.
    $mailer = new PHPMailer();
    $mailer->IsSMTP();
    $mailer->SMTPAuth = true;
    $mailer->SMTPSecure = 'tls';
    $mailer->Host = 'smtp.example.com';
    $mailer->Port = 587;
    $mailer->Username = '******';
    $mailer->Password = '******';
    $mailer->SetFrom('*****@*****.**', 'John Doe');
    $mailer->Subject = $mail->getSubject();
    $mailer->AltBody = $mail->getBody();
Beispiel #5
0
 public function testMsgHandleStartTls()
 {
     $server = new Server('', 0);
     $server->setLog(new Logger('test_application'));
     $server->init();
     $socket = $this->getMock('TheFox\\Network\\StreamSocket', array('enableEncryption'));
     $socket->expects($this->at(0))->method('enableEncryption')->will($this->throwException(new RuntimeException()));
     $socket->expects($this->at(1))->method('enableEncryption')->will($this->returnValue(true));
     $client = new Client();
     $client->setServer($server);
     $client->setId(1);
     $client->setSocket($socket);
     $msg = $client->msgHandle('EHLO localhost.localdomain');
     $expect = '250-localhost.localdomain' . Client::MSG_SEPARATOR;
     $expect .= '250-AUTH PLAIN LOGIN' . Client::MSG_SEPARATOR;
     $expect .= '250-STARTTLS' . Client::MSG_SEPARATOR;
     $expect .= '250 HELP' . Client::MSG_SEPARATOR;
     $this->assertEquals($expect, $msg);
     $msg = $client->msgHandle('STARTTLS PARAMETER');
     $this->assertEquals('501 Syntax error in parameters or arguments' . Client::MSG_SEPARATOR, $msg);
     $msg = $client->msgHandle('STARTTLS');
     $this->assertEquals('454 TLS not available due to temporary reason' . Client::MSG_SEPARATOR, $msg);
     $msg = $client->msgHandle('STARTTLS');
     $this->assertTrue($msg);
 }