pe/component-smtp

v1.0.0 2019-05-23 08:12 UTC

This package is auto-updated.

Last update: 2024-08-23 20:45:42 UTC


README

以下版本的PHP受到支持。

  • PHP 7.1+

安装

要安装,请使用composer

php composer.phar require pe/component-smtp

最小使用

<?php

namespace PE\Component\SMTP;

$host     = 'example.com';// Your server host
$port     = 587;          // Your server port
$security = true;         // Enable/disable server ssl security
$validate = false;        // Enable/disable certificate verification

// Create client
$client = new Client(new Connection($host, $port, $security, $validate));

// Send message minimal flow
$client->connect();
$client->HELO();
$client->MAIL('sender@domain.com');
$client->RCPT('recipient@domain.xyz');

$message = "
From: <sender@domain.com>\n
To: <recipient@domain.xyz>\n
Subject: HELLO\n
Content-Type: text/plain\n
\n
HELLO
";

$client->DATA($message);
$client->QUIT();

捆绑的E-SMTP扩展

StartTLS

<?php
namespace PE\Component\SMTP;

// For enable module you need to disable connection security and use one of options
$client = new Client(new Connection('example.com', 587, false));

// Option 1: upgrade connection if TLS supported (default)
$client->attachModule(new Module\ModuleStartTLS());

// Option 2: force TLS, if not supported - exception will be thrown
$client->attachModule(new Module\ModuleStartTLS(true));

身份验证

<?php
namespace PE\Component\SMTP;

$client = new Client(new Connection());

// For enable module first you need to configure authenticators
$module = new Module\ModuleAuthenticator([
    new Authenticator\AuthenticatorPlain(),
    new Authenticator\AuthenticatorLogin(),
    new Authenticator\AuthenticatorCramMD5(),
]);

// Then you must set your credentials
$module->setUsername('username');
$module->setPassword('password');

// And then you can attach module
$client->attachModule($module);

日志记录器

<?php
namespace PE\Component\SMTP;

$client = new Client(new Connection());

// Option 1: use STDOUT handler
$client->attachModule(
    new Module\ModuleLogger(new LogHandler\LogHandlerSTDOUT())
);

// Option 2: use PSR handler
/* @var $psrLogger \Psr\Log\LoggerInterface */
$client->attachModule(
    new Module\ModuleLogger(new LogHandler\LogHandlerPSR($psrLogger))
);

投递状态通知

允许配置投递状态通知报告

警告:SMTP服务器可能会忽略一些选项

<?php
namespace PE\Component\SMTP;

use PE\Component\SMTP\Module\ModuleDSN;

$module = new ModuleDSN();

// For enable you can use any combination of constants below
$module->setNotify([
    ModuleDSN::NOTIFY_SUCCESS,
    ModuleDSN::NOTIFY_DELAY,
    ModuleDSN::NOTIFY_FAILURE,
]);

// For disable you can pass only or just empty array
$module->setNotify([ModuleDSN::NOTIFY_NEVER]);

// For return only headers in delivery report
$module->setReturn(ModuleDSN::RETURN_HEADERS);

// For return full body in delivery report
$module->setReturn(ModuleDSN::RETURN_FULL);

$client = new Client(new Connection());
$client->attachModule($module);

管道支持

如果远程服务器支持管道 - 您的命令将以组的形式发送,并减少读取响应次数

<?php
namespace PE\Component\SMTP;

// For enable module just attach it
$client = new Client(new Connection());
$client->attachModule(new Module\ModulePipelining());

SMTP UTF8支持

为了使用此模块,所有消息地址都必须以UTF8编码

<?php
namespace PE\Component\SMTP;

// For enable module just attach it
$client = new Client(new Connection());
$client->attachModule(new Module\ModuleSMTPUTF8());

8位MIME支持

为了使用此模块,消息体必须以8位模式编码

<?php
namespace PE\Component\SMTP;

// For enable module just attach it
$client = new Client(new Connection());
$client->attachModule(new Module\Module8BitMIME());