PHP发送邮件详细说明

这两天琢磨了php得原生发送邮件,发现自带得mail方法不太好用,于是上网查询了好多方法,亲测以下方法能用

源代码都在 我的github

到github上下载

[https://github.com/PHPMailer/PHPMailer](https://github.com/PHPMailer/PHPMailer)
里面有很多文件,但是目前主要就用到两个文件
class.phpmailer.php class.smtp.php

我这个使用得是qq得smtp,如果需要其他得可以自行更改

qq的smtp服务器是:smtp.qq.com 端口号为465 或587
163smtp服务器是:smtp.163.com 端口25

下面是我进行了简单的封装


/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/19
 * Time: 9:12
 */
class SenMail
{
    private $fromMailName='';
    private $formMailPassword='';
   private $mail='';
//密码是授权码
    function __construct($fromMailName = '[email protected]', $fromMailPassword = 'xxxxxxx')
    {
        require_once('class.phpmailer.php');
        require_once('class.smtp.php');
        $this->fromMailName=$fromMailName;
        $this->formMailPassword=$fromMailPassword;
        $this->mail = new PHPMailer();
    }
    /**
     * @param string $arrayMail array集合
     * @param $senName 发件人
     * @param $title 标题
     * @param $body 内容
     */
    function sendMail($arrayMail,$senName, $title, $body)
    {
        //invoke mail() function to send mail
        // mail($toaddress, $subject, $mailcontent, $fromaddress);
        date_default_timezone_set('Asia/Shanghai');//设定时区东八区
        $fromMailPassword=$this->formMailPassword;
        $fromMailName=$this->fromMailName;
        $mail = $this->mail;
        $mail->SMTPDebug = 0;                               // Enable verbose debug output
        $mail->isSMTP();                                      // Set mailer to use SMTP
        $mail->Host = 'smtp.qq.com';  // Specify main and backup SMTP servers
        $mail->SMTPAuth = true;                               // Enable SMTP authentication
        $email = explode("@", $fromMailName);
        $mail->Username = $email[0];                 // SMTP username
        $mail->Password = $fromMailPassword;                            // SMTP password
        $mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
        $mail->Port = 465;
        $mail->CharSet = 'utf-8';
//        echo $mail->Host;                                  // TCP port to connect to
        $mail->setFrom($this->fromMailName, $senName);
        // $mail->addAddress('[email protected]', 'Joe User');     // Add a recipient
        foreach($arrayMail as $v) {
            $mail->addAddress($v);               // Name is optional
        }
//         $mail->addReplyTo('[email protected]', 'Information');
//         $mail->addCC('[email protected]');
//         $mail->addBCC('[email protected]');
//         $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
//         $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
        $mail->isHTML(false);                                  // Set email format to HTML
        $mail->Subject = $title;
        $mail->Body = $body;
        $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
        if (!$mail->send()) {
            echo 'Message could not be sent.';
            echo 'Mailer Error: ' . $mail->ErrorInfo;
        } else {
            echo ucwords('Message has been sent');
        }
    }
}

index.php文件调用


/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/16
 * Time: 15:15
 */
require "SenMail.php";
$e = new SenMail();
$toEmail = array('[email protected]','[email protected]');
$e->sendMail($toEmail,'xx程','测试2号','我是内容');

给新手的

开启qq邮箱的smtp

PHP发送邮件详细说明_第1张图片

密码是授权码,不是qq登录密码

遇到的问题

抛异常:Extesion missing: openssl
这个直接打开php.ini 查找 extension=php_openssl.dll 把注释去掉即可

如果哪里不对欢迎不吝赐教

最后如果对你有帮助请帮我点赞,谢谢

如果觉得有用请点赞,谢谢
首发简书 欢迎关注 我的简书
欢迎关注公众号

PHP传播者

推送最新文章和IT有趣新鲜知识科技

你可能感兴趣的:(php,php,github,源代码,邮件)