PHPMailer+phalcon中使用

一、下载phpmailer

   见备注。

二、打开phalcon,根目录下的index.php

    include __DIR__ . "/../app/extensions/phpmailer/PHPMailerAutoload.php";

三、作为service,加入到DI容器

  $di->set('phpmailer', function() {
    return new PHPMailer();
  });

四、实例function

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';

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');
}
}

 

思考:

  今天朋友问题,获取接收邮件列表的问题,只是简略的回答,自己没有测试,有一个例子,在备注。 

 

 

备注:

下载最新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 //补充一个获取邮件的类

 

posted @ 2014-06-18 20:11  zyliang  阅读(770)  评论(0编辑  收藏  举报