天天看點

ci架構(codeigniter)Email發送郵件、收件人、附件、Email調試工具

    ci架構(codeigniter)Email發送郵件、收件人、附件、Email調試工具

        Email 類

        CodeIgniter 擁有強大的 Email 類來提供如下的功能:

            多協定:Mail、Sendmail 和 SMTP

            多個收件人

            抄送和密送

            HTML 或純文字郵件

            附件

            自動換行

            郵件優先級

            密送批處理模式,開啟時,大郵件清單将被分成小批次密送。

            Email 調試工具

        發送郵件

        發送郵件不僅簡單,而且可以發送時進行配置或者将參數放到配置檔案中。

        這裡是一個發送郵件的标準示例。

         注意:該示例是假定使用一個控制器來發送郵件。

        $this->load->library('email');

        $this->email->from('[email protected]', 'Your Name');

        $this->email->to('[email protected]');

        $this->email->cc('[email protected]');

        $this->email->bcc('[email protected]');

        $this->email->subject('Email Test');

        $this->email->message('Testing the email class.');

        $this->email->send();

        echo $this->email->print_debugger();

        設定 Email 參數

        有17個不同的有效參數來提供給你如何定制你發送的電子郵件。您可以在此手動設定,或自動通過你儲存在配置檔案中的鍵值來設定,描述如下:

        參數設定通過一系列的參數值去完成電子郵件的initialize功能。這裡有一個例子,說明怎樣設定一些參數設定:

        $config['protocol'] = 'sendmail';

        $config['mailpath'] = '/usr/sbin/sendmail';

        $config['charset'] = 'iso-8859-1';

        $config['wordwrap'] = TRUE;

        $this->email->initialize($config);

        說明: 大多數參數都有預設值,如果你沒有配置這些參數,它們的預設值就會被使用。

        在配置檔案中設定 Email 參數

        如果您不想使用上述方法設定參數,您可以把它們放入一個配置檔案。建立一個新檔案名稱為email.php ,添加$config數組在該檔案中。然後将該檔案儲存為config/email.php 它将自動的被使用。如果您儲存了一個參數配置檔案,就不需要使用$this->email->initialize()函數來初始化參數了

        Email 參數

        下面是發送電子郵件時可以設定的屬性的清單。

        參數     預設值     選項     描述

        useragent    CodeIgniter    無    使用者代理 "user agent"。

        protocol    mail    mail, sendmail, or smtp    郵件發送協定。

        mailpath    /usr/sbin/sendmail    無    伺服器上 Sendmail 的實際路徑。protocol 為 sendmail 時使用。

        smtp_host    無預設值    無    SMTP 伺服器位址。

        smtp_user    無預設值    無    SMTP 使用者賬号。

        smtp_pass    無預設值    無    SMTP 密碼。

        smtp_port    25    無    SMTP 端口。

        smtp_timeout    5    無    SMTP 逾時設定(機關:秒)。

        wordwrap    TRUE    TRUE 或 FALSE (布爾值)    開啟自動換行。

        wrapchars    76        自動換行時每行的最大字元數。

        mailtype    text    text 或 html    郵件類型。發送 HTML 郵件比如是完整的網頁。請确認網頁中是否有相對路徑的連結和圖檔位址,它們在郵件中不能正确顯示。

        charset    utf-8        字元集(utf-8, iso-8859-1 等)。

        validate    FALSE    TRUE 或 FALSE (布爾值)    是否驗證郵件位址。

        priority    3    1, 2, 3, 4, 5    Email 優先級. 1 = 最高. 5 = 最低. 3 = 正常.

        crlf     \n     "\r\n" or "\n" or "\r"     換行符. (使用 "\r\n" to 以遵守RFC 822).

        newline    \n     "\r\n" or "\n" or "\r"    換行符. (使用 "\r\n" to 以遵守RFC 822).

        bcc_batch_mode    FALSE    TRUE or FALSE (boolean)    啟用批量暗送模式.

        bcc_batch_size    200    None    批量暗送的郵件數.

        Email 函數參考

        $this->email->from()

        設定發件人email位址和名稱:

        $this->email->from('[email protected]', 'Your Name');

        $this->email->reply_to()

        設定郵件回複位址. 如果沒有提供這個資訊,将會使用"from()"函數中的值. 例如:

        $this->email->reply_to('[email protected]', 'Your Name');

        $this->email->to()

        設定收件人email位址(多個). 位址可以是單個、一個以逗号分隔的清單或是一個數組:

        $this->email->to('[email protected]'); $this->email->to('[email protected], [email protected], [email protected]'); $list = array('[email protected]', '[email protected]', '[email protected]');

        $this->email->to($list);

        $this->email->cc()

        設定抄送(Carbon Copy / CC) email位址(多個). 類似to()函數, 位址可以是單個、一個以逗号分隔的清單或是一個數組.

        $this->email->bcc()

        設定暗送(Blind Carbon Copy / BCC) email位址(多個). 類似to()函數, 位址可以是單個、一個以逗号分隔的清單或是一個數組.

        $this->email->subject()

        設定email主題:

        $this->email->subject('This is my subject');

        $this->email->message()

        設定email正文部分:

        $this->email->message('This is my message');

        $this->email->set_alt_message()

        設定可選的郵件EMAIL正文部分:

        $this->email->set_alt_message('This is the alternative message');

        這是EMAIL可選的一部分,如果你發送帶HTML的郵件,這可以用到。它用于當接收郵件都不支援HTML格式時顯示給使用者的内容。如果你沒有設定這部分,CodeIginiter會自動從郵件正文中提取去掉TAGS的部分。

        $this->email->clear()

        将所有EMAIL的變量清空. 這個方法用于當你在循環中發送郵件時,可以在兩次循環中重新設定郵件内容。

        foreach ($list as $name => $address)

        {

            $this->email->clear();

            $this->email->to($address);

            $this->email->from('[email protected]');

            $this->email->subject('Here is your info '.$name);

            $this->email->message('Hi '.$name.' Here is the info you requested.');

            $this->email->send();

        }

        如果将參數設為TRUE,附件也會被清空:

        $this->email->clear(TRUE);

        $this->email->send()

        發送EMAIL. 根據發送結果,成功傳回TRUE,失敗傳回FALSE。就可以将它用于判斷語句:

        if ( ! $this->email->send())

            // Generate error

        $this->email->attach()

        添加附件。第一個參數是相對于入口檔案的檔案路徑/檔案名(不能寫成'/path/photo1.jpg'). 注意: 是路徑而不是URL。多次使用該函數可以添加多個附件:

        $this->email->attach('path/photo1.jpg');

        $this->email->attach('path/photo2.jpg');

        $this->email->attach('path/photo3.jpg');

        $this->email->print_debugger()

        傳回包含郵件内容的字元串,包括EMAIL頭和EMAIL正文。用于調試。

        取消自動換行

        如果你啟用自動換行(建議遵循 RFC 822),EMAIL中有一個非常長的連結它将會換行,導緻連結不能被收信人直接點選打開。CodeIgniter可以對正文的部分片段避免這種自動換行,比如:

        The text of your email that

        gets wrapped normally.

        {unwrap}http://example.com/a_long_link_that_should_not_be_wrapped.html{/unwrap}

        More text that will be

        wrapped normally.

        将你不想自動換行的部分放入: {unwrap} {/unwrap}中間

如何聯系我:【萬裡虎】www.bravetiger.cn

【QQ】3396726884 (咨詢問題100元起,幫助解決問題500元起)

【部落格】http://www.cnblogs.com/kenshinobiy/