MailXsendDemo


代码示列


<?php


    $appid = "";
    $appkey = "";
    $from = "from@domain.com";
    $to = "to@domain.com";
    $project_id = "";

    $version = "2";
    $sign_type = "md5";

    if($sign_type != "md5" && $sign_type != "sha1"){
        $post_data = Array
        (
            "appid" => $appid,
            "signature" => $appkey,
            "from" => $from,
            "to" => $to,
            "project" => $project_id,
            "vars"=>json_encode(array('content' => "来自SUBMAIL的通知邮件")),
        );
    }else{
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'http://api.mysubmail.com/service/timestamp.json');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 0);

        $output = curl_exec($ch);
        curl_close($ch);
        $output = json_decode($output,true);
        $timestamp = $output['timestamp'];
        

        $post_data = Array
        (
            "appid" => $appid,
            "from" => $from,
            "to" => $to,
            "project" => $project_id,
            "vars"=>json_encode(array('content' => "来自SUBMAIL的通知邮件")),
            "sign_version" => $version,
            "sign_type" => $sign_type,
            "timestamp" => $timestamp,
        );
        $temp = $post_data;
        
        if($version == '2'){
            unset($temp['vars']);
        }else{
            unset($temp['sign_version']);
        }

        ksort($temp);
        reset($temp);
        $tempStr = "";
        foreach ($temp as $key => $value) {
            $tempStr.=$key."=".$value."&";
        }
        $tempStr = substr($tempStr,0,count($tempStr)-2);
        if($sign_type == "md5")
            $post_data['signature'] = md5($appid.$appkey.$tempStr.$appid.$appkey);
        else
            $post_data['signature'] = sha1($appid.$appkey.$tempStr.$appid.$appkey);

    }

    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL, 'http://api.mysubmail.com/mail/xsend.json');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  
    $output = curl_exec($ch);
    curl_close($ch);
    echo json_encode($output);