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

时间:2023-03-10 04:01:37
SAE云平台上传图片和发送邮件

1.远程图片保存至Storage

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

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

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

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

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

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

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

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

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