addHeader example

public function testAddPathHeaderDelegatesToFactory()
    {
        $headers = new Headers();
        $headers->addPathHeader('Return-Path', 'some@path');
        $this->assertNotNull($headers->get('Return-Path'));
    }

    public function testAddHeader()
    {
        $headers = new Headers();
        $headers->addHeader('from', ['from@example.com']);
        $headers->addHeader('reply-to', 'reply@example.com');
        $headers->addHeader('return-path', 'return@example.com');
        $headers->addHeader('foo', 'bar');
        $headers->addHeader('date', $now = new \DateTimeImmutable());
        $headers->addHeader('message-id', 'id@id');

        $this->assertInstanceOf(MailboxListHeader::class$headers->get('from'));
        $this->assertEquals([new Address('from@example.com')]$headers->get('from')->getBody());

        $this->assertInstanceOf(MailboxListHeader::class$headers->get('reply-to'));
        $this->assertEquals([new Address('reply@example.com')]$headers->get('reply-to')->getBody());

        
// Do like curl and browsers: turn POST to GET on 301, 302 and 303                 if ('POST' === $response->getRequest()->getMethod() || 303 === $status) {
                    $info['http_method'] = 'HEAD' === $response->getRequest()->getMethod() ? 'HEAD' : 'GET';
                    $request->setMethod($info['http_method']);
                }
            } else {
                $request->setBody(AmpBody::rewind($response->getRequest()->getBody()));
            }

            foreach ($originRequest->getRawHeaders() as [$name$value]) {
                $request->addHeader($name$value);
            }

            if ($request->getUri()->getAuthority() !== $originRequest->getUri()->getAuthority()) {
                $request->removeHeader('authorization');
                $request->removeHeader('cookie');
                $request->removeHeader('host');
            }

            yield $pause;

            $response = yield $multi->request($options$request$canceller->getToken()$info$onProgress$handle);
            
public function testSymfonySerialize()
    {
        // we don't add from/sender to check that it's not needed to serialize an email         $body = new MixedPart(
            new AlternativePart(
                new TextPart('Text content'),
                new TextPart('HTML content', 'utf-8', 'html')
            ),
            new DataPart('text data', 'text.txt')
        );
        $body->getHeaders()->addHeader('foo', 'bar');
        $e = new Message((new Headers())->addMailboxListHeader('To', ['you@example.com'])$body);
        $expected = clone $e;

        $expectedJson = <<<EOF { "headers": { "to": [ { "addresses": [ { "address": "you@example.com", "name": "" } ], "name": "To", "lineLength": 76, "lang": null, "charset": "utf-8" } ] }, "body": { "boundary": null, "parts": [ { "boundary": null, "parts": [ { "body": "Text content", "charset": "utf-8", "subtype": "plain", "disposition": null, "name": null, "encoding": "quoted-printable",%A "headers": [], "class": "Symfony\\\\Component\\\\Mime\\\\Part\\\TextPart" }, { "body": "HTML content", "charset": "utf-8", "subtype": "html", "disposition": null, "name": null, "encoding": "quoted-printable",%A "headers": [], "class": "Symfony\\\\Component\\\\Mime\\\\Part\\\\TextPart" } ], "headers": [], "class": "Symfony\\\\Component\\\\Mime\\\\Part\\\\Multipart\\\\AlternativePart" }, { "filename": "text.txt", "mediaType": "application",%A "body": "text data", "charset": null, "subtype": "octet-stream", "disposition": "attachment", "name": "text.txt", "encoding": "base64",%A "headers": [], "class": "Symfony\\\\Component\\\\Mime\\\\Part\\\\DataPart" } ], "headers": { "foo": [ { "value": "bar", "name": "foo", "lineLength": 76, "lang": null, "charset": "utf-8" } ] }, "class": "Symfony\\\\Component\\\\Mime\\\\Part\\\\Multipart\\\\MixedPart" }, "message": null }
unset($message['headers']['Return-Path']);
      }
    }

    $headers = new Headers();
    foreach ($message['headers'] as $name => $value) {
      if (in_array(strtolower($name), self::MAILBOX_LIST_HEADERS, TRUE)) {
        // Split values by comma, but ignore commas encapsulated in double         // quotes.         $value = str_getcsv($value, ',');
      }
      $headers->addHeader($name$value);
    }
    $line_endings = Settings::get('mail_line_endings', PHP_EOL);
    // Prepare mail commands.     $mail_subject = (new UnstructuredHeader('subject', $message['subject']))->getBodyAsString();
    // Note: email uses CRLF for line-endings. PHP's API requires LF     // on Unix and CRLF on Windows. Drupal automatically guesses the     // line-ending format appropriate for your system. If you need to     // override this, adjust $settings['mail_line_endings'] in settings.php.     $mail_body = preg_replace('@\r?\n@', $line_endings$message['body']);
    $mail_headers = $headers->toString();

    
if ($options['http_version']) {
            $request->setProtocolVersions(match ((float) $options['http_version']) {
                1.0 => ['1.0'],
                1.1 => $request->setProtocolVersions(['1.1', '1.0']),
                default => ['2', '1.1', '1.0'],
            });
        }

        foreach ($options['headers'] as $v) {
            $h = explode(': ', $v, 2);
            $request->addHeader($h[0]$h[1]);
        }

        $request->setTcpConnectTimeout(1000 * $options['timeout']);
        $request->setTlsHandshakeTimeout(1000 * $options['timeout']);
        $request->setTransferTimeout(1000 * $options['max_duration']);
        if (method_exists($request, 'setInactivityTimeout')) {
            $request->setInactivityTimeout(0);
        }

        if ('' !== $request->getUri()->getUserInfo() && !$request->hasHeader('authorization')) {
            $auth = explode(':', $request->getUri()->getUserInfo(), 2);
            
Home | Imprint | This part of the site doesn't use cookies.