This article mainly introduces the mailbox sending in PHP, which has certain reference value. Now I share it with everyone. Friends in need can refer to it.
In the project, when the user changes the password, a verification code needs to be sent. I went to the user's mailbox, so I took a look and recorded it here.
1. Turn on the SMTP service
The test is using your own qq mailbox. First, you need to turn on the SMTP service of the mailbox. After turning it on, you must remember the authorization code given to you. The authorization code must be the latest Yes, mine has been turned on, and I have secretly memorized the authorization code.
2. Install phpmailer
Use composer and run it in the project directory
composer require phpmailer/phpmailer
After execution, you can see the phpmailer directory in the vendor directory. This is a php function package for sending emails, which is very convenient.
3. Send email function
1 /** 2 * 发送邮件方法 3 * @param string $to:接收者邮箱地址 4 * @param string $title:邮件的标题 5 * @param string $content:邮件内容 6 * @return boolean true:发送成功 false:发送失败 7 */ 8 function sendMail($to,$title,$content){ 9 10 11 //实例化PHPMailer核心类 12 $mail = new \PHPMailer(); 13 //是否启用smtp的debug进行调试 开发环境建议开启 生产环境注释掉即可 默认关闭debug调试模式 14 $mail->SMTPDebug = 1; 15 //使用smtp鉴权方式发送邮件 16 $mail->isSMTP(); 17 //smtp需要鉴权 这个必须是true 18 $mail->SMTPAuth=true; 19 //链接qq域名邮箱的服务器地址 20 $mail->Host = 'smtp.qq.com'; 21 //设置使用ssl加密方式登录鉴权 22 $mail->SMTPSecure = 'ssl'; 23 //设置ssl连接smtp服务器的远程服务器端口号,以前的默认是25,但是现在新的好像已经不可用了 可选465或587 24 $mail->Port = 465; 25 //设置smtp的helo消息头 这个可有可无 内容任意 26 $mail->Helo = 'Hello smtp.qq.com Server'; 27 //设置发件人的主机域 可有可无 默认为localhost 内容任意,建议使用你的域名 28 $mail->Hostname = 'localhost'; 29 //设置发送的邮件的编码 可选GB2312 我喜欢utf-8 据说utf8在某些客户端收信下会乱码 30 $mail->CharSet = 'UTF-8'; 31 //设置发件人姓名(昵称) 任意内容,显示在收件人邮件的发件人邮箱地址前的发件人姓名 32 $mail->FromName = '天下第一帅'; 33 //smtp登录的账号 这里填入字符串格式的qq号即可 34 $mail->Username =''; 35 //smtp登录的密码 使用生成的授权码 你的最新的授权码 36 $mail->Password = ''; 37 //设置发件人邮箱地址 这里填入上述提到的“发件人邮箱” 38 $mail->From = ''; 39 //邮件正文是否为html编码 注意此处是一个方法 不再是属性 true或false 40 $mail->isHTML(true); 41 //设置收件人邮箱地址 该方法有两个参数 第一个参数为收件人邮箱地址 第二参数为给该地址设置的昵称 不同的邮箱系统会自动进行处理变动 这里第二个参数的意义不大 42 $mail->addAddress($to,'测试通知'); 43 //添加多个收件人 则多次调用方法即可 44 // $mail->addAddress('xxx@qq.com','lsgo在线通知'); 45 //添加该邮件的主题 46 $mail->Subject = $title; 47 //添加邮件正文 上方将isHTML设置成了true,则可以是完整的html字符串 如:使用file_get_contents函数读取本地的html文件 48 $mail->Body = $content; 49 50 //为该邮件添加附件 该方法也有两个参数 第一个参数为附件存放的目录(相对目录、或绝对目录均可) 第二参数为在邮件附件中该附件的名称 51 // $mail->addAttachment('./d.jpg','mm.jpg'); 52 //同样该方法可以多次调用 上传多个附件 53 // $mail->addAttachment('./Jlib-1.1.0.js','Jlib.js'); 54 55 $status = $mail->send(); 56 57 //简单的判断与提示信息 58 if($status) { 59 return true; 60 }else{ 61 return false; 62 } 63 }
4. Test effect
In the controller, write a method to test whether the function works well.
1 public function test(){ 2 sendMail('email address','邮件的标题','发送成功'); 3 }
Related recommendations:
php two-dimensional array sorting
The above is the detailed content of Send via PHP email. For more information, please follow other related articles on the PHP Chinese website!

TomodifydatainaPHPsession,startthesessionwithsession_start(),thenuse$_SESSIONtoset,modify,orremovevariables.1)Startthesession.2)Setormodifysessionvariablesusing$_SESSION.3)Removevariableswithunset().4)Clearallvariableswithsession_unset().5)Destroythe

Arrays can be stored in PHP sessions. 1. Start the session and use session_start(). 2. Create an array and store it in $_SESSION. 3. Retrieve the array through $_SESSION. 4. Optimize session data to improve performance.

PHP session garbage collection is triggered through a probability mechanism to clean up expired session data. 1) Set the trigger probability and session life cycle in the configuration file; 2) You can use cron tasks to optimize high-load applications; 3) You need to balance the garbage collection frequency and performance to avoid data loss.

Tracking user session activities in PHP is implemented through session management. 1) Use session_start() to start the session. 2) Store and access data through the $_SESSION array. 3) Call session_destroy() to end the session. Session tracking is used for user behavior analysis, security monitoring, and performance optimization.

Using databases to store PHP session data can improve performance and scalability. 1) Configure MySQL to store session data: Set up the session processor in php.ini or PHP code. 2) Implement custom session processor: define open, close, read, write and other functions to interact with the database. 3) Optimization and best practices: Use indexing, caching, data compression and distributed storage to improve performance.

PHPsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIDstoredinacookie.Here'showtomanagethemeffectively:1)Startasessionwithsession_start()andstoredatain$_SESSION.2)RegeneratethesessionIDafterloginwithsession_regenerate_id(true)topreventsessi

In PHP, iterating through session data can be achieved through the following steps: 1. Start the session using session_start(). 2. Iterate through foreach loop through all key-value pairs in the $_SESSION array. 3. When processing complex data structures, use is_array() or is_object() functions and use print_r() to output detailed information. 4. When optimizing traversal, paging can be used to avoid processing large amounts of data at one time. This will help you manage and use PHP session data more efficiently in your actual project.

The session realizes user authentication through the server-side state management mechanism. 1) Session creation and generation of unique IDs, 2) IDs are passed through cookies, 3) Server stores and accesses session data through IDs, 4) User authentication and status management are realized, improving application security and user experience.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

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),

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
