1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- <?php
- if (!defined('BASEPATH')) exit('No direct script access allowed');
- require_once "PHPMail/PHPMailerAutoload.php";
- class Mailer
- {
- var $mail;
- public function __construct()
- {
- $this->mail = new PHPMailer();
- }
- public function set_config($config){
- $this->mail->IsSMTP(); // telling the class to use SMTP
- $this->mail->CharSet = "utf-8"; // 一定要設定 CharSet 才能正确处理中文
- $this->mail->SMTPDebug = 0; // enables SMTP debug information
- $this->mail->SMTPAuth = true; // enable SMTP authentication
- $this->mail->SMTPSecure = $config['smtp_secure']; // sets the prefix to the servier
- $this->mail->Host = $config['server']; // sets GMAIL as the SMTP server
- $this->mail->Port = $config['port']; // set the SMTP port for the GMAIL server
- $this->mail->Username = $config['sender'];
- $this->mail->Password = $config['secret_key'];
- $this->mail->AddReplyTo($config['sender']);
- $this->mail->SetFrom($config['sender']);
- $this->mail->isHTML(true);
- }
- public function add_recipient($recipient){
- $this->mail->AddAddress($recipient);
- }
- /**
- * 发送邮件
- * @param $recipient
- * @param $subject
- * @param $content
- * @param null $attachments
- * @return bool
- */
- public function send_email($recipient,$subject,$content,$attachments=null){
- $resp = array();
- $resp['code'] = "FAILED";
- try{
- if($recipient) {
- $this->mail->AddAddress($recipient);
- }
- $this->mail->Subject = $subject;
- $this->mail->Body = $content;
- if($attachments){
- if(is_array($attachments)){
- foreach($attachments as $k=>$val){
- $this->mail->addAttachment($val);
- }
- }else{
- $this->mail->addAttachment($attachments);
- }
- }
- if($this->mail->Send()) {
- $resp['code'] = "OK";
- }else{
- $resp['msg'] = $this->mail->ErrorInfo;
- }
- } catch (phpmailerException $e) {
- $resp['msg']=$e->errorMessage(); //Pretty error messages from PHPMailer
- } catch (Exception $e) {
- $resp['msg']=$e->errorMessage(); //Boring error messages from anything else!
- }
- return $resp;
- }
- }
|