search
HomeBackend DevelopmentPHP TutorialPHP uses PHPmailer package to send customized emails
PHP uses PHPmailer package to send customized emailsMay 31, 2018 am 11:00 AM
phpphpmailercustom made


First make sure that socket expansion support and openssl are installed in PHP. If you do not use PHPIZE to install


, then download the PHPmailer file on github https://github .com/PHPMailer/PHPMailer Unzip, copy the PHPMailer, Exception and SMTP files under src to the corresponding location, change the namespace inside, and then write the code to send the email

/*发送邮件方法
 *@param $to:接收者 $title:标题 $content:邮件内容
 *@return bool true:发送成功 false:发送失败
 */function sendMail($to,$title,$content){    //引入PHPMailer的核心文件 使用require_once包含避免出现PHPMailer类重复定义的警告
    require_once("phpmailer/class.phpmailer.php"); 
    require_once("phpmailer/class.smtp.php");    //实例化PHPMailer核心类
    $mail = new PHPMailer();    //是否启用smtp的debug进行调试 开发环境建议开启 生产环境注释掉即可 默认关闭debug调试模式
    $mail->SMTPDebug = 1;    //使用smtp鉴权方式发送邮件
    $mail->isSMTP();    //smtp需要鉴权 这个必须是true
    $mail->SMTPAuth=true;    //链接qq域名邮箱的服务器地址
    $mail->Host = 'smtp.qq.com';    //设置使用ssl加密方式登录鉴权
    $mail->SMTPSecure = 'ssl';    //设置ssl连接smtp服务器的远程服务器端口号,以前的默认是25,但是现在新的好像已经不可用了 可选465或587
    $mail->Port = 465;    //设置smtp的helo消息头 这个可有可无 内容任意
    // $mail->Helo = 'Hello smtp.qq.com Server';

    //设置发件人的主机域 可有可无 默认为localhost 内容任意,建议使用你的域名
    $mail->Hostname = 'http://www.lsgogroup.com';    //设置发送的邮件的编码 可选GB2312 我喜欢utf-8 据说utf8在某些客户端收信下会乱码
    $mail->CharSet = 'UTF-8';    //设置发件人姓名(昵称) 任意内容,显示在收件人邮件的发件人邮箱地址前的发件人姓名
    $mail->FromName = 'LSGO实验室';    //smtp登录的账号 这里填入字符串格式的qq号即可
    $mail->Username ='12345678@qq.com';    //smtp登录的密码 使用生成的授权码(就刚才叫你保存的最新的授权码)
    $mail->Password = 'sqyofzbqlfkntbncl';    //设置发件人邮箱地址 这里填入上述提到的“发件人邮箱”
    $mail->From = '12345678@qq.com';    //邮件正文是否为html编码 注意此处是一个方法 不再是属性 true或false
    $mail->isHTML(true); 

    //设置收件人邮箱地址 该方法有两个参数 第一个参数为收件人邮箱地址 第二参数为给该地址设置的昵称 不同的邮箱系统会自动进行处理变动 这里第二个参数的意义不大
    $mail->addAddress($to,'lsgo在线通知');    //添加多个收件人 则多次调用方法即可
    // $mail->addAddress('xxx@163.com','lsgo在线通知');

    //添加该邮件的主题
    $mail->Subject = $title;    //添加邮件正文 上方将isHTML设置成了true,则可以是完整的html字符串 如:使用file_get_contents函数读取本地的html文件
    $mail->Body = $content;    //为该邮件添加附件 该方法也有两个参数 第一个参数为附件存放的目录(相对目录、或绝对目录均可) 第二参数为在邮件附件中该附件的名称
    // $mail->addAttachment('./d.jpg','mm.jpg');
    //同样该方法可以多次调用 上传多个附件
    // $mail->addAttachment('./Jlib-1.1.0.js','Jlib.js');

    $status = $mail->send();    //简单的判断与提示信息
    if($status) {        return true;
    }else{        return false;
    }
}

Related recommendations:

PHP uses the binary heap to implement the TopK-algorithm method

php uses fsockopen GET/POST to submit the form and upload the file with detailed graphics and text

The above is the detailed content of PHP uses PHPmailer package to send customized 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
PHP使用PHPMailer发送多人邮件的方法和步骤PHP使用PHPMailer发送多人邮件的方法和步骤May 22, 2023 pm 06:10 PM

在Web应用程序中,往往需要将邮件一次性发送给多个收件人。PHP是一种很流行的Web开发语言,而PHPMailer是一种常见的发送邮件的PHP类库。PHPMailer提供了丰富的接口,使得在PHP应用程序中发送邮件变得更加方便和易于使用。在本篇文章中,我们将介绍如何使用PHPMailer向多个收件人发送邮件的方法和步骤。下载PHPMailer首先需要在官网(

掌握PHP和PHPMAILER:如何实现邮件发送的自动回复功能?掌握PHP和PHPMAILER:如何实现邮件发送的自动回复功能?Jul 22, 2023 am 11:57 AM

掌握PHP和PHPMAILER:如何实现邮件发送的自动回复功能?在现代社会中,电子邮件成为了人们日常沟通的重要方式之一。许多网站或者企业都需要通过邮件与用户进行沟通和交流,并且自动回复邮件变得非常重要。本文将介绍如何使用PHP和PHPMailer库实现邮件发送的自动回复功能。第一步:获取用户的邮件信息首先,我们需要获取用户的邮件信息。在网站或者应用程序中,用

如何使用PHP和PHPMAILER发送带有内嵌图片的HTML邮件?如何使用PHP和PHPMAILER发送带有内嵌图片的HTML邮件?Jul 22, 2023 am 11:29 AM

如何使用PHP和PHPMAILER发送带有内嵌图片的HTML邮件?HTML邮件是一种更加丰富和个性化的邮件形式,可以在邮件中插入图片、链接和样式。而内嵌图片是指在HTML邮件中直接将图片作为邮件的一部分发送,而不是通过附件方式发送。在PHP中,我们可以借助PHPMAILER来发送带有内嵌图片的HTML邮件。PHPMAILER是一个功能强大的PHP邮件发送类库

PHP和PHPMAILER:如何实现邮件发送的防垃圾邮件功能?PHP和PHPMAILER:如何实现邮件发送的防垃圾邮件功能?Jul 22, 2023 am 11:46 AM

PHP和PHPMAILER:如何实现邮件发送的防垃圾邮件功能?引言:在互联网时代,电子邮件已经成为了我们日常生活和工作中不可或缺的一部分。然而,随着电子邮件的普及和使用,垃圾邮件问题日益严重,给用户带来了诸多困扰。为了解决这个问题,本文将介绍如何利用PHP和PHPMailer库实现邮件发送的防垃圾邮件功能。一、了解垃圾邮件垃圾邮件(Spam),指的是那些未经

PHP使用PHPMailer库发送附件邮件的方法和注意事项PHP使用PHPMailer库发送附件邮件的方法和注意事项May 21, 2023 pm 06:12 PM

PHP使用PHPMailer库发送附件邮件的方法和注意事项邮件在现代生活中已经成为了非常重要的一种通信方式。在很多开发项目中,我们需要使用代码自动发送邮件,这时候PHPMailer库就是我们的不二之选。PHPMailer是一个专门用于PHP发送邮件的库。它可以方便地发送邮件,包括HTML格式的邮件和附件。本文将着重介绍PHPMailer库中如何发送带附件的邮

如何在CakePHP中使用PHPMailer?如何在CakePHP中使用PHPMailer?Jun 04, 2023 pm 01:10 PM

CakePHP是一个基于MVC模式的PHP开源框架,旨在为开发者提供高效、可扩展、易于维护的Web应用程序开发环境。其中,邮件功能一直是Web应用程序的重要组成部分之一。为了方便开发者使用邮件功能,在CakePHP中已经封装了PHPMailer类库。PHPMailer是一款常用的邮件发送类库,支持发送HTML邮件、附件、抄送、邮件队列和SMTP验证等功能。本

如何使用PHP和PHPMAILER发送HTML格式的邮件?如何使用PHP和PHPMAILER发送HTML格式的邮件?Jul 22, 2023 am 10:14 AM

如何使用PHP和PHPMailer发送HTML格式的邮件?随着互联网的发展,电子邮件已经成为人们日常沟通的重要工具。在开发网站和应用程序时,我们常常需要使用PHP和PHPMailer来发送邮件。本文将为您介绍如何使用PHP和PHPMailer发送HTML格式的邮件,并提供相应的代码示例。第一步:准备工作在开始之前,您需要确保已经安装了PHP和PHPMaile

如何使用PHP和PHPMAILER实现邮件模板的定制化?如何使用PHP和PHPMAILER实现邮件模板的定制化?Jul 22, 2023 pm 05:01 PM

如何使用PHP和PHPMailer实现邮件模板的定制化?随着电子邮件的普及和广泛应用,定制化邮件模板成为了一种非常重要的需求。通过使用PHP和PHPMailer,我们可以轻松地实现邮件模板的定制化,灵活地应对不同的邮件发送需求。本文将介绍如何使用PHP和PHPMailer来设计和发送定制化邮件模板,并提供相应的代码示例。步骤一:安装和配置PHPMailer首

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment