Smtp
電子郵件是—種用電子手段提供信息交換的通信方式,是互聯(lián)網(wǎng)應(yīng)用最廣的服務(wù)。電子郵件幾乎是每個web應(yīng)用程序不可或缺的,無論是時事通訊還是訂單確認(rèn)。本庫采用swoole協(xié)程客戶端實現(xiàn)了電子郵件的發(fā)送。
組件要求
- php: >=7.1.0
- ext-swoole: ^4.2.6
- easyswoole/spl: ^1.1
- easyswoole/utility: ^1.0
安裝方法
composer require easyswoole/smtp
倉庫地址
基本使用
郵件配置
set
設(shè)置服務(wù)器地址
public function setServer(string $server): void
設(shè)置服務(wù)器端口
public function setPort(int $port): void
設(shè)置ssl
public function setSsl(bool $ssl): void
設(shè)置用戶名
public function setUsername(string $username): void
設(shè)置密碼
public function setPassword(string $password): void
設(shè)置郵件發(fā)送方
public function setMailFrom(string $mailFrom): void
設(shè)置超時時間
public function setTimeout(float $timeout): void
設(shè)置郵件大小
public function setMaxPackage(int $maxPackage)
get
獲取服務(wù)地址
public function getServer(): string
獲取服務(wù)端口
public function getPort(): int
是否設(shè)置了ssl
public function isSsl(): bool
獲取用戶名
public function getUsername(): string
獲取密碼
public function getPassword(): string
獲取郵件發(fā)送方
public function getMailFrom(): string
獲取超時時間
public function getTimeout(): float
獲取郵件大小
public function getMaxPackage()
內(nèi)容配置
set
設(shè)置協(xié)議版本
public function setMimeVersion($mimeVersion): void
設(shè)置contentType
public function setContentType($contentType): void
設(shè)置字符
public function setCharset($charset): void
設(shè)置編碼
public function setContentTransferEncoding($contentTransferEncoding): void
設(shè)置主題
public function setSubject($subject): void
設(shè)置郵件內(nèi)容
public function setBody($body): void
添加附件
public function addAttachment($attachment)
get
獲取協(xié)議版本
public function getMimeVersion()
獲取contenttype
public function getContentType()
獲取字符
public function getCharset()
獲取編碼
public function getContentTransferEncoding()
獲取主題
public function getSubject()
獲取郵件內(nèi)容
public function getBody()
獲取附件
public function getAttachments()
使用示例
use EasySwoole\Smtp\Mailer;
use EasySwoole\Smtp\MailerConfig;
use EasySwoole\Smtp\Message\Html;
use EasySwoole\Smtp\Message\Attach;
// 必須用go
go(function (){
$config = new MailerConfig();
$config->setServer('smtp.163.com');
$config->setSsl(false);
$config->setUsername('huizhang');
$config->setPassword('*******');
$config->setMailFrom('xx@163.com');
$config->setTimeout(10);//設(shè)置客戶端連接超時時間
$config->setMaxPackage(1024*1024*5);//設(shè)置包發(fā)送的大小:5M
//設(shè)置文本或者h(yuǎn)tml格式
$mimeBean = new Html();
$mimeBean->setSubject('Hello Word!');
$mimeBean->setBody('<h1>Hello Word</h1>');
//添加附件
$mimeBean->addAttachment(Attach::create('./test.txt'));
$mailer = new Mailer($config);
$mailer->sendTo('xx@qq.com', $mimeBean);
});
進階使用
郵件內(nèi)容支持文本和html兩種類型
文本
示例
$mimeBean = new \EasySwoole\Smtp\Message\Text();
$mimeBean->setSubject('Hello Word!');
$mimeBean->setBody('<h1>Hello Word</h1>');
效果
Html
$mimeBean = new \EasySwoole\Smtp\Message\Html();
$mimeBean->setSubject('Hello Word!');
$mimeBean->setBody('<h1>Hello Word</h1>');
效果
附件
$mimeBean = new \EasySwoole\Smtp\Message\Text();
//$mimeBean = new \EasySwoole\Smtp\Message\Html();
...
// 創(chuàng)建附件
$createAttachment = Attach::create('./test.txt');
// 添加附件
$mimeBean->addAttachment($createAttachment);
...