codememory/mailer

v1.0 2021-09-03 17:45 UTC

This package is auto-updated.

Last update: 2024-04-29 04:40:59 UTC


README

Mailer - Отправка сообщений на почты

Установка

composer require codememory/mailer

Добавления серверов

<?php

use Codememory\Components\Mail\Workers\ServerWorker;
use Codememory\Components\Mail\Interfaces\ServerConfigurationInterface;

require_once 'vendor/autoload.php';

$serverWorker = new ServerWorker();

$serverWorker
    ->add('server-name', function (ServerConfigurationInterface $configuration) {
        $configuration
            ->setHost('smtp.gmail.com')
            ->setPort(487)
            ->setMimeTypeBody('text/html')
            ->setCharset('utf8');
    });

Добавления пользователей

<?php

use Codememory\Components\Mail\Workers\UserWorker;
use Codememory\Components\Mail\Interfaces\UserConfigurationInterface;

require_once 'vendor/autoload.php';

$userWorker = new UserWorker();

$userWorker
    ->addUser('user-alias', function (UserConfigurationInterface $configuration) {
        $configuration
            ->setUsername('example@gmail.com')
            ->setPassword('user password')
            ->setServer($serverWorker->getServer('server-name')) // Сервер, добавленный ранее
            ->addFrom('example@gmail.com', 'Example')
    });

Отправка сообщений

<?php

use Codememory\Components\Mail\Mailer;
use Codememory\Components\Mail\Interfaces\MessageInterface;

// В качестве параметра, передать добавленного пользователя
$mailer = new Mailer($userWorker->getUser('user-alias'));

$mailer
    ->createMessage(function (MessageInterface $message) {
        $message
            ->setSubject('Тема #1')
            ->setBody('Контент темы #1')
            ->addRecipientAddress('email', 'user<необязательно>')
            ->attach('images/image.png');
    })
    ->createMessage(function (MessageInterface $message) {
        $message
            ->setSubject('Тема #2')
            ->setBody('Контент темы #2')
            ->addRecipientAddress('email');
    });

// Отправляем все созданные сообщения
$mailer->send();

Использование MailerPack

Обязательно выполняем следующие команды

  • Создание глобальной конфигурации, если ее не существует
    • php vendor/bin/gc-cdm g-config:init
  • Merge всей конфигурации
    • php vendor/bin/gc-cdm g-config:merge --all

Создаем файл конфигурации mail.yaml в папке configs Данными именами, можно руководить с помощью глобальной конфигурации .config/.codememory.json

Обзор конфигурации

# configs/mail.yaml

mail:
  #! Servers and their settings
  servers:
    server1: 
      host: smtp.gmail.com
      port: 587
      mimeTypeBody: text/html
      encryption: tls
  #! Users and their settings
  users:
    user1: 
      username: "example@gmail.com"
      password: "password"
      server: "server1"
      from:
        - {
            email: "example@gmail.com",
            name: "Example"
        }
  #! The default user to use for authentication and sending messages to recipients
  activeUser: user1

Пример использования MailerPack

<?php

use Codememory\Components\Mail\MailerPack;

require_once 'vendor/autoload.php';

$mailerPack = new MailerPack($serverWorker, $userWorker);

$mailer = $mailerPack->getMailer(); // Returned: Codememory\Components\Mail\Mailer