Home  >  Article  >  PHP Framework  >  ThinkPHP uses PHPMailer to send emails

ThinkPHP uses PHPMailer to send emails

尚
Original
2020-04-04 09:05:213512browse

ThinkPHP uses PHPMailer to send emails

phpMailer is a very powerful php email class. You can set the sending email address, reply address, email subject, html web page, upload attachments, and it is very convenient to use.

Thinkphp3.2 PHPMailer sends emails combined with QQ enterprise mailbox to send emails
Download the attachment PHPMailer and extract it to ThinkPHP\Library\Vendor

Create a new function.php in the Common folder

/**
 * 邮件发送函数
 */
    function sendMail($to, $title, $content) {
     
        Vendor('PHPMailer.PHPMailerAutoload');     
        $mail = new PHPMailer(); //实例化
        $mail->IsSMTP(); // 启用SMTP
        $mail->Host=C('MAIL_HOST'); //smtp服务器的名称(这里以QQ邮箱为例)
        $mail->SMTPAuth = C('MAIL_SMTPAUTH'); //启用smtp认证
        $mail->Username = C('MAIL_USERNAME'); //你的邮箱名
        $mail->Password = C('MAIL_PASSWORD') ; //邮箱密码
        $mail->From = C('MAIL_FROM'); //发件人地址(也就是你的邮箱地址)
        $mail->FromName = C('MAIL_FROMNAME'); //发件人姓名
        $mail->AddAddress($to,"尊敬的客户");
        $mail->WordWrap = 50; //设置每行字符长度
        $mail->IsHTML(C('MAIL_ISHTML')); // 是否HTML格式邮件
        $mail->CharSet=C('MAIL_CHARSET'); //设置邮件编码
        $mail->Subject =$title; //邮件主题
        $mail->Body = $content; //邮件内容
        $mail->AltBody = "这是一个纯文本的身体在非营利的HTML电子邮件客户端"; //邮件正文不支持HTML的备用显示
        return($mail->Send());
    }

Add configuration file

config.php

// 配置邮件发送服务器
    'MAIL_HOST' =>'smtp.exmail.qq.com',//smtp服务器的名称
    'MAIL_SMTPAUTH' =>TRUE, //启用smtp认证
    'MAIL_USERNAME' =>'jufengjituan@gsjfjt.com',//你的邮箱名
    'MAIL_FROM' =>'jufengjituan@gsjfjt.com',//发件人地址
    'MAIL_FROMNAME'=>'聚丰集团',//发件人姓名
    'MAIL_PASSWORD' =>'******',//邮箱密码
    'MAIL_CHARSET' =>'utf-8',//设置邮件编码
    'MAIL_ISHTML' =>TRUE, // 是否HTML格式邮件

Finally, use PHPMailer to send emails

<form action="__URL__/add" method="post" enctype="multipart/form-data">
    邮箱:<input  type="text" id="mail" name="mail"/>
    标题:<input  type="text" id="title" name="title"/>
    内容<input  type="text" id="content" name="content"/>
    <input class="button" type="submit" value="发送" style="margin: 0 auto;display: block;"/>
 </form>
public function add(){    
            if(SendMail($_POST[&#39;mail&#39;],$_POST[&#39;title&#39;],$_POST[&#39;content&#39;]))
                $this->success(&#39;发送成功!&#39;);
            else
                $this->error(&#39;发送失败&#39;);
}

PHPMailer download address: https://github.com/PHPMailer/PHPMailer

Recommended tutorial: thinkphp tutorial

The above is the detailed content of ThinkPHP uses PHPMailer to send emails. For more information, please follow other related articles on the PHP Chinese website!

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