Hyperf 完整项目-3-邮件-短信

邮件

安装 @https://github.com/Yurunsoft/PHPMailer-Swoole

"yurunsoft/phpmailer-swoole":"~1.0"

服务层

<?php
declare(strict_types=1);

namespace App\Service;

use Hyperf\Utils\Context;
use PHPMailer\PHPMailer\PHPMailer;

class SendEmailService
{

    public function doSendEmail()
    {
        $channel = new \Swoole\Coroutine\Channel();
        co(function() use ($channel) {
            $mail = new PHPMailer; //PHPMailer对象
            $mail->CharSet = 'UTF-8'; //设定邮件编码,默认ISO-8859-1,如果发中文此项必须设置,否则乱码
            $mail->IsSMTP(); // 设定使用SMTP服务
            $mail->SMTPDebug = 0; // 关闭SMTP调试功能
            $mail->SMTPAuth = true; // 启用 SMTP 验证功能
            $mail->SMTPSecure = 'ssl'; // 使用安全协议
            $mail->Host = 'smtp.163.com'; // SMTP 服务器
            $mail->Port = '465'; // SMTP服务器的端口号
            $mail->Username = ''; // SMTP服务器用户名
            $mail->Password = ''; // SMTP服务器密码
            $mail->SetFrom('', ''); // 邮箱,昵称
            $mail->Subject = 'title test';
            $mail->MsgHTML('hello world');
            $mail->AddAddress(''); // 收件人
            $result = $mail->Send();
            $channel->push($result);
        });
        return $channel->pop();

    }

}

短信

安装 @https://hyperf.wiki/#/zh-cn/guzzle

使用

<?php

declare(strict_types=1);

namespace App\Service;

use Hyperf\Di\Annotation\Inject;
use Hyperf\Guzzle\ClientFactory;

class SendSmsService
{

    /**
     * @Inject
     * @var ClientFactory
     */
    private $clientFactory;

    public function doSendSms($phone)
    {
        $code = mt_rand(100000, 999999);
        $string = '';
        try {
            // $options 等同于 GuzzleHttp\Client 构造函数的 $config 参数
            $options = [];
            // $client 为协程化的 GuzzleHttp\Client 对象
            $client = $this->clientFactory->create($options);
            $api_send_url = 'https://*.com/sendsms';
            //接口参数
            $postFields = array (
                'clientid'  =>  '',
                'password' => '',
                'mobile' => $phone,
                'content' => $string,
            );
            $jsonFields = json_encode($postFields);
            $response = $client->request('POST', $api_send_url, [
                'body' => $jsonFields,
                'headers' => array(
                    'Accept-Encoding: identity',
                    'Content-Length: ' . strlen($jsonFields),
                    'Accept:application/json',
                    'Content-Type: application/json; charset=utf-8'   //json版本需要填写  Content-Type: application/json;
                ),
            ]);

            $body = $response->getBody();
            $result = json_decode($body,true);
            if ($result) {
                //do something
                return ['code' => 200,'msg' => '发送成功!'];
            } else {
                return ['code' => 500,'msg' => '发送失败!'];
            }

        } catch (\Throwable $e) {
            return ['code' => 500,'msg' => $e->getMessage()];
        }
    }

}
本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 2

这是封装了 alisms 的PHP SDK 吗? :+1:

3年前 评论

第一段代码使用协程+通道有什么效果?会异步执行回调函数内的代码吗?没有这样用过。

3年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!