?
This document uses PHP Chinese website manual Release
CodeIgniter強(qiáng)大的電子郵件類支持以下功能:
多種協(xié)議:Mail,Sendmail和SMTP
用于SMTP的TLS和SSL加密
多個(gè)收件人
CC和BCC
HTML或純文本電子郵件
附件
Word包裝
優(yōu)先級
BCC批處理模式,可以將大量電子郵件列表分解為小BCC批次。
電子郵件調(diào)試工具
使用電子郵件庫
在配置文件中設(shè)置電子郵件首選項(xiàng)
發(fā)送電子郵件
設(shè)置郵件首選項(xiàng)
- [Email Preferences](about:blank#email-preferences)- [Overriding Word Wrapping](about:blank#overriding-word-wrapping)
類參考
發(fā)送電子郵件不僅簡單,而且您可以隨時(shí)對其進(jìn)行配置或?qū)⒛钠迷O(shè)置設(shè)置為配置文件。
以下是一個(gè)演示您如何發(fā)送電子郵件的基本示例。注意:此示例假定您正在從您的一個(gè)控制器發(fā)送電子郵件。
$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();
有21種不同的優(yōu)先選擇可用于定制您的電子郵件發(fā)送方式。您可以按照此處所述手動(dòng)設(shè)置它們,或者通過存儲在配置文件中的首選項(xiàng)自動(dòng)設(shè)置它們,如下所述:
首選項(xiàng)是通過向電子郵件初始化方法傳遞一組首選項(xiàng)值來設(shè)置的。以下是您如何設(shè)置一些首選項(xiàng)的示例:
$config['protocol'] = 'sendmail';$config['mailpath'] = '/usr/sbin/sendmail';$config['charset'] = 'iso-8859-1';$config['wordwrap'] = TRUE;$this->email->initialize($config);
注意
如果您不設(shè)置它們,大部分首選項(xiàng)都會(huì)使用默認(rèn)值。
如果您不想使用上述方法設(shè)置首選項(xiàng),則可以將它們放入配置文件中。只需創(chuàng)建一個(gè)名為email.php的新文件,在該文件中添加$ config數(shù)組。然后將該文件保存在config / email.php中,它將自動(dòng)使用。如果將$this->email->initialize()
首選項(xiàng)保存在配置文件中,則不需要使用該方法。
以下是發(fā)送電子郵件時(shí)可以設(shè)置的所有首選項(xiàng)列表。
偏愛 | 默認(rèn)值 | 選項(xiàng) | 描述 |
---|---|---|---|
用戶代理 | 笨 | 沒有 | “用戶代理”。 |
協(xié)議 | 郵件 | mail,sendmail或smtp | 郵件發(fā)送協(xié)議。 |
MAILPATH | / usr / sbin目錄/ sendmail的 | 沒有 | Sendmail的服務(wù)器路徑。 |
smtp_host | 沒有默認(rèn) | 沒有 | SMTP服務(wù)器地址。 |
smtp_user | 沒有默認(rèn) | 沒有 | SMTP用戶名。 |
smtp_pass | 沒有默認(rèn) | 沒有 | SMTP密碼。 |
SMTP_PORT | 25 | 沒有 | SMTP端口。 |
smtp_timeout | 5 | 沒有 | SMTP超時(shí)(以秒為單位)。 |
smtp_keepalive | 假 | TRUE或FALSE(布爾值) | 啟用持久性SMTP連接。 |
smtp_crypto | 沒有默認(rèn) | tls或ssl | SMTP加密 |
換行 | 真正 | TRUE或FALSE(布爾值) | 啟用自動(dòng)換行。 |
wrapchars | 76 | 字符數(shù)要包裝在。 | |
mailtype | 文本 | 文字或html | 郵件類型。如果您發(fā)送HTML電子郵件,則必須將其作為完整的網(wǎng)頁發(fā)送。確保你沒有任何相對鏈接或相對圖像路徑,否則他們將無法工作。 |
字符集 | $ config'charset” | 字符集(utf-8,iso-8859-1等)。 | |
驗(yàn)證 | 假 | TRUE或FALSE(布爾值) | 是否驗(yàn)證電子郵件地址。 |
優(yōu)先 | 3 | 1, 2, 3, 4, 5 | 郵件優(yōu)先。1 =最高。5 =最低。3 =正常。 |
CRLF | \ n | “\ r \ n”或“\ n”或“\ r” | 換行符。(使用“\ r \ n”符合RFC 822)。 |
新隊(duì) | \ n | “\ r \ n”或“\ n”或“\ r” | 換行符。(使用“\ r \ n”符合RFC 822)。 |
bcc_batch_mode | 假 | TRUE或FALSE(布爾值) | 啟用BCC批處理模式。 |
bcc_batch_size | 200 | 沒有 | 每個(gè)BCC批次中的電子郵件數(shù)量。 |
DSN | 假 | TRUE或FALSE(布爾值) | 從服務(wù)器啟用通知消息 |
如果您啟用了自動(dòng)換行功能(建議遵循RFC 822),并且您的電子郵件中有很長的鏈接,則它也可能被封裝,導(dǎo)致其被收件人無法點(diǎn)擊。CodeIgniter允許您手動(dòng)覆蓋部分消息中的文字換行,如下所示:
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}
class CI_Emailfrom($from[, $name = ''[, $return_path = NULL]])
參數(shù): | $ from(string) - “From”電子郵件地址$ name(字符串) - “From”顯示名稱$ return_path(字符串) - 可選電子郵件地址將未傳送的電子郵件重定向到 |
---|---|
返回: | CI_Email實(shí)例(方法鏈接) |
返回類型: | CI_Email |
$ from(string) - “From”電子郵件地址
$ name(字符串) - “從”顯示名稱
$ return_path(字符串) - 可選電子郵件地址將未發(fā)送的電子郵件重定向到
Returns: CI\_Email instance (method chaining)
Return type: CI\_Email
Sets the email address and name of the person sending the email:
$ this-> email-> from('email protected','Your Name');
You can also set a Return-Path, to help redirect undelivered mail:
$this->email->from('email protected', 'Your Name', 'email protected');
Note
Return-Path can’t be used if you’ve configured ‘smtp’ as your protocol.
reply_to($replyto[, $name = ''])
Parameters: | $replyto (string) – E-mail address for replies $name (string) – Display name for the reply-to e-mail address |
---|---|
Returns: | CI_Email instance (method chaining) |
Return type: | CI_Email |
$replyto (string) – E-mail address for replies
$name (string) – Display name for the reply-to e-mail address
Returns: CI\_Email instance (method chaining)
Return type: CI\_Email
Sets the reply-to address. If the information is not provided the information in the :meth:from method is used. Example:
$this->email->reply_to('email protected', 'Your Name');
to($to)
Parameters: | $to (mixed) – Comma-delimited string or an array of e-mail addresses |
---|---|
Returns: | CI_Email instance (method chaining) |
Return type: | CI_Email |
$to (mixed) – Comma-delimited string or an array of e-mail addresses
Returns: CI\_Email instance (method chaining)
Return type: CI\_Email
Sets the email address(s) of the recipient(s). Can be a single e-mail, a comma-delimited list or an array:
$this->email->to('email protected');
$this->email->to('email protected, email protected, email protected');
$this->email->to( array('email protected', 'email protected', 'email protected') );
cc($cc)
Parameters: | $cc (mixed) – Comma-delimited string or an array of e-mail addresses |
---|---|
Returns: | CI_Email instance (method chaining) |
Return type: | CI_Email |
$cc (mixed) – Comma-delimited string or an array of e-mail addresses
Returns: CI\_Email instance (method chaining)
Return type: CI\_Email
Sets the CC email address(s). Just like the “to”, can be a single e-mail, a comma-delimited list or an array.
bcc($bcc[, $limit = ''])
Parameters: | $bcc (mixed) – Comma-delimited string or an array of e-mail addresses $limit (int) – Maximum number of e-mails to send per batch |
---|---|
Returns: | CI_Email instance (method chaining) |
Return type: | CI_Email |
$bcc (mixed) – Comma-delimited string or an array of e-mail addresses
$limit (int) – Maximum number of e-mails to send per batch
Returns: CI\_Email instance (method chaining)
Return type: CI\_Email
Sets the BCC email address(s). Just like the `to()` method, can be a single e-mail, a comma-delimited list or an array.
If $limit
is set, “batch mode” will be enabled, which will send the emails to batches, with each batch not exceeding the specified $limit
.
subject($subject)
Parameters: | $subject (string) – E-mail subject line |
---|---|
Returns: | CI_Email instance (method chaining) |
Return type: | CI_Email |
$subject (string) – E-mail subject line
Returns: CI\_Email instance (method chaining)
Return type: CI\_Email
Sets the email subject:
$this->email->subject('This is my subject');
message($body)
Parameters: | $body (string) – E-mail message body |
---|---|
Returns: | CI_Email instance (method chaining) |
Return type: | CI_Email |
$body (string) – E-mail message body
Returns: CI\_Email instance (method chaining)
Return type: CI\_Email
Sets the e-mail message body:
$this->email->message('This is my message');
set_alt_message($str)
Parameters: | $str (string) – Alternative e-mail message body |
---|---|
Returns: | CI_Email instance (method chaining) |
Return type: | CI_Email |
$str (string) – Alternative e-mail message body
Returns: CI\_Email instance (method chaining)
Return type: CI\_Email
Sets the alternative e-mail message body:
$this->email->set_alt_message('This is the alternative message');
This is an optional message string which can be used if you send HTML formatted email. It lets you specify an alternative message with no HTML formatting which is added to the header string for people who do not accept HTML email. If you do not set your own message CodeIgniter will extract the message from your HTML email and strip the tags.
set_header($header, $value)
Parameters: | $header (string) – Header name $value (string) – Header value |
---|---|
Returns: | CI_Email instance (method chaining) |
Return type: | CI_Email |
$header (string) – Header name
$value (string) – Header value
Returns: CI\_Email instance (method chaining)
Return type: CI\_Email
Appends additional headers to the e-mail:
$this->email->set_header('Header1', 'Value1'); $this->email->set_header('Header2', 'Value2');
clear([$clear_attachments = FALSE])
Parameters: | $clear_attachments (bool) – Whether or not to clear attachments |
---|---|
Returns: | CI_Email instance (method chaining) |
Return type: | CI_Email |
$clear_attachments (bool) – Whether or not to clear attachments
Returns: CI\_Email instance (method chaining)
Return type: CI\_Email
Initializes all the email variables to an empty state. This method is intended for use if you run the email sending method in a loop, permitting the data to be reset between cycles.
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(); }
If you set the parameter to TRUE any attachments will be cleared as well:
$this->email->clear(TRUE);
send([$auto_clear = TRUE])
Parameters: | $auto_clear (bool) – Whether to clear message data automatically |
---|---|
Returns: | TRUE on success, FALSE on failure |
Return type: | bool |
$auto_clear (bool) – Whether to clear message data automatically
Returns: TRUE on success, FALSE on failure
Return type: bool
The e-mail sending method. Returns boolean TRUE or FALSE based on success or failure, enabling it to be used conditionally:
if ( ! $this->email->send()) { // Generate error }
This method will automatically clear all parameters if the request was successful. To stop this behaviour pass FALSE:
if ($this->email->send(FALSE)) { // Parameters won't be cleared }
Note
In order to use the print_debugger()
method, you need to avoid clearing the email parameters.
attach($filename[, $disposition = ''[, $newname = NULL[, $mime = '']]])
Parameters: | $filename (string) – File name $disposition (string) – ‘disposition’ of the attachment. Most email clients make their own decision regardless of the MIME specification used here. https://www.iana.org/assignments/cont-disp/cont-disp.xhtml $newname (string) – Custom file name to use in the e-mail $mime (string) – MIME type to use (useful for buffered data) |
---|---|
Returns: | CI_Email instance (method chaining) |
Return type: | CI_Email |
$filename (string) – File name
$disposition (string) – ‘disposition’ of the attachment. Most email clients make their own decision regardless of the MIME specification used here. https://www.iana.org/assignments/cont-disp/cont-disp.xhtml
$newname (string) – Custom file name to use in the e-mail
$mime (string) – MIME type to use (useful for buffered data)
Returns: CI\_Email instance (method chaining)
Return type: CI\_Email
Enables you to send an attachment. Put the file path/name in the first parameter. For multiple attachments use the method multiple times. For example:
$this->email->attach('/path/to/photo1.jpg'); $this->email->attach('/path/to/photo2.jpg'); $this->email->attach('/path/to/photo3.jpg');
To use the default disposition (attachment), leave the second parameter blank, otherwise use a custom disposition:
$this->email->attach('image.jpg', 'inline');
You can also use a URL:
$this->email->attach('http://example.com/filename.pdf');
If you’d like to use a custom file name, you can use the third parameter:
$this->email->attach('filename.pdf', 'attachment', 'report.pdf');
If you need to use a buffer string instead of a real - physical - file you can use the first parameter as buffer, the third parameter as file name and the fourth parameter as mime-type:
$this->email->attach($buffer, 'attachment', 'report.pdf', 'application/pdf');
attachment_cid($filename)
Parameters: | $filename (string) – Existing attachment filename |
---|---|
Returns: | Attachment Content-ID or FALSE if not found |
Return type: | string |
$filename (string) – Existing attachment filename
Returns: Attachment Content-ID or FALSE if not found
Return type: string
Sets and returns an attachment’s Content-ID, which enables your to embed an inline (picture) attachment into HTML. First parameter must be the already attached file name.
$filename = '/img/photo1.jpg'; $this->email->attach($filename); foreach ($list as $address) { $this->email->to($address); $cid = $this->email->attachment_cid($filename); $this->email->message('<img src="cid:'. $cid .'" alt="photo1" />'); $this->email->send(); }
Note
Content-ID for each e-mail must be re-created for it to be unique.
print_debugger([$include = array('headers', 'subject', 'body')])
Parameters: | $include (array) – Which parts of the message to print out |
---|---|
Returns: | Formatted debug data |
Return type: | string |
$include (array) – Which parts of the message to print out
Returns: Formatted debug data
Return type: string
Returns a string containing any server messages, the email headers, and the email message. Useful for debugging.
You can optionally specify which parts of the message should be printed. Valid options are: headers, subject, body.
Example:
// You need to pass FALSE while sending in order for the email data // to not be cleared - if that happens, print_debugger() would have // nothing to output. $this->email->send(FALSE); // Will only print the email headers, excluding the message subject and body $this->email->print_debugger(array('headers'));
Note
By default, all of the raw data will be printed.
? 2014–2017 British Columbia Institute of Technology
Licensed under the MIT License.
https://www.codeigniter.com/user_guide/libraries/email.html