Smtp
介紹
電子郵件是—種用電子手段提供信息交換的通信方式,是互聯(lián)網(wǎng)應(yīng)用最廣的服務(wù)。電子郵件幾乎是每個(gè)web應(yīng)用程序不可或缺的,無論是時(shí)事通訊還是訂單確認(rèn)。本庫(kù)采用swoole協(xié)程客戶端實(shí)現(xiàn)了電子郵件的發(fā)送
安裝
composer require easyswoole/smtp 2.x
查看smtp 1.x
版本 點(diǎn)擊
用法
基礎(chǔ)配置
$mail = new \EasySwoole\Smtp\Mailer(false);
參數(shù):
$enableException
是否啟用異常 默認(rèn)false
設(shè)置超時(shí)
/** @var \EasySwoole\Smtp\Mailer $mail **/
$mail->setTimeout(5);
設(shè)置最大數(shù)據(jù)包大小
/** @var \EasySwoole\Smtp\Mailer $mail **/
$mail->setMaxPackage(1024 * 1024 * 2);
設(shè)置Host
/** @var \EasySwoole\Smtp\Mailer $mail **/
$mail->setHost("smtp.qq.com");
設(shè)置Port
/** @var \EasySwoole\Smtp\Mailer $mail **/
$mail->setPort(465);
設(shè)置Ssl
/** @var \EasySwoole\Smtp\Mailer $mail **/
$mail->setSsl(true);
設(shè)置用戶名及密碼
/** @var \EasySwoole\Smtp\Mailer $mail **/
$mail->setUsername("xxx@qq.com");
$mail->setPassword("xxxxx");
設(shè)置發(fā)件人地址
可選方法 默認(rèn)用戶名
/** @var \EasySwoole\Smtp\Mailer $mail **/
$mail->setFrom("xxx@qq.com");
參數(shù):
$address
發(fā)件人地址$name
設(shè)置昵稱 可選參數(shù)
設(shè)置收件人地址
/** @var \EasySwoole\Smtp\Mailer $mail **/
$mail->addAddress("xxx@qq.com");
參數(shù):
$address
收件人地址$name
設(shè)置昵稱 可選參數(shù)
設(shè)置回復(fù)地址
可選方法 默認(rèn)發(fā)件人地址
/** @var \EasySwoole\Smtp\Mailer $mail **/
$mail->setReplyTo("xxx@qq.com");
參數(shù):
$address
收件人地址$name
設(shè)置昵稱 可選參數(shù)
發(fā)送
發(fā)送文本
/** @var \EasySwoole\Smtp\Mailer $mail **/
$text = new \EasySwoole\Smtp\Request\Text();
$text->setSubject("Smtp Test Title");
$text->setBody("Smtp Test Body");
// 添加附件 可選
$text->addAttachment(__FILE__,'附件重命名');
// 發(fā)送
$mail->send($text);
發(fā)送Html
/** @var \EasySwoole\Smtp\Mailer $mail **/
$text = new \EasySwoole\Smtp\Request\Html();
$text->setSubject("Smtp Test Title");
$text->setBody("<h1>Smtp Test Body<h1>");
// 添加附件 可選
$text->addAttachment(__FILE__,'附件重命名');
// 發(fā)送
$mail->send($text);
注意事項(xiàng)
當(dāng)開啟異常的時(shí)候,發(fā)送過程中出現(xiàn)問題,將會(huì)拋出以下異常:
try {
/** @var \EasySwoole\Smtp\Mailer $mail **/
$mail->send($text);
}catch (\EasySwoole\Smtp\Exception\Exception $exception) {
}
當(dāng)未開啟異常的時(shí)候,發(fā)送過程中出現(xiàn)問題,將會(huì)返回:
/** @var \EasySwoole\Smtp\Mailer $mail **/
/** @var \EasySwoole\Smtp\Protocol\Response $response **/
$response = $mail->send($text);
StatusCode
- 0 發(fā)送成功
- 101 客戶端連接超時(shí)
- 102 對(duì)端不是smtp協(xié)議服務(wù)
- 103 客戶端接收超時(shí)
- 201 向smtp服務(wù)標(biāo)識(shí)發(fā)送者失敗
- 202 smtp服務(wù)不支持此驗(yàn)證模式
- 203 用戶名出現(xiàn)錯(cuò)誤
- 204 密碼驗(yàn)證失敗
- 205 發(fā)件人郵箱地址有誤
- 206 收件人郵箱地址有誤
- 207 標(biāo)識(shí)郵件數(shù)據(jù)開始錯(cuò)誤
- 208 標(biāo)識(shí)郵件數(shù)據(jù)結(jié)束錯(cuò)誤
- 209 退出smtp會(huì)話失敗