将文件放在系统目录下的library/Vendor目录下。下载地址:http://www.thinkphp.cn/extend/731.html (目录存放看个人爱好啦!)
在配置文件 config.php 里面设置邮箱发送信息(~~!可以在后台修改邮箱设置):下面举例用的是QQ邮箱 而且你的QQ邮箱必须开通 smtp功能:没有设置的请你的QQ邮箱 设置->账号:
/*======================== 邮件配置 ==========================*/
/* 邮件服务器 */
'MAIL_HOST' => 'smtp.qq.com', //服务器地址
"MAIL_SSL" => '465', //端口号
'MAIL_SECURE' => 'ssl', //加密方式
'MAIL_SMTP' => "smtp", // smtp服务器
/* 账号 */
'MAIL_USERNAME' => '972581428@qq.com', //发送邮箱的账号
/* 密码 */
'MAIL_PASSWORD' => 'kehui***********', //密码
前端代码(举例测试而已~~实际需求请修改):
" method="post" accept-charset="utf-8">邮件主题:
邮件来自邮箱号:
收件人邮箱:
邮件内容:
后台控制器(举例测试而已~~实际需求请修改):
public function post_form () {
emailsms(I("post.name"),I("post.id"),I("post.emali"),I("post.content"));
}
在Common/function.php方法:/**
* 发送邮箱
* @param [type] $name [主题名称]
* @param [type] $id [邮箱编号]
* @param [type] $cname [收件人账号]
* @param [type] $content [内容]
* @return [type]
*/
function emailsms ($name,$id,$cname,$content) {
Vendor('Swift.swift_required');
$transport=\Swift_SmtpTransport::newInstance(C("MAIL_HOST"),C("MAIL_SSL"),C("MAIL_SECURE"))->setUsername(C("MAIL_USERNAME"))->setPassword(C("MAIL_PASSWORD"));
$mailer = \Swift_Mailer::newInstance($transport);
$message= \Swift_Message::newInstance()->setSubject($name)->setFrom(array($id=>C('MAIL_USERNAME')))->setTo($cname)->setContentType("text/html")->setBody($content);
$mailer->protocol = C("MAIL_SMTP");
$mailer->send($message);
}