simplon/email

邮件构建库,利用 SwiftMailer

0.2.2 2015-09-01 09:55 UTC

This package is auto-updated.

Last update: 2024-09-21 00:09:14 UTC


README

     _                 _                                    _ _
 ___(_)_ __ ___  _ __ | | ___  _ __     ___ _ __ ___   __ _(_) |
/ __| | '_ ` _ \| '_ \| |/ _ \| '_ \   / _ \ '_ ` _ \ / _` | | |
\__ \ | | | | | | |_) | | (_) | | | | |  __/ | | | | | (_| | | |
|___/_|_| |_| |_| .__/|_|\___/|_| |_|  \___|_| |_| |_|\__,_|_|_|
                |_|                                             

Simplon Email

此库通过利用 SwiftMailer 来帮助构建电子邮件。

它允许开发者以纯文本/HTML版本发送电子邮件,并自动检测/内联注入图片。它强制使用模板抽象并提供数据注入。

设置

由于这是一个 composer 包,您只需在您的 composer 包定义中要求它,并安装/更新它即可

{
     "require": {
        "simplon/email": "*"
     }
}

如果您不知道什么是 composer,您应该查看 Composer 的网页

EmailTransportVo

此类存储我们选择的电子邮件传输实例,以便发送电子邮件。

// php's internal mail
$emailTransportVo = (new \Simplon\Email\Vo\EmailTransportVo())->setTransportInstance(
	\Swift_MailTransport::newInstance()
);

// or via smtp transport
$emailTransportVo = (new \Simplon\Email\Vo\EmailTransportVo())->setTransportInstance(
	\Swift_SmtpTransport::newInstance('localhost', 25)
		->setUsername('foo')
		->setPassword('bar')
);

电子邮件设置

简介

我们使用构建器模式类来定义我们的电子邮件。这非常简单,并有助于我们控制我们的数据。

构建电子邮件分为两个简单步骤。第一步是使用模板帮助我们定义电子邮件内容。第二步定义发送者/接收者数据以及主题。

模板

所有需要的只是告诉类在哪里找到特定电子邮件的模板。模板分为两种不同的类型

基础模板

  • 不是必需的
  • 需要命名为 "base.plain" \ "base.html"

示例

HEADER

#################################################

A base-template is the basis to all content-templates.
Content will replace the following placeholder:

{{content}}

#################################################

FOOTER

-------------------------------------------------

A base-template can read all injected
content variables, too. See below:

Today is {{date}}

内容模板

  • 必需的
  • 需要命名为 "content.plain" \ "content.html"
Oh herro!

This is a content template which can receive
content data such as the recipients name.

Be nice, {{name}}!

Thanks
Tino

PS: {{name}}'s age is {{age}}

注意:我们强制使用这些名称,只是为了使设置电子邮件尽可能简单快捷。

这是我们测试模板文件夹的快照,其中包含所有数据

文件夹结构

|-templates
|---base
|---content
|-----tmpl01
|-----tmpl02
|-----tmpl03

基础模板

drwxr-xr-x  4 fightbulc  staff  136  7 Jul 15:52 .
drwxr-xr-x  5 fightbulc  staff  170  7 Jul 15:52 ..
-rw-r--r--  1 fightbulc  staff  498  7 Jul 13:12 base.html
-rw-r--r--  1 fightbulc  staff  377  7 Jul 13:27 base.plain

内容模板(例如多部分电子邮件)

drwxr-xr-x  5 fightbulc  staff     170  7 Jul 15:52 .
drwxr-xr-x  6 fightbulc  staff     204  7 Jul 15:52 ..
-rw-r--r--@ 1 fightbulc  staff  170870  6 Jul 13:54 boat.jpg
-rw-r--r--  1 fightbulc  staff     332  7 Jul 13:29 content.html
-rw-r--r--  1 fightbulc  staff     166 30 Jun 15:54 content.plain

创建电子邮件

好的,让我们创建我们的第一个电子邮件。以下我们将设置所有相关数据。只需遵循示例

// set content data
$contentData = [
    'name' => 'Tino',
    'age'  => 32,
    'date' => date('r'),
];

```php
$emailVo = (new \Simplon\Email\Vo\EmailVo())
    ->setPathBaseTemplates(__DIR__ . '/templates/base') // optional
    ->setPathContentTemplates(__DIR__ . '/templates/content/tmpl01')
    ->setFrom($config['fromAddress'], $config['fromName'])
    ->setTo($config['toAddress'], $config['toName'])
    ->setSubject('Herro!')
    ->setContentData($contentData)

发送电子邮件

几乎完成了。只需传递我们的构建器类和电子邮件就准备好了 :)

$response = (new \Simplon\Email\Email($emailTransportVo))->sendEmail($emailVo);

完整示例

以下是发送电子邮件的完整示例。

require __DIR__ . '/../vendor/autoload.php';

// load test data
require __DIR__ . '/config.php';

// ##########################################

// set content data
$contentData = [
    'name' => 'Tino',
    'age'  => 32,
    'date' => date('r'),
];

// set email
$emailVo = (new \Simplon\Email\Vo\EmailVo())
    ->setPathBaseTemplates(__DIR__ . '/templates/base')
    ->setPathContentTemplates(__DIR__ . '/templates/content/tmpl01')
    ->setFrom($config['fromAddress'], $config['fromName'])
    ->setTo($config['toAddress'], $config['toName'])
    ->setSubject('Herro!')
    ->setContentData($contentData);

// ------------------------------------------

// set transport
$emailTransportVo = new \Simplon\Email\Vo\EmailTransportVo(
	\Swift_MailTransport::newInstance()
);

// ------------------------------------------

// send email
$response = (new \Simplon\Email\Email($emailTransportVo))->sendEmail($emailVo);

// BOOL to indicate if all went fine
var_dump($response);

还有其他吗?

仍然怀疑如何使用此库?请查看 test 文件夹。

许可证

Simplon\Email 可在 MIT 许可证的条款下自由分发。

版权所有 (c) 2015 Tino Ehrich (tino@bigpun.me)

凡获得此软件及其相关文档文件(“软件”)副本的任何人,无论是否收费,均可免费处理该软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件副本,并允许向获得软件的人提供这样做,前提是满足以下条件

在软件的所有副本或主要部分中应包含上述版权声明和本许可声明。

软件按“原样”提供,不提供任何明示或暗示的保证,包括但不限于对适销性、针对特定目的的适用性和非侵权的保证。在任何情况下,作者或版权所有者不对任何索赔、损害或其他责任负责,无论是否因合同、侵权或其他行为而引起,无论是由于软件或其使用或与其他软件一起使用而引起的。

Bitdeli Badge