Email 類
CodeIgniter 擁有強大的 Email 類來提供如下的功能:
- 多協(xié)議:Mail、Sendmail 和 SMTP
- 多個收件人
- 抄送和密送
- HTML 或純文本郵件
- 附件
- 自動換行
- 郵件優(yōu)先級
- 密送批處理模式,開啟時,大郵件列表將被分成小批次密送。
- Email 調(diào)試工具
發(fā)送郵件
發(fā)送郵件不僅簡單,而且可以發(fā)送時進行配置或者將參數(shù)放到配置文件中。
這里是一個發(fā)送郵件的標準示例。注意:該示例是假定使用一個控制器來發(fā)送郵件。
$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();
設(shè)置 Email 參數(shù)
有17個不同的有效參數(shù)來提供給你如何定制你發(fā)送的電子郵件。您可以在此手動設(shè)置,或自動通過你儲存在的配置文件中的來設(shè)置,描述如下:
參數(shù)設(shè)定通過一系列的參數(shù)值去完成電子郵件的initialize功能。這里有一個例子,說明怎樣設(shè)置一些參數(shù)設(shè)定:
$config['protocol'] = 'sendmail';
$config['mailpath'] = '/usr/sbin/sendmail';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;
$this->email->initialize($config);
說明: 大多數(shù)參數(shù)都有默認值,如果你沒有配置這些參數(shù),它們的默認值就會被使用。
在配置文件中設(shè)置 Email 參數(shù)
如果您不想使用使用上述方法設(shè)定參數(shù),您可以把它們放入一個配置文件。創(chuàng)建一個新文件稱為email.php ,添加$config數(shù)組在該文件中。然后將該文件保存為config/email.php 它將自動的被使用。如果您保存了一個參數(shù)配置文件,就不需要使用$this->email->initialize()函數(shù)來初始化參數(shù)了
Email 參數(shù)
下面是發(fā)送電子郵件時可以設(shè)置的屬性的列表。
參數(shù) | 默認值 | 選項 | 描述 |
---|---|---|---|
useragent | CodeIgniter | 無 | 用戶代理 "user agent"。 |
protocol | mail, sendmail, or smtp | 郵件發(fā)送協(xié)議。 | |
mailpath | /usr/sbin/sendmail | 無 | 服務(wù)器上 Sendmail 的實際路徑。protocol 為 sendmail 時使用。 |
smtp_host | 無默認值 | 無 | SMTP 服務(wù)器地址。 |
smtp_user | 無默認值 | 無 | SMTP 用戶賬號。 |
smtp_pass | 無默認值 | 無 | SMTP 密碼。 |
smtp_port | 25 | 無 | SMTP 端口。 |
smtp_timeout | 5 | 無 | SMTP 超時設(shè)置(單位:秒)。 |
wordwrap | TRUE | TRUE 或 FALSE (布爾值) | 開啟自動換行。 |
wrapchars | 76 | 自動換行時每行的最大字符數(shù)。 | |
mailtype | text | text 或 html | 郵件類型。發(fā)送 HTML 郵件比如是完整的網(wǎng)頁。請確認網(wǎng)頁中是否有相對路徑的鏈接和圖片地址,它們在郵件中不能正確顯示。 |
charset | utf-8 | 字符集(utf-8, iso-8859-1 等)。 | |
validate | FALSE | TRUE 或 FALSE (布爾值) | 是否驗證郵件地址。 |
priority | 3 | 1, 2, 3, 4, 5 | Email 優(yōu)先級. 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 | 批量暗送的郵件數(shù). |
Email 函數(shù)參考
$this->email->from()
設(shè)置發(fā)件人email地址和名稱:
$this->email->from('you@example.com', 'Your Name');
$this->email->reply_to()
設(shè)置郵件回復(fù)地址. 如果沒有提供這個信息,將會使用"from()"函數(shù)中的值. 例如:
$this->email->reply_to('you@example.com', 'Your Name');
$this->email->to()
設(shè)置收件人email地址(多個). 地址可以是單個、一個以逗號分隔的列表或是一個數(shù)組:
$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()
設(shè)置抄送(Carbon Copy / CC) email地址(多個). 類似to()函數(shù), 地址可以是單個、一個以逗號分隔的列表或是一個數(shù)組.
$this->email->bcc()
設(shè)置暗送(Blind Carbon Copy / BCC) email地址(多個). 類似to()函數(shù), 地址可以是單個、一個以逗號分隔的列表或是一個數(shù)組.
$this->email->subject()
設(shè)置email主題:
$this->email->subject('This is my subject');
$this->email->message()
設(shè)置email正文部分:
$this->email->message('This is my message');
$this->email->set_alt_message()
設(shè)置可選的郵件EMAIL正文部分:
$this->email->set_alt_message('This is the alternative message');
這是EMAIL可選的一部分,如果你發(fā)送帶HTML的郵件,這可以用到。它用于當(dāng)接收郵件都不支持HTML格式時顯示給用戶的內(nèi)容。如果你沒有設(shè)置這部分,CodeIginiter會自動從郵件正文中提取去掉TAGS的部分。
$this->email->clear()
將所有EMAIL的變量清空. 這個方法用于當(dāng)你在循環(huán)中發(fā)送郵件時,可以在兩次循環(huán)中重新設(shè)置郵件內(nèi)容。
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();
}
如果將參數(shù)設(shè)為TRUE,附件也會被清空:
$this->email->clear(TRUE);
$this->email->send()
發(fā)送EMAIL. 根據(jù)發(fā)送結(jié)果,成功返回TRUE,失敗返回FALSE。就可以將它用于判斷語句:
if ( ! $this->email->send())
{
????// Generate error
}
$this->email->attach()
添加附件。第一個參數(shù)是文件路徑/文件名. 注意: 用路徑而不是URL。多次使用該函數(shù)可以添加多個附件:
$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()
返回包含郵件內(nèi)容的字符串,包括EMAIL頭和EMAIL正文。用于調(diào)試。
取消自動換行
如果你啟用自動換行(建議遵循 RFC 822),EMAIL中有一個非常長的鏈接它將會換行,導(dǎo)致鏈接不能被收信人直接點擊打開。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}中間
?