praetoriantechnology/smart-mailer

基于 Symfony Mailer 的智能邮件发送工具,支持动态源邮箱和邮件内文件轻松集成

0.2 2023-06-05 14:17 UTC

This package is auto-updated.

Last update: 2024-08-27 09:24:05 UTC


README

Tests status GitHub tag (latest SemVer)

智能邮件发送库是一个简单的库,它通过使用 symfony/mailer 组件来辅助发送电子邮件。目前它需要 SMTP 连接,但提供了如图像轻松嵌入、附件添加简化、异常跟踪以及使用字符串模板而不是文件等特性。

示例用法

<?php

declare(strict_types=1);

use Praetorian\SmartMailer\Attachment;
use Praetorian\SmartMailer\EmailAddress;
use Praetorian\SmartMailer\Message;
use Praetorian\SmartMailer\SmartMailer;
use Praetorian\SmartMailer\Smtp;

include 'vendor/autoload.php';

$from = new EmailAddress('jaroslaw@kaczynski.pl', 'Jaroslaw Kaczynski');
$to = new EmailAddress('andrzej@duda.pl', 'Andrzej Duda');

$smtp = new Smtp();
$smtp->setHost('smtp.sample.com')
    ->setPort(465)
    ->setUsername('username')
    ->setPassword('password')
     ;

$image1 = new Attachment();
$image1->setPath('sample.png')
       ;

$message = new Message();
$message->setFrom($from)
    ->addTo($to)
    ->setHtml('<html><head><title>test</title></head><body>{{ variable }} <img src="cid:sample.png" alt="test"/></body></html>')
    ->addImage($image1)
    ->setSubject('sample mail title')
    ->setContext([
        'variable' => 'sample text',
    ])
        ;

$mailer = new SmartMailer($smtp);

$mailer->send($message);

您可以通过使用资源的 content id 来轻松嵌入任何图像。如果您的文件名是 sample.png,那么要嵌入,只需在内容中放置 cid:sample.png。您还可以定义一个自定义名称

$image1->setName('othername')

然后替换将变为 cid:othername

警告:名称必须唯一!

路线图

1.0:测试,评论 2.0:除了 SMTP 服务器之外的其他传输 3.0:支持 Pub/Sub 队列

贡献

欢迎提交任何拉取请求或报告问题。

许可证

GPL 3+