频道栏目
首页 > 资讯 > CI框架 > 正文

CI 框架的email类怎么用,CI email类配置。CI 使用

16-01-07        来源:[db:作者]  
收藏   我要投稿
CI 里面有强大的 Email 类来提供一些功能,很少有介绍CI 的东西,这里介绍CI的email类的使用方法,来自CI 中国官方网站。

比如 多协议:Mail、Sendmail 和 SMTP 多个收件人 抄送和密 HTML 或纯文本邮 附 自动换 邮件优先 密送批处理模式,开启时,大邮件列表将被分成小批次密送。 Email 调试工具

发送邮件

发送邮件不仅简单,而且可以发送时进行配置或者将参数放到配置文件中。

这里是一个发送邮件的标准示例。注意:该示例是假定使用一个控制器来发送邮件。

$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_window();

设置 Email 参数

有17个不同的有效参数来提供给你如何定制你发送的电子邮件。您可以在此手动设置,或自动通过你储存在的配置文件中的来设置,描述如下:

参数设定通过一系列的参数值去完成电子邮件的initialize功能。这里有一个例子,说明怎样设置一些参数设定:

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

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

说明: 大多数参数都有默认值,如果你没有配置这些参数,它们的默认值就会被使用。

在配置文件中设置 Email 参数

如果您不想使用使用上述方法设定参数,您可以把它们放入一个配置文件。创建一个新文件称为email.php,添加$config数组在该文件中。然后将该文件保存为config/email.php它将自动的被使用。如果您保存了一个参数配置文件,就不需要使用$this->email->initialize()函数来初始化参数了

$this->email->from()Email 函数参考

设置发件人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()

设置可选的邮件EMAIL正文部分:

$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()

发送EMAIL. 根据发送结果,成功返回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_window()

返回包含邮件内容的字符串,包括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}中间。

 

相关TAG标签
上一篇:Smarty教程(基本语法)
下一篇:Smarty优点以及不适合smarty的地方
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站