Welcome 微信登录

首页 / 网页编程 / PHP / ThinkPHP邮件发送类

ThinkPHP邮件发送类2014-04-06最近在做一个项目,需要有邮件发送的功能,服务器邮件发送的话,服务器上必须有能链接邮件服务器,才能实现以下的步骤,现在就给大家分享一下,专门做了一个邮件的发送类

/** * 系统邮件发送函数 * @param string $to接收邮件者邮箱 * @param string $name接收邮件者名称 * @param string $subject 邮件主题 * @param string $body邮件内容 * @param string $attachment 附件列表 * @return boolean */ function think_send_mail($to, $name, $subject = "", $body = "", $attachment = null){$config = C("THINK_EMAIL");vendor("PHPMailer.class#phpmailer"); //从PHPMailer目录导class.phpmailer.php类文件$mail = new PHPMailer(); //PHPMailer对象$mail->CharSet= "UTF-8"; //设定邮件编码,默认ISO-8859-1,如果发中文此项必须设置,否则乱码$mail->IsSMTP();// 设定使用SMTP服务$mail->SMTPDebug= 0; // 关闭SMTP调试功能 // 1 = errors and messages // 2 = messages only$mail->SMTPAuth = true;// 启用 SMTP 验证功能$mail->SMTPSecure = "ssl"; // 使用安全协议$mail->Host = $config["SMTP_HOST"];// SMTP 服务器$mail->Port = $config["SMTP_PORT"];// SMTP服务器的端口号$mail->Username = $config["SMTP_USER"];// SMTP服务器用户名$mail->Password = $config["SMTP_PASS"];// SMTP服务器密码$mail->SetFrom($config["FROM_EMAIL"], $config["FROM_NAME"]);$replyEmail = $config["REPLY_EMAIL"]?$config["REPLY_EMAIL"]:$config["FROM_EMAIL"];$replyName= $config["REPLY_NAME"]?$config["REPLY_NAME"]:$config["FROM_NAME"];$mail->AddReplyTo($replyEmail, $replyName);$mail->Subject= $subject;$mail->MsgHTML($body);$mail->AddAddress($to, $name);if(is_array($attachment)){ // 添加附件foreach ($attachment as $file){is_file($file) && $mail->AddAttachment($file);}}return $mail->Send() ? true : $mail->ErrorInfo; }
此函数只能在ThinkPHP中使用且需要phpmailer扩展的支持;

phpmailer扩展的放置目录为 ThinkPHP/Extend/Vendor/PHPMailer/class.phpmailer.php

phpmail的下载地址:

https://code.google.com/a/apache-extras.org/p/phpmailer

使用此函数 必须在项目中加入以下配置项

//邮件配置 "THINK_EMAIL" => array("SMTP_HOST" => "smtp.aaa.com", //SMTP服务器"SMTP_PORT" => "465", //SMTP服务器端口"SMTP_USER" => "mail@aaa.com", //SMTP服务器用户名"SMTP_PASS" => "password", //SMTP服务器密码"FROM_EMAIL"=> "mail@aaa.com", //发件人EMAIL"FROM_NAME" => "ThinkPHP", //发件人名称"REPLY_EMAIL" => "", //回复EMAIL(留空则为发件人EMAIL)"REPLY_NAME"=> "", //回复名称(留空则为发件人名称) ),
本文出自 “尛雷” 博客,请务必保留此出处http://a3147972.blog.51cto.com/2366547/1221287

查看本栏目更多精彩内容:http://www.bianceng.cn/webkf/PHP/