newInstance() публичный статический Метод

Create a new MimePart.
public static newInstance ( string $body = null, string $contentType = null, string $charset = null ) : Swift_Mime_MimePart
$body string
$contentType string
$charset string
Результат Swift_Mime_MimePart
Пример #1
0
 public function testBodySwap()
 {
     $message1 = new Swift_Message('Test');
     $html = Swift_MimePart::newInstance('<html></html>', 'text/html');
     $html->getHeaders()->addTextHeader('X-Test-Remove', 'Test-Value');
     $html->getHeaders()->addTextHeader('X-Test-Alter', 'Test-Value');
     $message1->attach($html);
     $source = $message1->toString();
     $message2 = clone $message1;
     $message2->setSubject('Message2');
     foreach ($message2->getChildren() as $child) {
         $child->setBody('Test');
         $child->getHeaders()->removeAll('X-Test-Remove');
         $child->getHeaders()->get('X-Test-Alter')->setValue('Altered');
     }
     $final = $message1->toString();
     if ($source != $final) {
         $this->fail("Difference although object cloned \n [" . $source . "]\n[" . $final . "]\n");
     }
     $final = $message2->toString();
     if ($final == $source) {
         $this->fail('Two body matches although they should differ' . "\n [" . $source . "]\n[" . $final . "]\n");
     }
     $id_1 = $message1->getId();
     $id_2 = $message2->getId();
     $this->assertEquals($id_1, $id_2, 'Message Ids differ');
     $id_2 = $message2->generateId();
     $this->assertNotEquals($id_1, $id_2, 'Message Ids are the same');
 }
 public function testSetText()
 {
     $setText = \Closure::bind(function (&$data, $message) {
         return $this->setText($data, $message);
     }, $this->transport, 'Sichikawa\\LaravelSendgridDriver\\Transport\\SendGridTransport');
     $data = [];
     $message = new Message($this->getMessage());
     $message->getSwiftMessage()->setChildren([Swift_MimePart::newInstance('This is a test.')]);
     $setText($data, $message->getSwiftMessage());
     $this->assertEquals('This is a test.', $data['text']);
 }
Пример #3
0
 /**
  * Adds body parts to the message.
  *
  * @param Email $notification
  * @param \Swift_Message $email
  * @param string $boundary
  */
 protected function addParts(Email $notification, \Swift_Message $email, $boundary)
 {
     foreach ($notification->getParts() as $part) {
         $mimePart = \Swift_MimePart::newInstance($part->getContent(), $part->getContentType());
         $mimePart->setBoundary($boundary);
         if ($encoder = $this->getEncoder($part)) {
             $mimePart->setEncoder($encoder);
         }
         $email->attach($mimePart);
     }
 }
 public function testGetContents()
 {
     $getContents = \Closure::bind(function ($message) {
         return $this->getContents($message);
     }, $this->transport, SendgridV3Transport::class);
     $message = new Message($this->getMessage());
     $message->getSwiftMessage()->setChildren([Swift_MimePart::newInstance('This is a test.')]);
     $res = $getContents($message->getSwiftMessage());
     $this->assertEquals('text/plain', array_get($res, '0.type'));
     $this->assertEquals('This is a test.', array_get($res, '0.value'));
     $this->assertEquals('text/html', array_get($res, '1.type'));
     $this->assertEquals('Test body.', array_get($res, '1.value'));
 }
Пример #5
0
 /**
  * {@inheritdoc}
  */
 public function notify(NotificationInterface $notification)
 {
     if (!class_exists('Swift_Message')) {
         throw new \RuntimeException('You need to install swift mailer to use mailgun transport');
     }
     /** @var Email $notification */
     $message = \Swift_Message::newInstance($notification->getSubject())->setFrom($notification->getFrom());
     foreach ($notification->getParts() as $part) {
         $message->attach(\Swift_MimePart::newInstance($part->getContent(), $part->getContentType()));
     }
     foreach ($notification->getAttachments() as $attachment) {
         $message->attach(\Swift_Attachment::newInstance($attachment->getContent(), $attachment->getName(), $attachment->getContentType()));
     }
     $postData = ['o:tag' => $notification->getTags()];
     foreach ($notification->getMetadata() as $key => $value) {
         $postData['v:' . $key] = $value;
     }
     if ($recipientVariables = $notification->getRecipientVariables()) {
         $postData['recipient-variables'] = json_encode($recipientVariables);
     }
     $failed = [];
     $success = [];
     $to = array_merge(array_values($notification->getTo()), array_values($notification->getCc()), array_values($notification->getBcc()));
     if (!empty($to)) {
         foreach (array_chunk($to, 1000) as $to_chunk) {
             $result = new Result('mailgun', $this->getName());
             $data = $postData;
             $data['to'] = $to_chunk;
             $res = $this->mailgun->sendMessage($this->domain, $data, $message->toString());
             if ($res->http_response_code == 200) {
                 $success[] = $res;
             } else {
                 $result->setResult(Result::FAIL);
                 $failed[] = $res;
             }
             $result->setResponse($res);
             $notification->addResult($result);
         }
         if (count($success) === 0) {
             throw new NotificationFailedException("Sending failed for message {$notification->getSubject()}", $failed);
         }
     }
 }
 public function setPlain($plain)
 {
     if (!isset($this->plainMimePart)) {
         $this->plainMimePart = \Swift_MimePart::newInstance($plain, 'text/plain');
     } else {
         $this->emailObj->detach($this->plainMimePart);
         $this->plainMimePart->setBody($plain);
     }
     if (!empty($plain)) {
         $this->emailObj->attach($this->plainMimePart);
     }
 }
Пример #7
0
 /**
  * Add a MimePart to this Message.
  * @param string|Swift_OutputByteStream $body
  * @param string $contentType
  * @param string $charset
  */
 public function addPart($body, $contentType = null, $charset = null)
 {
     return $this->attach(Swift_MimePart::newInstance($body, $contentType, $charset));
 }
 protected function _createMimePart()
 {
     Swift_DependencyContainer::getInstance()->register('properties.charset')->asValue(null);
     return Swift_MimePart::newInstance();
 }
Пример #9
0
 /**
  * Add a MimePart to this Message.
  * @param string|Swift_OutputByteStream $body
  * @param string                        $contentType
  * @param string                        $charset
  */
 public function addPart($body, $contentType = null, $charset = null)
 {
     $part = Swift_MimePart::newInstance($body, $contentType, $charset);
     $part->setEncoder($this->getEncoder());
     return $this->attach($part);
 }