ThinkPHP 3.2 DEMO案例系列【phpmailer批量發送郵件】

來源:互聯網
上載者:User

標籤:

但是郵件和簡訊相比在一些情境依然有著重要的意義和優勢;

1:零成本;發郵件沒有費用;

2:內容豐富且量大;郵件可以長篇大論;圖文並茂;

3:增加訪問量;使用者很容易通過郵件中的連結訪問網站;

好了;下面來來為thinkphp整合郵件系統;

樣本項目:https://github.com/Tinywan/ThinkPhpStudy

一、引入phpmail

從樣本項目中複製兩個檔案到自己的項目;

/ThinkPHP/Library/Org/Nx/class.phpmailer.php ;/ThinkPHP/Library/Org/Nx/class.smtp.php ;

二、設定配置項

 //***********************************郵件伺服器**********************************    ‘EMAIL_FROM_NAME‘        => ‘[email protected]‘, // 寄件者    ‘EMAIL_SMTP‘             => ‘smtp.163.com‘, // smtp    ‘EMAIL_USERNAME‘         => ‘[email protected]‘, // 帳號    ‘EMAIL_PASSWORD‘         => ‘你的密碼 // 授權密碼而不是登陸的密碼

三、配置163箱:

    首先是要開啟smtp服務:

    設定檔中的資訊在這裡:

注意:開啟過程中是要設定一個授權密碼,這裡的授權密碼也就是設定檔中需用的那個密碼,否則的話否則,SMTP connect() failed

‘EMAIL_PASSWORD‘         => ‘你的密碼 // 授權密碼而不是登陸的密碼

 四、發送郵件的實際代碼,這個在(ThinkPHP架構中的Function()函數中寫著來)

/** * 發送郵件 * @param  string $address 需要發送的郵箱地址 發送給多個地址需要寫成數組形式 * @param  string $subject 標題 * @param  string $content 內容 * @return boolean       是否成功 */function send_email($address,$subject,$content){    $email_smtp=C(‘EMAIL_SMTP‘);    $email_username=C(‘EMAIL_USERNAME‘);    $email_password=C(‘EMAIL_PASSWORD‘);    $email_from_name=C(‘EMAIL_FROM_NAME‘);    if(empty($email_smtp) || empty($email_username) || empty($email_password) || empty($email_from_name)){        return array("error"=>1,"message"=>‘郵箱配置不完整‘);    }    require ‘./ThinkPHP/Library/Org/Nx/class.phpmailer.php‘;    require ‘./ThinkPHP/Library/Org/Nx/class.smtp.php‘;    $phpmailer=new \Phpmailer();    // 設定PHPMailer使用SMTP伺服器發送Email    $phpmailer->IsSMTP();    // 設定為html格式    $phpmailer->IsHTML(true);    // 設定郵件的字元編碼‘    $phpmailer->CharSet=‘UTF-8‘;    // 設定SMTP伺服器。    $phpmailer->Host=$email_smtp;    // 設定為"需要驗證"    $phpmailer->SMTPAuth=true;    // 設定使用者名稱    $phpmailer->Username=$email_username;    // 設定密碼    $phpmailer->Password=$email_password;    // 設定郵件標頭的From欄位。    $phpmailer->From=$email_username;    // 設定寄件者名字    $phpmailer->FromName=$email_from_name;    // 添加收件者地址,可以多次使用來添加多個收件者    if(is_array($address)){        foreach($address as $addressv){            $phpmailer->AddAddress($addressv);        }    }else{        $phpmailer->AddAddress($address);    }    // 設定郵件標題    $phpmailer->Subject=$subject;    // 設定郵件內文    $phpmailer->Body=$content;    // 發送郵件。    if(!$phpmailer->Send()) {        $phpmailererror=$phpmailer->ErrorInfo;        return array("error"=>1,"message"=>$phpmailererror);    }else{        return array("error"=>0);    }}

五、發送郵件調用函數樣本:

 /**     * 發送郵件     */    public function send_email()    {        $name = I(‘post.name‘, ‘這是一份測試郵件名稱‘);        $address = I(‘post.address‘, ‘這是一份測試郵件發送的地址‘);        $phone = I(‘post.phone‘, ‘13669361192‘);        $email = ‘[email protected]‘;        $str = <<<html            姓名:$name <br>            地址:$address <br>            手機號:$phonehtml;        $result = send_email($email, ‘這個郵件是我用來做測試的,親!‘, $str);        if ($result[‘error‘] == 1) {            var_dump($result);            die;        }        var_dump(‘發送完成‘);    }

基本來源:http://baijunyao.com

 

ThinkPHP 3.2 DEMO案例系列【phpmailer批量發送郵件】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.