PHP code example of nimblephp / email

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

    

nimblephp / email example snippets


use NimblePHP\Email\Email;

// Użycie domyślnej konfiguracji (z zmiennych środowiskowych)
$email = new Email();
$email->to('[email protected]')
      ->from('[email protected]', 'Jan Kowalski')
      ->subject('Ważna wiadomość')
      ->body('Treść wiadomości', false)
      ->send();

$email = new Email();
$email->to('[email protected]')
      ->from('[email protected]', 'Jan Kowalski')
      ->subject('Ważna wiadomość')
      ->body('<h1>Witaj!</h1><p>To jest wiadomość HTML.</p>', true)
      ->attachment('/sciezka/do/pliku.pdf', 'dokument.pdf')
      ->send();

$email = new Email();
$email->to('[email protected]')
      ->from('[email protected]')
      ->subject('Ważna wiadomość')
      ->body('Treść wiadomości')
      ->cc('[email protected]')
      ->bcc('[email protected]')
      ->send();

$email = new Email();
$email->from('[email protected]')
      ->subject('Ważna wiadomość grupowa')
      ->body('Treść wiadomości')
      ->addRecipients([
          '[email protected]' => 'Jan Kowalski',
          '[email protected]' => 'Anna Nowak',
          '[email protected]'
      ])
      ->addCc([
          '[email protected]' => 'Piotr Wiśniewski',
          '[email protected]'
      ])
      ->send();

$email = new Email();
$email->to('[email protected]')
      ->from('[email protected]')
      ->subject('Witaj w naszym serwisie')
      ->template('/sciezka/do/szablonu.html', [
          'imie' => 'Jan',
          'data' => date('Y-m-d'),
          'link_aktywacyjny' => 'https://example.com/activate?token=123'
      ])
      ->send();

$templateContent = '<h1>Witaj {{imie}}!</h1><p>Dziękujemy za rejestrację w dniu {{data}}.</p>';

$email = new Email();
$email->to('[email protected]')
      ->from('[email protected]')
      ->subject('Witaj w naszym serwisie')
      ->templateFromString($templateContent, [
          'imie' => 'Jan',
          'data' => date('Y-m-d')
      ])
      ->send();

$email = new Email();
$email->to('[email protected]')
      ->from('[email protected]')
      ->subject('Wiadomość z obrazkiem')
      ->body('<h1>Witaj!</h1><p>Oto nasze logo:</p><img src="cid:logo" alt="Logo">', true)
      ->embedImage('/sciezka/do/logo.png', 'logo')
      ->send();

$pdf = generuj_pdf(); // Funkcja generująca zawartość PDF

$email = new Email();
$email->to('[email protected]')
      ->from('[email protected]')
      ->subject('Raport miesięczny')
      ->body('W załączniku znajduje się raport za bieżący miesiąc.')
      ->attachmentFromString($pdf, 'raport.pdf', 'application/pdf')
      ->send();

$email = new Email();
$email->to('[email protected]')
      ->from('[email protected]')
      ->subject('Ważna wiadomość')
      ->body('Treść wiadomości')
      ->addHeader('X-Priority', '1')
      ->addHeader('X-Mailer', 'NimblePHP')
      ->send();

$email = new Email();
$email->to('[email protected]')
      ->from('[email protected]', 'Twoje Imię')
      ->subject('Ważna wiadomość')
      ->body('Treść wiadomości')
      ->setOAuthToken('twoj_token_oauth2')
      ->send();

$email = new Email();
$email->to('[email protected]')
      ->from('[email protected]')
      ->subject('Ważna wiadomość')
      ->body('Treść wiadomości')
      ->setConnectionTimeout(60)  // 60 sekund na nawiązanie połączenia
      ->setTimeout(120)          // 120 sekund na operacje
      ->send();

use NimblePHP\Email\Email;
use NimblePHP\Email\Config\EmailConfig;

// Utworzenie własnej konfiguracji
$config = new EmailConfig();

// Ustawienie konfiguracji bezpośrednio
$config->setConfig([
    'host' => 'moj-serwer-smtp.com',
    'port' => 465,
    'username' => 'uzytkownik',
    'password' => 'haslo',
    'auth' => true,
    'secure' => 'ssl'
]);

$email = new Email($config);
$email->to('[email protected]')
      ->subject('Testowa wiadomość')
      ->body('To jest wiadomość testowa')
      ->send();

use NimblePHP\Email\Email;
use NimblePHP\Email\Config\EmailConfig;
use NimblePHP\Email\Transport\SmtpTransport;

// Utworzenie własnej konfiguracji i transportu
$config = new EmailConfig();
$transport = new SmtpTransport($config);
$transport->setConnectionTimeout(60);

$email = new Email($config, $transport);
$email->to('[email protected]')
      ->subject('Wiadomość przez niestandardowy transport')
      ->body('Treść wiadomości')
      ->send();

use NimblePHP\Email\Email;
use NimblePHP\Email\Template\TemplateProcessor;

// Użycie z niestandardowym procesorem szablonów
$templateProcessor = new TemplateProcessor();
$templateProcessor->setPlaceholderFormat('${%s}'); // zmiana formatu placeholderów na ${nazwa}

$email = new Email(null, null, $templateProcessor);
$email->to('[email protected]')
      ->from('[email protected]')
      ->subject('Niestandardowy format szablonu')
      ->templateFromString('<p>Witaj ${imie}!</p>', ['imie' => 'Jan'])
      ->send();

use NimblePHP\Email\Email;
use NimblePHP\Email\Config\EmailConfig;
use NimblePHP\Email\Transport\SmtpTransport;
use NimblePHP\Email\Template\TemplateProcessor;

// Pełna konfiguracja z wszystkimi komponentami
$config = new EmailConfig();
$config->setConfig([
    'host' => 'smtp.example.com',
    'port' => 587,
    'username' => 'user',
    'password' => 'pass',
    'auth' => true,
    'secure' => 'tls'
]);

$transport = new SmtpTransport($config);
$transport->setConnectionTimeout(30);
$transport->setTimeout(60);

$templateProcessor = new TemplateProcessor();

$email = new Email($config, $transport, $templateProcessor);
$email->to('[email protected]', 'Jan Kowalski')
      ->from('[email protected]', 'System Powiadomień')
      ->addCc([
          '[email protected]' => 'Osoba 1',
          '[email protected]' => 'Osoba 2'
      ])
      ->addBcc(['[email protected]'])
      ->subject('Raport miesięczny')
      ->addHeader('X-Priority', '1')
      ->templateFromString('<h1>Raport za {{miesiac}}</h1><p>Znajdziesz go w załączniku.</p>', [
          'miesiac' => 'kwiecień 2025'
      ], true)
      ->attachment('/sciezka/do/raportu.pdf')
      ->embedImage('/sciezka/do/logo.png', 'logo_id')
      ->send();

src/
├── Config/
│   └── EmailConfig.php
├── Transport/
│   ├── TransportInterface.php
│   ├── SmtpTransport.php
│   └── PhpMailTransport.php
├── Template/
│   └── TemplateProcessor.php
├── Exception/
│   └── EmailException.php
└── Email.php