PHP code example of gryfoss / twig-smart-mailer

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

    

gryfoss / twig-smart-mailer example snippets




declare(strict_types=1);

use GryfOSS\Mailer\Attachment;
use GryfOSS\Mailer\EmailAddress;
use GryfOSS\Mailer\Message;
use GryfOSS\Mailer\SmartMailer;
use GryfOSS\Mailer\Dsn\Smtp;

ge = new Message();
$message->setFrom(new EmailAddress('[email protected]', 'Sender Name'))
    ->addTo(new EmailAddress('[email protected]', 'Recipient Name'))
    ->setSubject('Test Email')
    ->setHtml('<h1>Hello {{ name }}!</h1><img src="cid:logo.png" alt="Logo"/>')
    ->setText('Hello {{ name }}!')
    ->setContext(['name' => 'World']);

// Add attachments and images
$attachment = new Attachment('/path/to/file.pdf', 'Document.pdf');
$message->addAttachment($attachment);

$logo = new Attachment('/path/to/logo.png', 'logo.png');
$message->addImage($logo);

// Send email
$mailer = new SmartMailer($dsn);
$mailer->send($message);

use GryfOSS\Mailer\Dsn\Gmail;

$gmail = new Gmail();
$gmail->setUsername('[email protected]')
      ->setPassword('your-app-password');

$mailer = new SmartMailer($gmail);

$image = new Attachment('/path/to/image.png');
$image->setName('custom-name');
$message->addImage($image);

// In HTML: <img src="cid:custom-name" alt="Custom Image"/>

$message->setHtml('
    <h1>Welcome {{ user.name }}!</h1>
    <p>You have {{ notifications|length }} new notifications.</p>
    {% for notification in notifications %}
        <p>{{ notification.message }}</p>
    {% endfor %}
')
->setContext([
    'user' => ['name' => 'John Doe'],
    'notifications' => [
        ['message' => 'New message received'],
        ['message' => 'Profile updated']
    ]
]);

use GryfOSS\Mailer\FakeFileSmartMailer;

$mailer = new FakeFileSmartMailer('/tmp/test-email.json');
$mailer->send($message);

// Email data will be written to /tmp/test-email.json as JSON

use GryfOSS\Mailer\Exception\SmartMailerException;

try {
    $mailer->send($message);
} catch (SmartMailerException $e) {
    // Handle any SmartMailer-specific error
    echo "Email error: " . $e->getMessage();
}