Beispiel #1
0
 /**
  * @param Swift_SignedMessage $message
  *
  * @return Swift_Message
  */
 protected function createMessage(Swift_SignedMessage $message)
 {
     $mimeEntity = new Swift_Message('', $message->getBody(), $message->getContentType(), $message->getCharset());
     $mimeEntity->setChildren($message->getChildren());
     $messageHeaders = $mimeEntity->getHeaders();
     $messageHeaders->remove('Message-ID');
     $messageHeaders->remove('Date');
     $messageHeaders->remove('Subject');
     $messageHeaders->remove('MIME-Version');
     $messageHeaders->remove('To');
     $messageHeaders->remove('From');
     return $mimeEntity;
 }
Beispiel #2
0
 # SendGrid
 $transport = Swift_SmtpTransport::newInstance();
 $transport->setHost('smtp.sendgrid.net');
 $transport->setPort(587);
 $transport->setUsername($this->sub_sendgrid_user);
 $transport->setPassword($this->sub_sendgrid_pass);
 $transport->setEncryption('tls');
 # Create Mailer
 $mailer = Swift_Mailer::newInstance($transport);
 # DKIM
 if ($this->sub_dkim_active) {
     $privateKey = file_get_contents(LETHE_KEY_STORE . DIRECTORY_SEPARATOR . $this->sub_dkim_private);
     $domainName = $this->sub_dkim_domain;
     $selector = $this->sub_dkim_selector;
     $signer = new Swift_Signers_DKIMSigner($privateKey, $domainName, $selector);
     $message = Swift_SignedMessage::newInstance();
     $message->attachSigner($signer);
 } else {
     $message = Swift_Message::newInstance();
 }
 # Create a message
 $message->setEncoder(Swift_Encoding::getBase64Encoding());
 $message->setReplyTo(array($this->sub_reply_mail => $this->sub_from_title));
 $message->setCharset('utf-8');
 $message->setPriority(3);
 $message->setFrom(array($this->sub_from_mail => $this->sub_from_title));
 if ($this->sub_mail_attach != '') {
     $message->attach(Swift_Attachment::fromPath($this->sub_mail_attach)->setFilename(basename($this->sub_mail_attach))->setContentType('application/octet-stream'));
 }
 $headers = $message->getHeaders();
 $headers->addTextHeader('X-Mailer', 'Lethe Newsletter v' . LETHE_VERSION . ' http://www.newslether.com/');
    public function testEncryptThenSignMessage()
    {
        $message = Swift_SignedMessage::newInstance('Wonderful Subject')->setFrom(array('*****@*****.**' => 'John Doe'))->setTo(array('*****@*****.**', '*****@*****.**' => 'A name'))->setBody('Here is the message itself');
        $originalMessage = $this->cleanMessage($message->toString());
        $signer = Swift_Signers_SMimeSigner::newInstance();
        $signer->setSignCertificate($this->samplesDir . 'smime/sign.crt', $this->samplesDir . 'smime/sign.key');
        $signer->setEncryptCertificate($this->samplesDir . 'smime/encrypt.crt');
        $signer->setSignThenEncrypt(false);
        $message->attachSigner($signer);
        $messageStream = $this->newFilteredStream();
        $message->toByteStream($messageStream);
        $messageStream->commit();
        $entityString = $messageStream->getContent();
        $headers = self::getHeadersOfMessage($entityString);
        if (!($boundary = $this->getBoundary($headers['content-type']))) {
            return false;
        }
        $expectedBody = <<<OEL
This is an S/MIME signed message

--{$boundary}
(?P<encrypted_message>MIME-Version: 1\\.0
Content-Disposition: attachment; filename="smime\\.p7m"
Content-Type: application/(x\\-)?pkcs7-mime; smime-type=enveloped-data; name="smime\\.p7m"
Content-Transfer-Encoding: base64

(?:^[a-zA-Z0-9\\/\\r\\n+]*={0,2})


)--{$boundary}
Content-Type: application/(x\\-)?pkcs7-signature; name="smime\\.p7s"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="smime\\.p7s"

(?:^[a-zA-Z0-9\\/\\r\\n+]*={0,2})

--{$boundary}--
OEL;
        if (!$this->assertValidVerify($expectedBody, $messageStream)) {
            return false;
        }
        $expectedBody = str_replace("\n", "\r\n", $expectedBody);
        if (!preg_match('%' . $expectedBody . '*%m', $entityString, $entities)) {
            $this->fail('Failed regex match.');
            return false;
        }
        $messageStreamClean = new Swift_ByteStream_TemporaryFileByteStream();
        $messageStreamClean->write($entities['encrypted_message']);
        $decryptedMessageStream = new Swift_ByteStream_TemporaryFileByteStream();
        if (!openssl_pkcs7_decrypt($messageStreamClean->getPath(), $decryptedMessageStream->getPath(), 'file://' . $this->samplesDir . 'smime/encrypt.crt', array('file://' . $this->samplesDir . 'smime/encrypt.key', 'swift'))) {
            $this->fail(sprintf('Decrypt of the message failed. Internal error "%s".', openssl_error_string()));
        }
        $this->assertEquals($originalMessage, $decryptedMessageStream->getContent());
        unset($messageStreamClean, $messageStream, $decryptedMessageStream);
    }