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