Home  >  Article  >  Backend Development  >  php发送邮件方法-亲测可用,email.class.php过期解决办法

php发送邮件方法-亲测可用,email.class.php过期解决办法

WBOY
WBOYOriginal
2016-06-23 13:23:211395browse

php虽然提供了mail()函数,但并不好用,而PHPMailer是一个不错的邮件发送工具,使用起来也是非常简单!
使用PHPMailer发送邮件:

<?php header("content-type:text/html;charset=utf-8"); ini_set("magic_quotes_runtime",0); require 'class.phpmailer.php'; try { $mail = new PHPMailer(true); $mail->IsSMTP(); $mail->CharSet='UTF-8'; //设置邮件的字符编码,这很重要,不然中文乱码 $mail->SMTPAuth = true; //开启认证 $mail->Port = 25; $mail->Host = "smtp.163.com"; $mail->Username = "phpddt1990@163.com"; $mail->Password = "这是密码"; //$mail->IsSendmail(); //如果没有sendmail组件就注释掉,否则出现“Could not execute: /var/qmail/bin/sendmail ”的错误提示 $mail->AddReplyTo("phpddt1990@163.com","mckee");//回复地址 $mail->From = "phpddt1990@163.com"; $mail->FromName = "www.guodongkeji.com"; $to = "987044391@qq.com"; $mail->AddAddress($to); $mail->Subject = "phpmailer测试标题"; $mail->Body = "<h1>phpmail演示</h1>这是php点点通(<font color=red>www.guodongkeji.com</font>)对phpmailer的测试内容"; $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; //当邮件不支持html时备用显示,可以省略 $mail->WordWrap = 80; // 设置每行字符串的长度 //$mail->AddAttachment("f:/test.png"); //可以添加附件 $mail->IsHTML(true); $mail->Send(); echo '邮件已发送'; } catch (phpmailerException $e) { echo "邮件发送失败:".$e->errorMessage(); } ?> 

打开我的qq邮件可以看到:

测试非常顺利:
从上图可以看出,PHPMailer是支持html格式发送,而且支持发送图片,附件!经过测试,对各种SMTP服务器兼容很好!
添加附件如果报错:
那是因为(set_magic_quotes_runtime())已经关闭。并且在PHP6中已经完全移除此特性。
你可以注释或者删除掉出错的行,或者是在set_magic_quotes_runtime()前面加@符号
或者是配置;error_reporting = E_ALL & ~E_NOTICE & ~E_DEPRECATED
PHPMailer及测试文件下载:http://pan.baidu.com/s/1dDJizpJ

更多相关信息请咨询:APP开发

 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn