Email 类
CodeIgniter 拥有强大的 Email 类来提供如下的功能:
- Multiple Protocols: Mail, Sendmail, and SMTP(支持多种协议)
- Multiple recipients(多个邮件接收人)
- CC and BCCs
- HTML or Plaintext email(HTML邮件和Plaintext明码文本 明码文本也叫做明码报文,是普通的,未被加密成密文或经过解密所得的且直接可读的文本。)
- Attachments(支持发送附件)
- Word wrapping
- Priorities(支持邮件优先级)
- BCC Batch Mode, enabling large email lists to be broken into small BCC batches.
- Email Debugging tools
发送邮件
发送邮件不仅简单,而且可以发送时进行配置或者将参数放到配置文件中。
这里是一个发送邮件的标准示例。注意:该示例是假定使用一个控制器来发送邮件。
$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 参数
有17个不同的有效参数来提供给你如何定制你发送的电子邮件。您可以在此手动设置,或自动通过你储存在的配置文件中的来设置,描述如下:
参数设定通过一系列的参数值去完成电子邮件的initialize功能。这里有一个例子,说明怎样设置一些参数设定:
$config['protocol'] = 'sendmail';
$config['mailpath'] = '/usr/sbin/sendmail';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;
$this->email->initialize($config);
Note: Most of the preferences have default values that will be used if you do not set them.
在配置文件中设置 Email 参数
如果您不想使用使用上述方法设定参数,您可以把它们放入一个配置文件。创建一个新文件称为email.php ,添加$config数组在该文件中。然后将该文件保存为config/email.php 它将自动的被使用。如果您保存了一个参数配置文件,就不需要使用$this->email->initialize()函数来初始化参数了
Email 参数
The following is a list of all the preferences that can be set when sending email.
| 参数 | 默认值 | 选项 | 描述 |
|---|---|---|---|
| useragent | CodeIgniter | 无 | 用户代理 "user agent"。 |
| protocol | mail, sendmail, or smtp | 邮件发送协议。 | |
| mailpath | /usr/sbin/sendmail | 无 | 服务器上 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" | Newline character. (Use "\r\n" to comply with RFC 822). |
| newline | \n | "\r\n" or "\n" or "\r" | Newline character. (Use "\r\n" to comply with RFC 822). |
| bcc_batch_mode | FALSE | TRUE or FALSE (boolean) | Enable BCC Batch Mode. |
| bcc_batch_size | 200 | None | Number of emails in each BCC batch. |
Email 函数参考
$this->email->from()
设置发件人email地址和名称:
$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()
设置收件人email地址(多个). 地址可以是单个、一个以逗号分隔的列表或是一个数组:
$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()
设置抄送(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()
Sets the alternative email 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.
$this->email->clear()
Initializes all the email variables to an empty state. This function is intended for use if you run the email sending function 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('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();
}
If you set the parameter to TRUE any attachments will be cleared as well:
$this->email->clear(TRUE);
$this->email->send()
The Email sending function. Returns boolean TRUE or FALSE based on success or failure, enabling it to be used conditionally:
if ( ! $this->email->send())
{
// Generate error
}
$this->email->attach()
Enables you to send an attachment. Put the file path/name in the first parameter. Note: Use a file path, not a URL. For multiple attachments use the function 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');
$this->email->send();
$this->email->print_debugger()
Returns a string containing any server messages, the email headers, and the email messsage. Useful for debugging.
Overriding Word Wrapping
If you have word wrapping enabled (recommended to comply with RFC 822) and you have a very long link in your email it can get wrapped too, causing it to become un-clickable by the person receiving it. CodeIgniter lets you manually override word wrapping within part of your message like this:
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.
Place the item you do not want word-wrapped between: {unwrap} {/unwrap}