PHP code example of utopia-php / smtp

1. Go to this page and download the library: Download utopia-php/smtp library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

utopia-php / smtp example snippets




use Utopia\SMTP\Address;
use Utopia\SMTP\Auth\Plain;
use Utopia\SMTP\Client;
use Utopia\SMTP\Message;
use Utopia\SMTP\Transport\Native;

$client = new Client(
    transport: new Native('smtp.example.com', 587),
    domain: 'app.example.com',
    authenticators: [new Plain('username', 'password')],
);

$result = $client->send(new Message(
    from: new Address('[email protected]', 'Jane Doe'),
    to: [new Address('[email protected]')],
    subject: 'Hello',
    text: 'Plain text body',
));

$client->close();

use Utopia\SMTP\Envelope;

$client->sendRaw(
    new Envelope('[email protected]', ['[email protected]']),
    "Subject: Hello\r\n\r\nBody\r\n",
);

$result = $client->send($message);

$result->accepted;      // list of addresses the server took
$result->rejected;      // address => Reply, for the ones it would not
$result->messageId;     // the queue identifier, when the server offers one
$result->isComplete();  // nothing was rejected

use Utopia\SMTP\TransactionException;

try {
    $client->send($message);
} catch (TransactionException $exception) {
    if ($exception->isTransient()) {
        // 4yz — put it back on the queue
    }
}

use Utopia\SMTP\Encryption;

new Client($transport, encryption: Encryption::StartTls);

use Utopia\SMTP\Tls;
use Utopia\SMTP\Verification;

new Native('smtp.example.com', 587, new Tls(caFile: '/etc/ssl/certs/ca.pem'));

// A private authority or a test rig: any issuer, but still the host we dialled.
new Native('smtp.internal', 587, new Tls(verify: Verification::SelfSigned));

use Utopia\SMTP\Auth\XOAuth2;

new Client($transport, authenticators: [new XOAuth2('[email protected]', $token)]);

use Utopia\SMTP\Attachment;

new Message(
    from: new Address('[email protected]'),
    to: [new Address('[email protected]')],
    subject: 'Report',
    text: 'The figures are attached.',
    html: '<p>The figures are attached. <img src="cid:logo"></p>',
    cc: [new Address('[email protected]')],
    bcc: [new Address('[email protected]')],
    attachments: [
        Attachment::fromPath('/srv/reports/q3.pdf'),
        Attachment::fromString($png, 'logo.png', 'image/png')->inline('logo'),
    ],
    headers: ['X-Mailer' => 'Utopia'],
);

if ($client->idle() > 100 && ! $client->ping()) {
    // The session was dead and has been dropped; send() will reconnect.
}

if ($client->transactions >= 100) {
    $client->close(); // Rotate a long-lived session the way a relay expects.
}

use Utopia\SMTP\Timeouts;

new Client($transport, timeouts: new Timeouts(connect: 5.0, read: 60.0, write: 30.0));
bash
composer