ThinkPHP3.2与PHPMailer完美结合

浏览:4698 发布日期:2016/07/01 分类:技术分享 关键字: thinkphp phpmailer 发邮件
和大家分下一下PHPMailer结合ThinkPHP3.2实现邮件发送的例子

首先下载PHPMailer_v5.1
下载地址http://down.chinaz.com/soft/31112.htm

将class.phpmailer.php和class.SMTP.php复制到Org/Util目录下

将class.phpmailer.php改名为PHPMailer.class.php,并修改2317行,改为
class phpmailerException extends \Think\Exception {
//原先是extends Exception
}
和705行,改为引入文件Smtp.class.php
都要带上命名空间namespace Org\Util
将class.SMTP.php改成Smtp.class.php,并修改类名

在config.php文件里添加邮件配置
//邮件配置
'MAIL_CHARSET'=>'UTF-8',
'MAIL_PORT'=>25,
'MAIL_HOST'=>'smtp.126.com',
'MAIL_USER'=>'********',
'MAIL_PASS'=>'*********',
'MAIL_COM_NAME'=>'********',
'MAIL_ALT'=>'To view the message, please use an HTML compatible email viewer!',
'MAIL_WORDWRAP'=>100,

在function.php文件里新建方法
function to_send_mail($mail,$to,$subject,$body){
ini_set("magic_quotes_runtime",0);
try {
$mail->IsSMTP();
$mail->CharSet =C('MAIL_CHARSET');
$mail->SMTPAuth = true;
$mail->Port = C('MAIL_PORT');
$mail->Host = C('MAIL_HOST');
$mail->Username = C('MAIL_USER');
$mail->Password = C('MAIL_PASS');
$mail->AddReplyTo(C('MAIL_USER'),C('MAIL_COM_NAME'));
$mail->From = C('MAIL_USER');
$mail->FromName = C('MAIL_COM_NAME');
$mail->AddAddress($to);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AltBody = C('MAIL_ALT');
$mail->WordWrap = C('MAIL_WORDWRAP');
//$mail->AddAttachment("1.gif");
$mail->IsHTML(true);
$mail->Send();
return true;
} catch (phpmailerException $e) {
return $e->errorMessage();
}
}

最后在控制器里调用

public function send_mail(){
$to = $_POST['email'];
$subject = 'ThinkPHP && PHPMailer 邮件发送';
$body = '<h1>phpmail演示</h1>ThinkPHP && PHPMailer 邮件发送' ;
$mail = new \Org\Util\PHPMailer(true);
$re = to_send_mail($mail,$to,$subject,$body);
var_dump($re);
}

附件 PHPMailer_v5.1.rar ( 104.54 KB 下载:207 次 )

最佳答案
评论( 相关
后面还有条评论,点击查看>>