wyzen-packages/mailer-factory

Simple mailer factory

v3.0.0 2023-12-04 16:32 UTC

This package is auto-updated.

Last update: 2024-11-04 18:19:11 UTC


README

Mailer factory

Changelog

- 3.0.0 : upgrade to symfony/mailjet-mailer >= 6.4. !!! changement de port de 465 à 587 par défaut
  - Ajout de la prise en compte de MAILERFACTORY_PORT pour remettre le port à 465
- 2.1.0 : Add attachFromContent & embedFromContent methods
- 2.0.0 : php 8.2 mini
- 1.0.5 : Add 'name' => 'name@email.com' format
- 1.0.4 : Fix bugs

Tests

  • Require : php 8.2 minimum
  • use docker/composer.sh to use the correct release of php.

Import

composer require wyzen-packages/mailer-factory

Usage

Env variables

Common

MAILERFACTORY_FROM="Support<support@email.com>"

With DSN

MAILERFACTORY_DSN=smtp://user:pass@host:port?verify_peer=1
  • verify_peer:
    • 1 (default) => TLS
    • 0 => NO TLS

With explicit var

MAILERFACTORY_PROVIDER=smtp | mailjet
MAILERFACTORY_HOST=host
MAILERFACTORY_TLS=true
MAILERFACTORY_PORT=1025
MAILERFACTORY_USER=user | api_public_key
MAILERFACTORY_PASSWORD=password | api_private_key

emails Format without duplicate mail

All mails are uniq

$to = [
        'vde<vde@email.com>',
        'user1@email.com, user2@email.com  ; user3<user3@email.com>',
        'other@email.com',
        'duplicate<other@email.com>',
        'user5 name' => 'user5@email.com',
    ];

// OR
$to = 'user1@email.com, user2@email.com  ; user3<user3@email.com>';

// Transform emails to array of Address
$tb_emails = $mailer::fixEmails($to);

Send Mail

$mailer = MailerFactory::getMailer($this->smtp);
$to = 'user1@email.com, user2@email.com  ; user3<user3@email.com>';
$cc = [
    'user1@email.com, user2@email.com  ; user3<user3@email.com>',
    'user4<user4@email.com>',
]
$mailer->setTo($to);
$mailer->setCc($to);
// $mailer->setBcc($to);
$mailer->setReplyTo('REPLY<reply@email.com>');

$mailer->setSubject('Subject message');
$mailer->setText('Only text format');
$mailer->setHtml('<h1>HTML format mail</h1>');
try{
    $mailer->sendMessage();
}
catch (TransportExceptionInterface $ex){

}

Send Mail with attach

$mailer = MailerFactory::getMailer($this->smtp);
$to = 'user1@email.com';
$mailer->setTo($to);
$mailer->setReplyTo('REPLY<reply@email.com>');
$mailer->attach(__DIR__ . '/../images/approved.png', 'Image Approved');

$mailer->setSubject('Subject message');
$mailer->setText('Only text format');
$mailer->setHtml('<h1>HTML format mail</h1>');
try{
    $mailer->sendMessage();
}
catch (TransportExceptionInterface $ex){

}

Send Mail with embed image

$mailer = MailerFactory::getMailer($this->smtp);
$to = 'user1@email.com';
$mailer->setTo($to);
$mailer->setReplyTo('REPLY<reply@email.com>');
$mailer->embedImage(__DIR__ . '/../images/suricate.jpg', 'embedimage');

$mailer->setSubject('Subject message');
$mailer->setText('Only text format');
$mailer->setHtml('<h1>HTML format mail</h1><img src="cid:embedimage">');
try{
    $mailer->sendMessage();
}
catch (TransportExceptionInterface $ex){

}