123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- <?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){
- 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()) {
- return true;
- }else{
- show_error($this->mail->ErrorInfo);
- return false;
- }
- } catch (phpmailerException $e) {
- show_error($e->errorMessage(),500); //Pretty error messages from PHPMailer
- } catch (Exception $e) {
- show_error($e->getMessage(),500); //Boring error messages from anything else!
- }
- }
- }
|