public function run() { // Read the contents of the message in from the buffer $src = ''; $stream = fopen($this->_emailSource, 'r'); // The first line in the stream is a postfix message, so we discard it fgets($stream); while ($line = fgets($stream)) { $src .= $line; } fclose($stream); // Create a Zend\Mail\Message object with our message $email = \Zend\Mail\Message::fromString($src); $from = $email->getHeaders()->get('From')->getAddressList()->current()->getEmail(); $email->setTo($from); // Create our transport $transport = new \Zend\Mail\Transport\Smtp(); $options = new \Zend\Mail\Transport\SmtpOptions(array('host' => $this->_opts['smtp_server'])); $transport->setOptions($options); $transport->send($email); }
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()); }
public function msgHandle($msgRaw) { #$this->log('debug', 'client '.$this->id.' raw: /'.$msgRaw.'/'); $rv = ''; $str = new StringParser($msgRaw); $args = $str->parse(); #ve($args); $command = array_shift($args); $commandcmp = strtolower($command); if ($commandcmp == 'helo') { #$this->log('debug', 'client '.$this->id.' helo'); $this->setStatus('hasHello', true); return $this->sendOk($this->getHostname()); } elseif ($commandcmp == 'ehlo') { #$this->log('debug', 'client '.$this->id.' helo'); $this->setStatus('hasHello', true); $msg = '250-' . $this->getHostname() . static::MSG_SEPARATOR; $count = count($this->extendedCommands) - 1; for ($i = 0; $i < $count; $i++) { $msg .= '250-' . $this->extendedCommands[$i] . static::MSG_SEPARATOR; } $msg .= '250 ' . end($this->extendedCommands); return $this->dataSend($msg); } elseif ($commandcmp == 'mail') { #$this->log('debug', 'client '.$this->id.' mail'); #ve($args); if ($this->getStatus('hasHello')) { if (isset($args[0]) && $args[0]) { $this->setStatus('hasMail', true); $from = $args[0]; if (substr(strtolower($from), 0, 6) == 'from:<') { $from = substr(substr($from, 6), 0, -1); } #$this->log('debug', 'client '.$this->id.' from: /'.$from.'/'); $this->from = $from; $this->mail = ''; return $this->sendOk(); } else { return $this->sendSyntaxErrorInParameters(); } } else { return $this->sendSyntaxErrorCommandUnrecognized(); } } elseif ($commandcmp == 'rcpt') { #$this->log('debug', 'client '.$this->id.' rcpt'); #ve($args); if ($this->getStatus('hasHello')) { if (isset($args[0]) && $args[0]) { $this->setStatus('hasMail', true); $rcpt = $args[0]; if (substr(strtolower($rcpt), 0, 4) == 'to:<') { $rcpt = substr(substr($rcpt, 4), 0, -1); $this->rcpt[] = $rcpt; } #$this->log('debug', 'client '.$this->id.' rcpt: /'.$rcpt.'/'); return $this->sendOk(); } else { return $this->sendSyntaxErrorInParameters(); } } else { return $this->sendSyntaxErrorCommandUnrecognized(); } } elseif ($commandcmp == 'data') { #$this->log('debug', 'client '.$this->id.' data'); if ($this->getStatus('hasHello')) { $this->setStatus('hasData', true); return $this->sendDataResponse(); } else { return $this->sendSyntaxErrorCommandUnrecognized(); } } elseif ($commandcmp == 'noop') { return $this->sendOk(); } elseif ($commandcmp == 'quit') { $rv .= $this->sendQuit(); $this->shutdown(); } elseif ($commandcmp == 'auth') { $this->setStatus('hasAuth', true); if (empty($args)) { return $this->sendSyntaxErrorInParameters(); } $authentication = strtolower($args[0]); if ($authentication == 'plain') { $this->setStatus('hasAuthPlain', true); if (isset($args[1])) { $this->setStatus('hasAuthPlainUser', true); $this->setCredentials(array($args[1])); if ($this->authenticate('plain')) { return $this->sendAuthSuccessResponse(); } return $this->sendAuthInvalid(); } return $this->sendAuthPlainResponse(); } elseif ($authentication == 'login') { $this->setStatus('hasAuthLogin', true); return $this->sendAskForUserResponse(); } elseif ($authentication == 'cram-md5') { return $this->sendCommandNotImplemented(); } else { return $this->sendSyntaxErrorInParameters(); } } elseif ($commandcmp == 'starttls') { if (!empty($args)) { return $this->sendSyntaxErrorInParameters(); } $this->sendReadyStartTls(); try { return $this->getSocket()->enableEncryption(); } catch (RuntimeException $e) { return $this->sendTemporaryErrorStartTls(); } } elseif ($commandcmp == 'help') { return $this->sendOk('HELO, EHLO, MAIL FROM, RCPT TO, DATA, NOOP, QUIT'); } else { if ($this->getStatus('hasAuth')) { if ($this->getStatus('hasAuthPlain')) { $this->setStatus('hasAuthPlainUser', true); $this->setCredentials(array($command)); if ($this->authenticate('plain')) { return $this->sendAuthSuccessResponse(); } return $this->sendAuthInvalid(); } elseif ($this->getStatus('hasAuthLogin')) { $credentials = $this->getCredentials(); if ($this->getStatus('hasAuthLoginUser')) { $credentials['password'] = $command; $this->setCredentials($credentials); if ($this->authenticate('login')) { return $this->sendAuthSuccessResponse(); } return $this->sendAuthInvalid(); } $this->setStatus('hasAuthLoginUser', true); $credentials['user'] = $command; $this->setCredentials($credentials); return $this->sendAskForPasswordResponse(); } } elseif ($this->getStatus('hasData')) { if ($msgRaw == '.') { $this->mail = substr($this->mail, 0, -strlen(static::MSG_SEPARATOR)); $zmail = Message::fromString($this->mail); $this->getServer()->mailNew($this->from, $this->rcpt, $zmail); $this->from = ''; $this->rcpt = array(); $this->mail = ''; return $this->sendOk(); } else { $this->mail .= $msgRaw . static::MSG_SEPARATOR; } } else { $this->log('debug', 'client ' . $this->id . ' not implemented: /' . $command . '/ - /' . join('/ /', $args) . '/'); return $this->sendSyntaxErrorCommandUnrecognized(); } } return $rv; }
/** * Email from file * * Given a filename, returns the email's object * * @return Zend\Mail\Message * @author Koen Punt **/ protected function emailFromFile($email) { return Message::fromString(file_get_contents($email)); }
public function testRestoreFromSerializedString() { $this->message->addTo('*****@*****.**', 'ZF DevTeam'); $this->message->addFrom('*****@*****.**', "Matthew Weier O'Phinney"); $this->message->addCc('*****@*****.**', 'ZF Contributors List'); $this->message->setSubject('This is a subject'); $this->message->setBody('foo'); $serialized = $this->message->toString(); $restoredMessage = Message::fromString($serialized); $this->assertEquals($serialized, $restoredMessage->toString()); }
private function sendAppend($data = '') { $appendMsgLen = strlen($this->getStatus('appendMsg')); #$this->log('debug', 'client '.$this->id.' append step: '.$this->getStatus('appendStep')); #$this->log('debug', 'client '.$this->id.' append len: '.$appendMsgLen); #$this->log('debug', 'client '.$this->id.' append lit: '.$this->getStatus('appendLiteral')); if ($this->getStatus('appendStep') == 1) { $this->status['appendStep']++; return $this->dataSend('+ Ready for literal data'); } elseif ($this->getStatus('appendStep') == 2) { if ($appendMsgLen < $this->getStatus('appendLiteral')) { $this->status['appendMsg'] .= $data . Headers::EOL; $appendMsgLen = strlen($this->getStatus('appendMsg')); } if ($appendMsgLen >= $this->getStatus('appendLiteral')) { $this->status['appendStep']++; $this->log('debug', 'client ' . $this->id . ' append len reached: ' . $appendMsgLen); $message = Message::fromString($this->getStatus('appendMsg')); try { $this->getServer()->addMail($message, $this->getStatus('appendFolder'), $this->getStatus('appendFlags'), false); $this->log('debug', 'client ' . $this->id . ' append completed: ' . $this->getStatus('appendStep')); return $this->sendOk('APPEND completed', $this->getStatus('appendTag')); } catch (Exception $e) { $noMsg = 'Can not get folder: ' . $this->getStatus('appendFolder'); return $this->sendNo($noMsg, $this->getStatus('appendTag'), 'TRYCREATE'); } } else { $diff = $this->getStatus('appendLiteral') - $appendMsgLen; $this->log('debug', 'client ' . $this->id . ' append left: ' . $diff . ' (' . $appendMsgLen . ')'); } } }
public function getMailById($msgId) { $storage = $this->getDefaultStorage(); $mailStr = $storage->getPlainMailById($msgId); $mail = ZendMailMessage::fromString($mailStr); return $mail; }