<?php
/**
* Simple example script using PHPMailer with exceptions enabled
* @package phpmailer
* @version $Id$
*/ require_once'/class.phpmailer.php';
require_once'/class.smtp.php';
try {
$mail = new PHPMailer(true); //New instance, with exceptions enabled $body = file_get_contents('contents.html');
$body = preg_replace('/\\\\/','', $body); //Strip backslashes $mail->IsSMTP(); // tell the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 25; // set the SMTP server port
$mail->Host = "smtp.163.com"; // SMTP server
$mail->Username = "1585358****@163.com"; // SMTP server username
$mail->Password = "*******"; // SMTP server password //$mail->IsSendmail(); // tell the class to use Sendmail $mail->AddReplyTo("1585358****@163.com","First Last"); $mail->From = "1585358****@163.com";
$mail->FromName = "First Last"; $to = "309407***@qq.com"; $mail->AddAddress($to); $mail->Subject = "First PHPMailer Message"; $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap = 80; // set word wrap $mail->MsgHTML($body); $mail->IsHTML(true); // send as HTML $mail->Send();
echo 'Message has been sent.';
} catch (phpmailerException $e) {
echo $e->errorMessage();
}
?>
用PHP发送邮件, 可以用PHPMailer。PHPMailer是一个用于发送电子邮件的PHP函数包

那么PHPMailer怎么用呢?

第一步当然是下载PHPMailer包,有两个版本 、php4版本和php5/6版本的 下载自己合适的

下载完以后 ,看到里面有很多文件 , 其实大部分都用不到

然后进入 test看到testemail.php 文件 这里面是一个例子

代码类似

<?php
/**
* Simple example script using PHPMailer with exceptions enabled
* @package phpmailer
* @version $Id$
*/ require '../class.phpmailer.php'; try {
$mail = new PHPMailer(true); //New instance, with exceptions enabled $body = file_get_contents('contents.html'); // 发送的内容
$body = preg_replace('/\\\\/','', $body); //Strip backslashes $mail->IsSMTP(); // tell the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 25; // set the SMTP server port
$mail->Host = "smtp.163.com"; // SMTP server
$mail->Username = "yinxiaoluan110@163.com"; // SMTP server username
$mail->Password = "*********"; // 填写你自己邮箱的密码 //$mail->IsSendmail(); // tell the class to use Sendmail $mail->AddReplyTo("yinxiaoluan110@163.com","First Last"); $mail->From = "yinxiaoluan110@163.com"; //发件人邮箱
$mail->FromName = “小白"; //发件人 $to = "745888750@qq.com"; //收件人 $mail->AddAddress($to); $mail->Subject = "First PHPMailer Message"; $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap = 80; // set word wrap $mail->MsgHTML($body); $mail->IsHTML(true); // send as HTML $mail->Send();
echo 'Message has been sent.';
} catch (phpmailerException $e) {
echo $e->errorMessage();
}
?>
上面代码 ,有注释说明的比较重要 需要认真填写一下 有不懂SMTP服务器的 可以参考一下 wordpress发送解决方法 这篇文章 注意:
1.如果报错 :Could not execute: /var/qmail/bin/sendmail 那么你需要把 配置文件中的 $mail->IsSendmail(); 注释掉
2.如果你用的是空间
报错:SMTP Error: Could not connect to SMTP host
那么你需要 修改
class.smtp.php
$this->smtp_conn = @fsockopen
改为
$this->smtp_conn = @pfsockopen
因为很多空间把fsockopen禁用了! 按照以上步骤 , 您的邮件应该已经发送成功了 其实 phpmailer 包我们真正用到的文件只有 class.phpmailer.php和class.smtp.php
require_once('class.phpmailer.php');//引入类库文件
$mail = new PHPMailer(); //实例化
$mail->IsSMTP(); // 启用SMTP
$mail->Host = "smtp.sina.com"; //SMTP服务器 这里以新浪邮箱为例子
$mail->Port = 25; //邮件发送端口
$mail->SMTPAuth = true; //启用SMTP认证
$mail->CharSet = "UTF-8"; //字符集
$mail->Encoding = "base64"; //编码方式
$mail->Username = "test@sina.com"; //你的邮箱
$mail->Password = "123"; //你的密码
$mail->Subject = "测试邮件标题"; //邮件标题
$mail->From = "test@sina.com"; //发件人地址(也就是你的邮箱)
$mail->FromName = "发件人测试姓名"; //发件人姓名
$address = "test@sina.com";//收件人email
$mail->AddAddress($address, "某某人");//添加收件人地址,昵称
$mail->AddAttachment("test,zip","重命名附件.zip"); // 添加附件,并重命名
$mail->IsHTML(true); //支持html格式内容
$mail->Body = "这是一份测试邮件,此为邮件内容"; //邮件内容
//发送
if(!$mail->Send()) {
echo "fail: " . $mail->ErrorInfo;
} else {
echo "ok";
}
<?php
/**
* Simple example script using PHPMailer with exceptions enabled
* @package phpmailer
* @version $Id$
*/ require_once'/class.phpmailer.php';
require_once'/class.smtp.php';
try {
$mail = new PHPMailer(true); //New instance, with exceptions enabled //$body = file_get_contents('contents.html');
//$body = preg_replace('/\\\\/','', $body); //Strip backslashes $mail->IsSMTP(); // tell the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 25; // set the SMTP server port
$mail->Host = "smtp.163.com"; // SMTP server
$mail->Username = "1585358****@163.com"; // SMTP server username
$mail->Password = "**********"; // SMTP server password //$mail->IsSendmail(); // tell the class to use Sendmail $mail->AddReplyTo("1585358****@163.com","First Last"); $mail->From = "1585358****@163.com";
$mail->FromName = "First Last"; $to = "******@qq.com"; $mail->AddAddress($to); $mail->Subject = "First PHPMailer Message"; $mail->Body="这是我的测试邮件,the test email"; $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap = 80; // set word wrap //$mail->CharSet = "UTF-8"; //字符集 //$mail->MsgHTML($body); $mail->IsHTML(true); // send as HTML $mail->Send();
echo 'Message has been sent.';
} catch (phpmailerException $e) {
echo $e->errorMessage();
}
?>
<?php
require("class.phpmailer.php"); //下载的文件必须放在该文件所在目录
$mail = new PHPMailer(); //建立邮件发送类
$address = $_POST['address'];
$mail->IsSMTP(); // 使用SMTP方式发送
$mail->CharSet='UTF-8';// 设置邮件的字符编码
$mail->Host = "mail.xxxxx.***"; // 您的企业邮局域名
$mail->SMTPAuth = true; // 启用SMTP验证功能
$mail->Port = "***"; //SMTP端口
$mail->Username = ***@xxxx.***; // 邮局用户名(请填写完整的email地址)
$mail->Password = "******"; // 邮局密码
$mail->From = ***@xxxx.***; //邮件发送者email地址
$mail->FromName = "您的名称";
$mail->AddAddress("$address", "");//收件人地址,可以替换成任何想要接收邮件的email信箱,格式是AddAddress("收件人email","收件人姓名")
//$mail->AddReplyTo("", "");
//$mail->AddAttachment("/var/tmp/file.tar.gz"); // 添加附件
//$mail->IsHTML(true); // set email format to HTML //是否使用HTML格式
$mail->Subject = "PHPMailer测试邮件"; //邮件标题
$mail->Body = "Hello,这是测试邮件"; //邮件内容
$mail->AltBody = "This is the body in plain text for non-HTML mail clients"; //附加信息,可以省略
if(!$mail->Send())
{
echo "邮件发送失败. <p>";
echo "错误原因: " . $mail->ErrorInfo;
exit;
}
echo "邮件发送成功";
?>

我的PHPMailer_v5.1 使用的更多相关文章

  1. PHPMailer_v5.1 使用[转]

    <?php /* * email 报警,主要检查服务器数据库是否还能正常连接 */ require("../common/config.php"); include(&quo ...

  2. Thinkphp 使用gmail发送邮件

    1.Thinkphp 发送邮件内容来自:http://www.thinkphp.cn/code/32.html /** * 系统邮件发送函数 * @param string $to 接收邮件者邮箱 * ...

  3. tp 邮件发送

    1.需要phpmail邮件发送包, 2.邮件发送函数function sendMail($to, $title, $content){ require_once('./PHPMailer_v5.1/c ...

随机推荐

  1. win 10 下面安装 mysql-8.0.12-winx64 的过程

    win 10 下面安装 mysql-8.0.12-winx64 的过程 1.官网下载 mysql 2.解压到你要安装的目录 3.在mysql目录D:\Programming\mysql-8.0.12- ...

  2. ubuntu16.04 安装composer和 laravel

    一.安装composer $ sudo apt-get update $ sudo apt-get install wget 下载composer.phar $ wget https://getcom ...

  3. 四:Ionic Framework不支持Android4.2.2的解决方法

    目前有一个项目是在Ionic3上开发的,浏览器中进行开发和处理,并将项目打包至Android7.1的平板中查看,运行效果是不错的 大体的框架与交互方式已经完成了,开会时并演示给用户看,发现都是不错的, ...

  4. python 多网站采集,解决编码问题

    import chardet def get_charset(str): return chardet.detect(str)['encoding'] html = '' # bytes 字节串 ch ...

  5. mysql 允许远程IP连接, 并查看所有用户的所有权限

    添加mysql用户 http://my.oschina.net/u/1179414/blog/202377 允许远程ip连接 GRANT select,insert,update,delete ON ...

  6. CentOS7编译安装PostgreSQL

    创建组和用户 groupadd postgres useradd -g postgres postgres passwd postgres 编译安装 yum install -y gcc gcc-c+ ...

  7. 【java回调】同步/异步回调机制的原理和使用方法

    回调(callback)在我们做工程过程中经常会使用到,今天想整理一下回调的原理和使用方法. 回调的原理可以简单理解为:A发送消息给B,B处理完后告诉A处理结果.再简单点就是A调用B,B调用A. 那么 ...

  8. 使用html+css实现三角标示符号

    我们平常打开某个网站的时候,常常会发现网页上很多导航或者指示条会使用一个三角符号去指向内容,效果简洁美观,甚至很多前端面试中也会提及如何在网页上实现一个三角符号,这里给出一个很简单使用的实现方式. 首 ...

  9. POJ2234 Matches Game 尼姆博弈 博弈论

    http://poj.org/problem?id=2234 尼姆博弈(Nimm's Game) 指的是这样一个博弈游戏:有任意堆物品,每堆物品的个数是任意的,双方轮流从中取物品,每一次只能从一堆物品 ...

  10. python基础之集合,字符编码

    六.集合类型 1.用途:关系运算 2.定义方式:s = {1,2,’a’} {}内用,分隔开多个元素,每个元素都必须是不可变(即可hash)类型 强调:2.1集合内的元素时无序的 2.2集合内的元素不 ...