#1 anythink
需要服务器启动Curl扩展保证邮件发送的稳定,使用SendGrid接口发送,1秒能发送一封邮件。已经在生产环境使用。代码如下:
/*类配置信息如下
* sendGrid 发邮件api
* 参数 to、toname、xsmtapi、from、fromname、subject、text、html、bcc、date、headers、files
* headers、xsmtpapi JSON
* url: http://docs.sendgrid.com/apiworkshop/
* */
class sendGridApi {
private $apiUri = 'http://sendgrid.com/api/mail.send.json';
private $attributes;
public $error;
function __construct(){
$this->api_user = $GLOBALS['G_SP']['mail']['mail_user'];
$this->api_key = $GLOBALS['G_SP']['mail']['mail_pwd'];
$this->from = $GLOBALS['G_SP']['mail']['mail_from'];
$this->fromname = $GLOBALS['G_SP']['mail']['mail_fromname'];
}
private function __get($v){
return $this->attributes[$k];
}
private function __set($k,$v){
$this->attributes[$k] = $v;
}
public function send(){
$this->sendDate();
}
/*设置x-smtpapi*/
public function setSmtpApi($v){
$this->attributes['x-smtpapi'] = $v;
}
private function sendDate(){
$string = http_build_query($this->attributes);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$this->apiUri.'?'.$string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$output = curl_exec($ch);
if ($output === FALSE) {
$this->error = 'CurlError:'.curl_error($ch);
}else{
$output = json_decode($output,true);
}
curl_close($ch);
if($output['message'] != 'success'){
$this->error = 'SendGridError:'.implode(',',$output['errors']);
}
}
}
?>
$mail ['mail_port'] = '25';调用方法如下
$mail ['mail_user'] = 'username';
$mail ['mail_pwd'] = 'password';
$mail ['mail_from'] = 'from';
$mail ['mail_fromname'] = 'fromname';
加入spConfig
......
'mail' => $mail,
'mode' => 'debug', // 应用程序模式,默认为调试模式
'api_path' => APP_PATH . '/api_controller', // API目录
'sp_error_show_source' => 5, // spError显示代码的行数
.......
$mail = spClass('sendGridApi');
//$mail->to ='xi@anythink.com.cn';
$mail->toname = 'anythink';
$mail->subject = '邮件发送的标题';
$mail->html = '邮件发送的html代码
';
//$mail->setSmtpApi('{"category": "Test3 Category"}');
$mail->send();
if($mail->error != ''){
//do someting...
}
2012-09-06 13:23:37