SAE云平台上传图片和发送邮件

 

1.远程图片保存至Storage

  其中public是Storage中的容器名,"目录1/目录2/"是容器下的路径 $file_content 是得到的文件数据

1 $s = new SaeStorage();
2 $file_content= file_get_contents('http://abc.pmg');   //括号中的为远程图片地址
3 $s->write ( 'public' ,  '目录1/目录2/abc.png' , $file_content); 
    
2.sae上的上传图片
   用thinkphp框架内置上传类能够完美兼容sae环境,只是在sae上上传成功后返回的路    径是文件名,需要手动加上上传的容器路径,即在项目中需要替换__ING__或者是__PUBLIC__    的路径,得到路径的方法如下:
1 $storage = new SaeStorage();                      //实例化SaeStorage类
2 $uploadsae = $storage->getUrl('public','Upload');  //得到sae的上传容器路径 
 其中public是Storage中的容器名,"Upload"是容器下的路径;
 替换方法可采用tp的模板替换方法:
1 'TMPL_PARSE_STRING'    =>    array(
2      '__ING__'    => $st->getUrl('public','upload'),
3      '__PUBLIC__' => $st->getUrl('application','user')
4 
5 )

 

3:生成缩略图并保存在相应的容器中

   如果用tp的生成缩略图方法也可以在sae上成功运行,下面使用sae提供的方法生成缩略图
 1 $f = new SaeFetchurl();
 2 $img_data = $f->fetch(“图片url”); //有文件名读取文件
 3 $img = new SaeImage();
 4 $img->setData($img_data);
 5 $img->resize($width,$height);    //指定缩略图的宽和高
 6 $new_data = $img->exec();        //执行处理并返回处理后的二进制数据
 7 if ($new_data === false){        //图片处理失败时输出错误
 8         return false;
 9 }
10 $s->write ( 'public' , '目录1/目录2/abc.png' , $file_content);
11 $img->exec( 'jpg' , true );  //如果不想保存,而是想输出则用:

4:不带html的邮件(不支持html)
1 $mail = new SaeMail();
2 $body = "亲爱的用户:感谢您在在本网站注册了新帐号。请点击链接激活您的帐号"3 $mail->setAttach(array("my_photo"=>"照片的二进制数据"));//如果发送图片
4 $ret = $mail->quickSend("收件人的邮箱", '邮件标题' , $body , '发送人的邮箱' , '发送人邮箱密码','smtp.139.com',25); //smtp.139.com是主机号,25是SMTP服务器端口
5 $mail->clean();     //清楚之前的对象用于循环发送

 

5:带html的邮件(支持html)

       其中setOpt()只在send()发送命令中起作用,在快速发送quickSend()中不起作用;

 1 $mail = new SaeMail();
 2 $mail->setOpt(array(
 3      'from'          => 'abc@sina.com', //发件邮箱例如abc@sina.com
 4      'to'            => $data['email'],      //接收信箱  
 5      'smtp_host'     => 'smtp.139.com',  //smtp服务器
 6      'smtp_port'     => 25,  //port
 7      'smtp_username' => 'abc@sina.com', //账户全名,abc@sina.com,要和上面的一样
 8      'smtp_password' => '发送邮箱吗密码',
 9      'subject'       => '标题',
10      'content'       => $body, //发送内容
11      'content_type'  => 'html'   //发送格式,默认是text
12      )
13 );
14 $ret = $mail->send();
15 $mail->clean();
16 if($ret == false){
17       var_dump($mail->errno(), $mail->errmsg()); //打印出错信息
18       return false;
19 }else{
20       return true;
21 }

想要了解跟多参数:http://apidoc.sinaapp.com/class-SaeMail.html

原文地址:https://www.cnblogs.com/zyf-zhaoyafei/p/4373665.html