function PhpMailTest::testMail

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/Mail/Plugin/Mail/PhpMailTest.php \Drupal\Tests\Core\Mail\Plugin\Mail\PhpMailTest::testMail()
  2. 10 core/tests/Drupal/Tests/Core/Mail/Plugin/Mail/PhpMailTest.php \Drupal\Tests\Core\Mail\Plugin\Mail\PhpMailTest::testMail()

Tests sending a mail using a From address with a comma in it.

@covers ::mail

File

core/tests/Drupal/Tests/Core/Mail/Plugin/Mail/PhpMailTest.php, line 112

Class

PhpMailTest
@coversDefaultClass <a href="/api/drupal/core%21lib%21Drupal%21Core%21Mail%21Plugin%21Mail%21PhpMail.php/class/PhpMail/11.x" title="Defines the default Drupal mail backend, using PHP&#039;s native mail() function." class="local">\Drupal\Core\Mail\Plugin\Mail\PhpMail</a> @group Mail

Namespace

Drupal\Tests\Core\Mail\Plugin\Mail

Code

public function testMail() : void {
    // Setup a mail message.
    $message = [
        'id' => 'example_key',
        'module' => 'example',
        'key' => 'key',
        'to' => 'to@example.org',
        'from' => 'from@example.org',
        'reply-to' => 'from@example.org',
        'langcode' => 'en',
        'params' => [],
        'send' => TRUE,
        'subject' => "test\r\nsubject",
        'body' => '',
        'headers' => [
            'MIME-Version' => '1.0',
            'Content-Type' => 'text/plain; charset=UTF-8; format=flowed; delsp=yes',
            'Content-Transfer-Encoding' => '8Bit',
            'X-Mailer' => 'Drupal',
            'From' => '"Foo, Bar, and Baz" <from@example.org>',
            'Reply-to' => 'from@example.org',
            'Return-Path' => 'from@example.org',
        ],
    ];
    $mailer = $this->createPhpMailInstance();
    // Verify we use line endings consistent with the PHP mail() function, which
    // changed with PHP 8. See:
    // - https://www.drupal.org/node/3270647
    // - https://bugs.php.net/bug.php?id=81158
    $line_end = "\r\n";
    $expected_headers = "MIME-Version: 1.0{$line_end}";
    $expected_headers .= "Content-Type: text/plain; charset=UTF-8; format=flowed; delsp=yes{$line_end}";
    $expected_headers .= "Content-Transfer-Encoding: 8Bit{$line_end}";
    $expected_headers .= "X-Mailer: Drupal{$line_end}";
    $expected_headers .= "From: \"Foo, Bar, and Baz\" <from@example.org>{$line_end}";
    $expected_headers .= "Reply-to: from@example.org{$line_end}";
    $mailer->expects($this->once())
        ->method('doMail')
        ->with($this->equalTo('to@example.org'), $this->equalTo("=?utf-8?Q?test?={$line_end} =?utf-8?Q?subject?="), $this->equalTo(''), $this->stringStartsWith($expected_headers))
        ->willReturn(TRUE);
    $this->assertTrue($mailer->mail($message));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.