声明:本站文章均为作者个人原创,图片均为实际截图。如有需要请收藏网站,禁止转载,谢谢配合!!!

phpMailer是一个非常强大的php发送邮件类,可以设定发送邮件地址、回复地址、邮件主题、html网页,上传附件,并且使用起来非常方便。本文将利用它实现邮件发送功能,需要的可以参考一下

用的phpmail版本是5.2.21以下
https://github.com/PHPMailer/PHPMailer/releases/tag/v5.2.21

1、下载phpmail压缩包,并解压。

2、创建index.html文件。并写入代码。

<form action="" method="post">
    <p>收件人邮箱:<input type="text" name="toemail" id="mail"/></p>
    <p>标  题:<input type="text" name="title" id="sub"/></p>
    <p>内  容:<textarea name="content" cols="50" id="con" rows="5"></textarea></p>
    <p><input type="button" value="发送" onclick="sendMail()"/></p>
</form>
<script>
    function sendMail() {
        mail=$('#mail').val();
        sub=$('#sub').val();
        con=$('#con').val();
        $.post('index.php',{mail:mail,sub:sub,con:con},function (data) {
            if (data=='Message has been sent.'){
                alert('发送成功');
            }else{
                alert('发送失败');
            }
        });
    }
</script>

3、创建index.php文件并写入代码。

首先引入class.phpmailer.php和class.smtp.php这两个类文件

<?php
    include "class.phpmailer.php";
    include "class.smtp.php";
    $mail = new PHPMailer();
    $mail->isSMTP();// 使用SMTP服务
    $mail->CharSet = "utf8";// 编码格式为utf8,不设置编码的话,中文会出现乱码
    $mail->Host = "smtp.163.com";// 发送方的SMTP服务器地址
    $mail->SMTPAuth = true;// 是否使用身份验证
    $mail->Username = "xxxx@163.com";// 发送方的163邮箱用户名
    $mail->Password = "xxxx";// 发送方的邮箱密码,注意用163邮箱这里填写的是“客户端授权密码”而不是邮箱的登录密码!
    $mail->SMTPSecure = "ssl";// 使用ssl协议方式
    $mail->Port = 994;// 163邮箱的ssl协议方式端口号是465/994
    $mail->From= "xxxx";
    $mail->Helo= "xxxx";
    $mail->setFrom("xxxx@163.com","xxxx");// 设置发件人信息,如邮件格式说明中的发件人,这里会显示为Mailer(xxxx@163.com),Mailer是当做名字显示
    $mail->addAddress($_POST['mail'],'Liang');// 设置收件人信息,如邮件格式说明中的收件人,这里会显示为Liang(yyyy@163.com)
    $mail->IsHTML(true);
    $mail->Subject = $_POST['sub'];// 邮件标题
    $mail->Body = $_POST['con'];// 邮件正文
    if(!$mail->send()){// 发送邮件
      echo "Message could not be sent.";
      echo "Mailer Error: ".$mail->ErrorInfo;// 输出错误信息
    }else{
      echo 'Message has been sent.';
    }
?>

4、完成后就能发送邮件了!

注意:

  • 1、163、qq等邮箱端口不一样。应填写相应的端口。
  • 2、需开启POP3/SMTP/IMAP服务,如图所示。(获取授权密码)

使用PHPMail发送邮箱(163邮箱为例

  • 3、所填密码为授权密码。不是登录密码。

图片alt

  • 4、确保extension=php_openssl.dll开启(在php.ini中设置),将前面的分号去掉。

使用PHPMail发送邮箱(163邮箱为例)