PHP code example of webfiori / mailer

1. Go to this page and download the library: Download webfiori/mailer 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/ */

    

webfiori / mailer example snippets


$div = $email->insert('div');
$div->addChild('p')->text('Hello!');
$div->addChild('p', ['style' => ['color' => 'red']])->text('Important message.');

$email->setMode(SendMode::TEST_STORE, ['store-path' => '/tmp/emails']);


use WebFiori\Mail\AccountOption;
use WebFiori\Mail\SMTPAccount;
use WebFiori\Mail\Email;

$smtp = new SMTPAccount([
    AccountOption::PORT => 465,
    AccountOption::SERVER_ADDRESS => 'mail.example.com',
    AccountOption::USERNAME => '[email protected]',
    AccountOption::PASSWORD => 'secret',
    AccountOption::SENDER_NAME => 'My App',
    AccountOption::SENDER_ADDRESS => '[email protected]',
    AccountOption::NAME => 'no-reply'
]);

$email = new Email($smtp);

$email->setSubject('Hello World From PHP 😀');
$email->addTo('[email protected]');

$div = $email->insert('div');
$div->addChild('p')->text('Hello World Message');
$div->addChild('p', ['style' => ['font-weight' => 'bold', 'color' => 'red']])
    ->text('This is just a test message.');

$email->send();


use WebFiori\Mail\SMTPAccount;
use WebFiori\Mail\AccountOption;

$smtp = new SMTPAccount([
    AccountOption::PORT => 465,
    AccountOption::SERVER_ADDRESS => 'mail.example.com',
    AccountOption::USERNAME => '[email protected]',
    AccountOption::PASSWORD => 'KnvcbxFYCz77',
    AccountOption::SENDER_NAME => 'Ibrahim',
    AccountOption::SENDER_ADDRESS => '[email protected]',
    AccountOption::NAME => 'no-reply'
]);

use WebFiori\Mail\Email;

$email = new Email($smtp);

$email->setSubject('Hello World From PHP 😀');

$email->addTo('[email protected]');

$div = $email->insert('div');
$div->addChild('p')->text('Hello World Message');
$div->addChild('p', [
    'style' => [
        'font-weight' => 'bold',
        'color' => 'red'
    ]
])->text('This is just a test message.');

$email->send();

$email = new Email($smtpAccount);

$email->to('[email protected]', 'Recipient Name')
      ->cc('[email protected]', 'Manager')
      ->subject('Hello from WebFiori Mailer!')
      ->priority(1)
      ->send();

use WebFiori\Mail\MicrosoftOAuthProvider;

$provider = new MicrosoftOAuthProvider(
    tenantId:     getenv('SMTP_TENANT_ID'),
    clientId:     getenv('SMTP_CLIENT_ID'),
    clientSecret: getenv('SMTP_CLIENT_SECRET')
);

$account = new SMTPAccount([
    AccountOption::SERVER_ADDRESS => 'smtp.office365.com',
    AccountOption::PORT           => 587,
    AccountOption::USERNAME       => getenv('SMTP_USERNAME'),
    AccountOption::SENDER_ADDRESS => getenv('SMTP_USERNAME'),
    AccountOption::SENDER_NAME    => 'My App',
]);
$account->setTokenProvider($provider);

use WebFiori\Mail\OAuthTokenProvider;

class MyProvider implements OAuthTokenProvider {
    public function getToken(): string {
        // fetch, cache, and return your access token
    }
}

$account->setTokenProvider(new MyProvider());

use WebFiori\Mail\SESCredentialHelper;

$region  = 'us-east-1';
$account = new SMTPAccount([
    AccountOption::SERVER_ADDRESS => SESCredentialHelper::smtpEndpoint($region),
    AccountOption::PORT           => 587,
    AccountOption::USERNAME       => getenv('AWS_ACCESS_KEY_ID'),
    AccountOption::PASSWORD       => SESCredentialHelper::deriveSmtpPassword(
                                         getenv('AWS_SECRET_ACCESS_KEY'), $region
                                     ),
    AccountOption::SENDER_ADDRESS => '[email protected]',
    AccountOption::SENDER_NAME    => 'My App',
]);

$account = new SMTPAccount([
    AccountOption::SERVER_ADDRESS  => 'mail.internal.example.com',
    AccountOption::PORT            => 587,
    AccountOption::USERNAME        => '[email protected]',
    AccountOption::PASSWORD        => 'secret',
    AccountOption::SENDER_ADDRESS  => '[email protected]',
    AccountOption::SENDER_NAME     => 'Internal Mailer',
    AccountOption::ALLOW_SELF_SIGNED => true,  // allow self-signed cert; verify_peer stays on
]);

$account = new SMTPAccount([
    // ...
    AccountOption::VERIFY_SSL => false,  // disables verify_peer and verify_peer_name
]);

$account = new SMTPAccount([
    // ...
    AccountOption::MAX_RETRIES => 5,  // retry up to 5 times
    AccountOption::RETRY_DELAY => 2,  // backoff: 2s, 4s, 8s, 16s, 32s
]);

// Disable retries entirely:
$account->setMaxRetries(0);

$email->send();
echo $email->getMessageId(); // e.g. <[email protected]>

$reply = new Email($account);
$reply->setInReplyTo($originalEmail->getMessageId());
$reply->setSubject('Re: Original Subject');
$reply->addTo('[email protected]');
$reply->send();

use webfiori\file\File;

$email->addAttachment('Attach00.txt');
$email->addAttachment(new File('another.txt'));

$email->addBeforeSend(function (Email $e) {
    $e->insert('p')->text('This text is added before sending');
});

$email->addAfterSend(function (Email $e) {
    // Do any action like storing the log.
});

foreach ($email->getLog() as $logEvent) {
    echo ' Command: '.$logEvent['command'];
    echo ' Code: '.$logEvent['response-code'];
    echo ' Message: '.$logEvent['response-message'];
}

$email->storeEmail('/path/to/email/file');

$email->setMode(SendMode::TEST_STORE, [
    'store-path' => '/path/to/store/message'
]);

$email->send(); // Stores instead of sending

$email->setMode(SendMode::TEST_SEND, [
    'send-addresses' => [
        '[email protected]',
        '[email protected]',
    ]
]);

$email->send(); // Sends to test addresses only