thinkphp如何使用sendcloud发送邮件

首先注册一个sendcloud账号,就会有一个API_USER和一个API_KEY
然后根据sendcloud的api接口就可以进行对接了。

<?php
namespace app\api\controller;
use think\Controller;

class Index extends Controller
{
    public function index()
    {
        $res = $this->send_mail();
        dump($res);
    }
   
    //普通发送
    public function send_mail() {
        $url = 'http://api.sendcloud.net/apiv2/mail/send';
        $API_USER = 'SendCloud的API_USER';
        $API_KEY = 'SendCloud的API_KEY';

        $param = array(
            'apiUser' => $API_USER,//SendCloud的API_USER账号
            'apiKey' => $API_KEY,//SendCloud的API_KEY
            'from' => 'huangzhibo@C0rQTeFbN7NggD518gA95DjvHWFSqMfY.sendcloud.org', # //发件人邮箱
           'fromName' => 'SendCloud',//发件人昵称
            'to' => 'xxx@qq.com',//收件人邮箱
            'subject' => 'Sendcloud php webapi common example',//邮件标题
            'html' => '欢迎使用SendCloud',//邮件正文
            'respEmailId' => 'true'
        );
       

        $data = http_build_query($param);

        $options = array(
            'http' => array(
                'method' => 'POST',
                'header' => 'Content-Type: application/x-www-form-urlencoded',
                'content' => $data
        ));
        $context  = stream_context_create($options);
        $result = file_get_contents($url, FILE_TEXT, $context);
        $result = json_decode($result,true);
        return $result;
    }
   
    //发送模板邮件
    public function sendtemplate_mail() {
        $url = 'http://api.sendcloud.net/apiv2/mail/sendtemplate';
       
        $API_USER = 'SendCloud的API_USER';
        $API_KEY = 'SendCloud的API_KEY';

        $param = array(
            'apiUser' => $API_USER, # Verification using api_user and api_key
           'apiKey' => $API_KEY,
            'from' => 'huangzhibo@C0rQTeFbN7NggD518gA95DjvHWFSqMfY.sendcloud.org', # The sender, use the correct email address instead
           'fromName' => 'SendCloud',
            'useAddressList' => 'true',
            'to' => 'xxx@qq.com',# Use alias address of address list
           'templateInvokeName' => 'test_template',
            'subject' => '测试标题',
            'respEmailId' => 'true'
        );
       

        $data = http_build_query($param);

        $options = array(
            'http' => array(
                'method' => 'POST',
                'header' => 'Content-Type: application/x-www-form-urlencoded',
                'content' => $data
        ));
        $context  = stream_context_create($options);
        $result = file_get_contents($url, FILE_TEXT, $context);
        $result = json_decode($result,true);
        return $result;
    }
   
   
    //查询地址列表
    public function searchlist()
    {
        $url = 'http://api.sendcloud.net/apiv2/addresslist/list';
        $API_USER = 'SendCloud的API_USER';
        $API_KEY = 'SendCloud的API_KEY';
        $param = [
                'apiUser' => $API_USER,
                'apiKey' => $API_KEY,
            ];
        $data = http_build_query($param);

        $options = array(
            'http' => array(
                'method' => 'POST',
                'header' => 'Content-Type: application/x-www-form-urlencoded',
                'content' => $data
        ));
        $context  = stream_context_create($options);
        $result = file_get_contents($url, FILE_TEXT, $context);
        $result = json_decode($result,true);
        return $result;
    }
   
    //添加地址列表
    public function addlist()
    {
        $url = 'http://api.sendcloud.net/apiv2/addresslist/add';
        $API_USER = 'SendCloud的API_USER';
        $API_KEY = 'SendCloud的API_KEY';
       
        $address = 'xxx@qq.com';//要添加的邮件地址
       
        $param = [
                'apiUser' => $API_USER,
                'apiKey' => $API_KEY,
                'address'=>$address,
                'name' => '名称1'
            ];
        $data = http_build_query($param);

        $options = array(
            'http' => array(
                'method' => 'POST',
                'header' => 'Content-Type: application/x-www-form-urlencoded',
                'content' => $data
        ));
        $context  = stream_context_create($options);
        $result = file_get_contents($url, FILE_TEXT, $context);
        $result = json_decode($result,true);
        return $result;
    }
}

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: