您的位置:首页 > 编程语言 > PHP开发

使用cackPHP发送smtp邮件

2010-04-09 17:23 711 查看
读者要求:1.熟悉php、2.熟悉cackphp。

cackephp框架中已有组件email.php,具体位置:cakephp/cake/libs/controller/components。现在先讲一下此组件基本的参数

to

收件人地址(string)
cc
抄送人数组
bcc
密件副本收件人数组(blind carbon copy)
replyTo
回信地址(string)
from
发送人地址(string)
subject
邮件标题(string)
template
邮件模板(保存于本地的app/views/elements/email/html/ 和 app/views/elements/email/text/)
layout
邮件使用的布局(保存于本地的app/views/layouts/email/html/ 和app/views/layouts/email/text/)
lineLength
自动换行长度, 默认为70。(integer)
sendAs
邮件发送格式 text, html 或者 both
attachments
附件路径(绝对或相对地址)
delivery
邮件发送方式(mail, smtp [需要下面的 smtpOptions设置] 和 debug)
smtpOptions
smtp参数设置(port, host, timeout, username, password, client)
发送邮件的关键代码如下:

var $components = array('Email');
function sendNewUserMail() {

$this->Email->to = '<592938903@qq.com>';
//$this->Email->bcc = array('<592938903@qq.com>');
$this->Email->subject = 'Welcome to our really cool thing';
$this->Email->replyTo = '<592938903@qq.com>';
$this->Email->from = '<exp@myemailserver.com>';
$this->Email->template = 'simple_message'; // note no '.ctp'
//Send as 'html', 'text' or 'both' (default is 'text')
$this->Email->sendAs = 'html'; // because we like to send pretty mail
//Set view variables as normal
$this->Email->smtpOptions = array(
'port'=>'25',
'timeout'=>'30',
'host' => 'mailserver',
'username'=>'username',
'password'=>'pwd',
'client' => 'mailserver'
);
//Do not pass any args to send()
$this->Email->delivery = 'smtp';
$this->Email->send();
$err= $this->Email->smtpError;
echo $err;//此次可查看错误,正式发布时要注销掉
}


其他地方调用此处是请使用$this->sendNewUserMail();如果要循环发送邮件再发送下封邮件时要使用$this->Email->reset()重置。

代码中文件的位置app/views/elements/email/html/simple_message.ctp
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: