©
Dieses Dokument verwendetPHP-Handbuch für chinesische WebsitesFreigeben
CodeIgniter强大的电子邮件类支持以下功能:
多种协议:Mail,Sendmail和SMTP
用于SMTP的TLS和SSL加密
多个收件人
CC和BCC
HTML或纯文本电子邮件
附件
Word包装
优先级
BCC批处理模式,可以将大量电子邮件列表分解为小BCC批次。
电子邮件调试工具
使用电子邮件库
在配置文件中设置电子邮件首选项
发送电子邮件
设置邮件首选项
- [Email Preferences](about:blank#email-preferences)- [Overriding Word Wrapping](about:blank#overriding-word-wrapping)
类参考
发送电子邮件不仅简单,而且您可以随时对其进行配置或将您的偏好设置设置为配置文件。
以下是一个演示您如何发送电子邮件的基本示例。注意:此示例假定您正在从您的一个控制器发送电子邮件。
$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种不同的优先选择可用于定制您的电子邮件发送方式。您可以按照此处所述手动设置它们,或者通过存储在配置文件中的首选项自动设置它们,如下所述:
首选项是通过向电子邮件初始化方法传递一组首选项值来设置的。以下是您如何设置一些首选项的示例:
$config['protocol'] = 'sendmail';$config['mailpath'] = '/usr/sbin/sendmail';$config['charset'] = 'iso-8859-1';$config['wordwrap'] = TRUE;$this->email->initialize($config);
注意
如果您不设置它们,大部分首选项都会使用默认值。
如果您不想使用上述方法设置首选项,则可以将它们放入配置文件中。只需创建一个名为email.php的新文件,在该文件中添加$ config数组。然后将该文件保存在config / email.php中,它将自动使用。如果将$this->email->initialize()
首选项保存在配置文件中,则不需要使用该方法。
以下是发送电子邮件时可以设置的所有首选项列表。
偏爱 |
默认值 |
选项 |
描述 |
---|---|---|---|
用户代理 |
笨 |
没有 |
“用户代理”。 |
协议 |
邮件 |
mail,sendmail或smtp |
邮件发送协议。 |
MAILPATH |
/ usr / sbin目录/ sendmail的 |
没有 |
Sendmail的服务器路径。 |
smtp_host |
没有默认 |
没有 |
SMTP服务器地址。 |
smtp_user |
没有默认 |
没有 |
SMTP用户名。 |
smtp_pass |
没有默认 |
没有 |
SMTP密码。 |
SMTP_PORT |
25 |
没有 |
SMTP端口。 |
smtp_timeout |
5 |
没有 |
SMTP超时(以秒为单位)。 |
smtp_keepalive |
假 |
TRUE或FALSE(布尔值) |
启用持久性SMTP连接。 |
smtp_crypto |
没有默认 |
tls或ssl |
SMTP加密 |
换行 |
真正 |
TRUE或FALSE(布尔值) |
启用自动换行。 |
wrapchars |
76 |
|
字符数要包装在。 |
mailtype |
文本 |
文字或html |
邮件类型。如果您发送HTML电子邮件,则必须将其作为完整的网页发送。确保你没有任何相对链接或相对图像路径,否则他们将无法工作。 |
字符集 |
$ config'charset” |
|
字符集(utf-8,iso-8859-1等)。 |
验证 |
假 |
TRUE或FALSE(布尔值) |
是否验证电子邮件地址。 |
优先 |
3 |
1, 2, 3, 4, 5 |
邮件优先。1 =最高。5 =最低。3 =正常。 |
CRLF |
\ n |
“\ r \ n”或“\ n”或“\ r” |
换行符。(使用“\ r \ n”符合RFC 822)。 |
新队 |
\ n |
“\ r \ n”或“\ n”或“\ r” |
换行符。(使用“\ r \ n”符合RFC 822)。 |
bcc_batch_mode |
假 |
TRUE或FALSE(布尔值) |
启用BCC批处理模式。 |
bcc_batch_size |
200 |
没有 |
每个BCC批次中的电子邮件数量。 |
DSN |
假 |
TRUE或FALSE(布尔值) |
从服务器启用通知消息 |
如果您启用了自动换行功能(建议遵循RFC 822),并且您的电子邮件中有很长的链接,则它也可能被封装,导致其被收件人无法点击。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}
class CI_Emailfrom($from[, $name = ''[, $return_path = NULL]])
参数: |
$ from(string) - “From”电子邮件地址$ name(字符串) - “From”显示名称$ return_path(字符串) - 可选电子邮件地址将未传送的电子邮件重定向到 |
---|---|
返回: |
CI_Email实例(方法链接) |
返回类型: |
CI_Email |
$ from(string) - “From”电子邮件地址
$ name(字符串) - “从”显示名称
$ return_path(字符串) - 可选电子邮件地址将未发送的电子邮件重定向到
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 theprint_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(''); $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