1、下載phpmailerphp
見備註。html
2、打開phalcon,根目錄下的index.phpgit
include __DIR__ . "/../app/extensions/phpmailer/PHPMailerAutoload.php";github
3、做爲service,加入到DI容器安全
$di->set('phpmailer', function() {
return new PHPMailer();
});服務器
4、實例functionapp
public function sendEmail($config)
{
if(!$config['address'] || !$config['subject'] || !$config['body']){
return array('flag'=>0,'msg'=>'地址欄、主題、郵件內容,不能爲空');
}
$mail = $this->phpmailer; // 這個phalcon下直接調用di的service
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = '****'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'c**@**'; // SMTP username
$mail->Password = '123456'; // SMTP password
// $mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted 安全協議,此處不加,聯彤服務器沒加,so
$mail->CharSet = "utf-8"; //本身加的
$mail->From = '**@**.com';
$mail->FromName = '**管理系統';
if(@$config['isClear'] == 1){ //http://blog.sina.com.cn/s/blog_64589b1101016pxd.html 講phpmailer參數的
$mail->ClearAddresses(); //爲何定時發送郵件須要清理,由於是同一個實例,而indexcontroller,用戶訪問一次就是一個實例,不會被記憶。
}
if(is_array($config['address'])){
foreach ($config['address'] as $ads) {
$mail->addAddress($ads,$ads);
}
}else{
$mail->addAddress($config['address'],$config['address']);
}
// $mail->addAddress('910ss@qq.com', 'Jimliang'); // Add a recipient
// $mail->addReplyTo('jim**@gmail.com', 'jim---test');
// $mail->addCC('jiml***@chs**.com');
$mail->WordWrap = 50; // Set word wrap to 50 characters
// $mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
// $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML測試
$mail->Subject = $config['subject'];
$mail->Body = $config['body'];
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';this
if(!$mail->send()) {
return array('flag'=>0,'msg'=>'Message could not be sent. <br/>Mailer Error: ' . $mail->ErrorInfo);
} else {
return array('flag'=>1,'msg'=>'Congratulation! Message send success');
}
}spa
思考:
今天朋友問題,獲取接收郵件列表的問題,只是簡略的回答,本身沒有測試,有一個例子,在備註。
備註:
下載最新phpmailer
https://github.com/PHPMailer/PHPMailer
http://blog.sina.com.cn/s/blog_64589b1101016pxd.html //phpmailer的接口,ClearAddresses--方法 都很好
http://www.zgguan.com/zsfx/php/573.html //補充一個獲取郵件的類