用file_get_contents和Curl向sendcloud发送post请求

使用Curl 向sendCloud发送post请求

使用sendCloud发送邮件需要我们向sendCloud提供的接口发送包括一系列信息的post请求,然后通过返回值判断是否发送成功。

代码示例
//使用的是sendCloud非模板的发送方式
function send_mail($to){
    $email = $to.'@qq.com';
    $ch = curl_init();
    $url = 'http://sendcloud.sohu.com/webapi/mail.send.json';//此地址可以查看sendCloud文档
    $timeout='5';
    $str =' ';//这是邮件内容
    $post_data = array(
            'api_user' => ' ',//在sendCloud中设置
            'api_key' => ' ',//在sendCloud中设置
            'from' => ' ', // 发信人,用正确邮件地址替代
            'fromname' => ' ',//发件人名字
            'to' => $email,//目标邮件地址
            'subject' => ' ',//邮件主题
            'html' => $str,//邮件内容
    );
        curl_setopt ($ch, CURLOPT_URL, $url);
        curl_setopt ($ch, CURLOPT_POST, 1);
        if($post_data != ''){
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        }
 
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_HEADER, false);
 
        $file_contents = curl_exec($ch);
        $return = json_decode($file_contents);
        curl_close($ch);
 
        if(is_object($return) && $return->message=='success') {
            return true;
        } else {
            return false;
        }
}

使用file_get_contents方式向sendCloud发送post请求

这种方法比Curl简洁

代码示例
function send_mail($to) {
    $email = $to.'@qq.com';
    $url = 'http://sendcloud.sohu.com/webapi/mail.send.json';   
    $str ='  ';
    $param = array(
            'api_user' => '  ',
            'api_key' => '  ',
            'from' => '  ', # 发信人,用正确邮件地址替代
            'fromname' => '  ',
            'to' => $email,
            'subject' => '  ',
            'html' => $str,
    );
    $options = array(
            'http' => array(
                    'method' => 'POST',
                    'header' => 'Content-type: application/x-www-form-urlencoded',
                    'content' => http_build_query($param)));
    $context  = stream_context_create($options);
    $result = file_get_contents($url, false, $context);
    $return = json_decode($result);
    if(is_object($return) && $return->message=='success') {
        return true;
    } else {
        return false;
    }
}
补充代码
 function file_get_content($url) {
  if (function_exists('file_get_contents')) {
    $file_contents = @file_get_contents($url);
  }
  if ($file_contents == '') {
    $ch = curl_init();
    $timeout = 30;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $file_contents = curl_exec($ch);
    curl_close($ch);
  }
  return $file_contents;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,273评论 19 139
  • cURL是一个利用URL语法规定来传输文件和数据的工具,支持很多协议和选项,如HTTP、FTP、TELNET等,能...
    司马东阳阅读 5,314评论 0 6
  • 原文地址:PHPcURL库函数抓取页面内容(转)作者:巴克 cURL是一个利用URL语法规定来传输文件和数据的工具...
    司马东阳阅读 4,941评论 0 3
  • 2017.10.12.星期四多云 这 一天又在忙碌中结束了,但我过的很充实,只可惜这一周又不能陪女儿了,在...
    899037e3b5bb阅读 1,558评论 1 0
  • 我们都习惯了用第三方框架去约束控件,大家常用的OC中的Masonry,Swift中的SnapKit,当然了这是一个...
    伊蕊飘零阅读 3,957评论 0 0

友情链接更多精彩内容