CodeIgniter 使用手冊版本 2.1.4


Email 類別

CodeIgniter 擁有強大的 Email 類別函數,支持下列功能:

Sending Email

發送電子郵件不僅是簡單,也可以輕鬆設定個人偏好到檔案中。

底下是一個基本的例子,教您如何簡單的寄送電子郵件。注意: 這例子假設您的其中一個控制器(controllers)要發送電子郵件。

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

$this->email->from('your@example.com', 'Your Name');
$this->email->to('someone@example.com');
$this->email->cc('another@another-example.com');
$this->email->bcc('them@their-example.com');

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

$this->email->send();

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

設定 Email 參數

Email 類別函數提供了17個參數,讓您量身定制的電子郵件偏好設定。您可以手動設定,或者是自動設定在您的設定檔,說明如下:

可以利用陣列來初始化(initialize) E-mail 設定。底下這個例子,讓您知道如何設定基本參數:

$config['protocol'] = 'sendmail';
$config['mailpath'] = '/usr/sbin/sendmail';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;

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

注意: 假如您沒有設定參數值,大多數的參數都有各自預設值。

在設定文件檔中(Config File)設置 Email 參數偏好

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

Email 設定偏好

當您要發送郵件,底下是全部的偏好參數設定。

偏好設定 預設值 選擇 描述
useragentCodeIgniterNone用戶代理 "user agent"。
protocolmailmail,sendmail,or smtp電子郵件協定。
mailpath/usr/sbin/sendmailNone系統 Sendmail 路徑。
smtp_hostNo DefaultNoneSMTP 伺服器位址。
smtp_userNo DefaultNoneSMTP 使用者帳號。
smtp_passNo DefaultNoneSMTP 使用者密碼。
smtp_port25NoneSMTP 連接埠。
smtp_timeout5NoneSMTP 逾時(秒)。
wordwrapTRUETRUE or FALSE (boolean)開啟自動換行。
wrapchars76 自動換行時每行的最大字符數。
mailtypetexttext or html郵件型態。郵件類型。發送 HTML 郵件時,必須是完整的網頁。請確認網頁中是否有相對路徑的連結和圖片位址,它們在郵件中不能正確顯示。
charsetutf-8字元集 (utf-8,iso-8859-1,等.)。
validateFALSETRUE or FALSE (boolean)是否驗證郵件地址。
priority31,2,3,4,5Email 優先級. 1 = 最高. 5 = 最低. 3 = 正常。
crlf \n "\r\n" or "\n" or "\r" 換行符號。(使用 "\r\n" 遵守 822)。
newline\n "\r\n" or "\n" or "\r"換行符號。 (使用 "\r\n" 遵守 RFC 822).
bcc_batch_modeFALSETRUE or FALSE (boolean)啟動 BCC Batch 模式。
bcc_batch_size200None每個 BCC batch 傳送多少郵件。

Email 函數說明

$this->email->from()

設定寄件者 E-mail 跟名稱:

$this->email->from('you@example.com', 'Your Name');

$this->email->reply_to()

設定郵件回覆地址。如果沒有提供這個信息,將會使用 "from()" 函數中的值. 參考範例:

$this->email->reply_to('you@example.com', 'Your Name');

$this->email->to()

設定接受者 E-mail 位址. 可以是單一郵件,或是以逗號分隔名單,或者是陣列資料.:

$this->email->to('someone@example.com'); $this->email->to('one@example.com, two@example.com, three@example.com'); $list = array('one@example.com', 'two@example.com', 'three@example.com');

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

$this->email->cc()

設定郵件副本(CC). 就像寄送郵件,可以是單一郵件,或是以逗號分隔名單,或者是陣列資料.

$this->email->bcc()

設定密件副本(BCC). 就像寄送郵件,可以是單一郵件,或是以逗號分隔名單,或者是陣列資料.

$this->email->subject()

設定郵件標題:

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

$this->email->message()

設定郵件內容(body):

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

$this->email->set_alt_message()

設置替代電子郵件內容:Sets the alternative email message body:

$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('your@example.com');
    $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()

郵件寄送函數。當函數執行將會回傳 boolean TRUE(成功)或 FALSE(失敗),可以透過條件函數來判斷:

if ( ! $this->email->send())
{
    // Generate error
}

$this->email->attach()

新增附件檔案。第一個參數是檔案路徑/檔案名稱. 注意: 用檔案路徑而不是 URL。多次使用該函數可以新增多個附件:

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

$this->email->send();

$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} 中間