codememory/mailer

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

This package is auto-updated.

Last update: 2024-09-29 05:49:58 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
  • 合并所有配置
    • php vendor/bin/gc-cdm g-config:merge --all

在文件夹 configs 中创建配置文件 mail.yaml。可以使用全局配置 .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