当前位置: 移动技术网 > IT编程>开发语言>PHP > php引入PHPMailer发送邮件

php引入PHPMailer发送邮件

2018年10月16日  | 移动技术网IT编程  | 我要评论

朝阳区邮编,江绵康简历,嵌入式培训骗局

昨天做了一个发送邮件的功能,如果直接用mail()函数,需要拥有自己的邮件服务器,所有引入phpmailer类方便快捷,简单写一下开发步骤:

一、拥有自己的邮箱账号(作为发件人邮箱)

  分两种情况:

  1、如果是企业邮箱,需要用到的参数为邮箱号和密码

  2、如果是个人邮箱,需要用到的参数为邮箱号和授权码(只有开启之后才能通过php代码,控制邮件的自动发送)

 

二、phpmailer的下载

  下载地址:https://github.com/phpmailer/phpmailer

  我这里是用的thinkphp 5,下载后放到vendor目录

三、封装发送邮件方法,设置相关参数

 具体代码如下,这里我用的是企业邮箱,如果是个人邮箱,需更改以下几个参数值:

$mail->host,$mail->port,发件人邮箱密码(授权码)
<?php
/**
 * created by phpstorm.
 * user: administrator
 * date: 2018/10/15 0015
 * time: 13:33
 */

namespace app\common\model;

require vendor_path . 'phpmailer/src/exception.php';
require vendor_path . 'phpmailer/src/phpmailer.php';
require vendor_path . 'phpmailer/src/smtp.php';

use phpmailer\phpmailer\phpmailer;
use phpmailer\phpmailer\smtp;
use phpmailer\phpmailer\exception ;
class mail
{
    //发件人邮箱
    private static $_fromaddress = '***@medpeer.cn';
    //发件人邮箱登录密码(非企业邮箱为授权码)
    private static $_pwd = '***';
    //收件人邮箱
    private static $_toaddress = '***@medpeer.cn';

    /**
     * 发送邮件
     * @author zz
     * @param string $fromaddress 发件人
     * @param string $pwd 发件人登录密码
     * @param string $toaddress 收件人
     * @param string $title 邮件题目
     * @param string $content 邮件内容
     * @return array
     */
    public function sendmail($title,$content,$toaddress = '',$fromaddress = '',$pwd = ''){
        if (empty($title) || empty($content)) {
            return ['result' => false, 'msg' => '参数错误'];
        }
        if (empty($fromaddress)) {
            $fromaddress = self::$_fromaddress;
            $pwd = self::$_pwd;
        }
        if (empty($toaddress)) {
            $toaddress = self::$_toaddress;
        }
        $mail = new phpmailer();
        //告诉phpmailer使用smtp
        $mail->issmtp();
        //启用s​​mtp调试
        // 0 =关闭(供生产使用)
        // 1 =客户端消息
        // 2 =客户端和服务器消息
        $mail->smtpdebug = 2 ;
        //设置邮件服务器的主机名
        $mail->host = 'smtp.exmail.qq.com';
        //使用
        // $ mail-> host = gethostbyname('smtp.gmail.com');
        //如果您的网络不支持smtp over ipv6
        //设置smtp端口号 -  587用于经过身份验证的tls,即rfc4409 smtp提交
        $mail->port = 465;
        //设置加密系统使用 -  ssl(不建议使用)或tls
        $mail->smtpsecure = 'ssl';
        //是否使用smtp身份验证
        $mail->smtpauth = true ;
        //用于smtp身份验证的用户名 - 使用gmail的完整电子邮件地址
        $mail->username = $fromaddress;
        //用于smtp身份验证的密码(企业邮箱的话为登录密码)
        $mail->password = $pwd;
        //设置发送的邮件的编码 可选gb2312 我喜欢utf-8 据说utf8在某些客户端收信下会乱码
        $mail->charset = 'utf-8';
        //设置要从中发送消息的人员
        $mail->setfrom($fromaddress,'***');
        //设置备用回复地址
        //$mail->addreplyto('***@qq.com','腾讯');
        //设置要将消息发送给谁
        $mail->addaddress($toaddress,'***');
        //设置主题行
        $mail->subject = $title;
        //从外部文件中读取html邮件正文,将引用的图像转换为嵌入式图像
        //将html转换为基本的纯文本替代正文
        //$mail->msghtml(file_get_contents(' contents.html '),__dir__);
        //用手动创建的纯文本正文替换
        $mail->altbody  = 'this is the body in plain text for non-html mail clients';
        $mail->body  = $content;
        $result = $mail->send();
        if (!$result) {
            return ['result' => false, 'msg' => $mail->errorinfo];
        } else {
            return ['result' => true, 'msg' => '成功发送邮件消息'] ;
        }
    }
}

  

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网