Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the function of sending mail with phpmailer in PHP

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "how PHP uses phpmailer to achieve mail sending function". The editor shows you the operation process through an actual case. The operation method is simple and fast, and it is practical. I hope that this article "how PHP uses phpmailer to achieve mail sending function" can help you solve the problem.

Baidu basically introduced two methods.

The first is that PHP sends mail through the SMTP server of QQ Mail or NetEase's mailbox.

The second is to use phpmailer to send mail.

Note that the second method also requires your mailbox to open the SMTP service.

The final effect of how to enable the SMTP service of a mailbox is shown below:

During the opening process, the page will prompt you for an authorization password for calling SMTP, as shown in the following figure. This password must be remembered. If not, it can be generated again. I have opened two SMTP services above, and the final authorization password shall prevail.

The first method has been tried and the error is as follows:

Trying to smtp.qq.com:587 220smtp.qq.com Esmtp QQ Mail Server Connected to relay host smtp.qq.com > HELO localhost 250smtp.qq.com > AUTH LOGIN ODA1Nzk1OTU1QHFxLmNvbQ== 530Must issue a STARTTLS command first. Error: Remote host returned "530 Must issue a STARTTLS command first." Error: Error occurred while sending HELO command. Error: Cannot send email to Disconnected from remote host

Baidu did not find a clear answer for a while. If you have encountered such a problem, please leave a message below and we can communicate.

I'm using the second method here.

I'm not going to talk about the specific advantages and disadvantages of Phpmail. If you want to know more, please help yourself to Baidu.

The first step is to get phpmailer

PHPMailer project address: https://github.com/PHPMailer/PHPMailer; use the git command to clone to the local, or directly in the lower right of the project page click "Download ZIP" to get the complete PHPMailer code package, and then decompress it locally.

Step 2: enable server support

To use phpmailer, our server needs to enable sockets and openssl services.

The third step is to upload phpmailer to our server.

I'm using the thinkphp5 framework here, and I uploaded it to the extend directory under the root directory.

There is no need to take this step alone, but not all the project files we downloaded from github are useful, we can just upload a few master files to the server.

The fourth step is to encode the public method of sending mail and its calling method.

Public methods for sending mail:

/ * send mail method * @ param $to: recipient $title: title $content: message content * @ return bool true: send success false: send failed * / function sendMail ($to,$title,$content) {/ / introduce the core file of PHPMailer using require_once to include warnings require_once ("phpmailer/class.phpmailer.php") to avoid duplicate definitions of the PHPMailer class; require_once ("phpmailer/class.smtp.php") / / instantiate the PHPMailer core class $mail = new PHPMailer (); / / whether to enable debug of smtp for debugging the development environment it is recommended to turn off the debug debug mode $mail- > SMTPDebug = 1 by default by opening the production environment and commenting it off; / / use smtp authentication to send an email $mail- > isSMTP (); / / the smtp needs authentication which must be true $mail- > SMTPAuth=true / / the server address of the link qq domain name mailbox $mail- > Host = 'smtp.qq.com'; / / set login authentication using ssl encryption $mail- > SMTPSecure =' ssl'; / / set the remote server port number of ssl to connect to the smtp server. The default was 25, but now the new one seems to be unavailable. Optional 465 or 587$ mail- > Port = 465 / / set the smtp helo header / / $mail- > Helo = 'Hello smtp.qq.com Server'; / / set the sender's host domain to localhost content by default. It is recommended to use your domain name $mail- > Hostname =' https://guanchao.site'; / / set the coding of the sent message optional GB2312 I like utf-8 it is said that utf8 will garbled $mail- > CharSet = 'UTF-8'; / / set any content of the sender's name (nickname) under some client receiving mail, and display the sender's name in front of the sender's email address $mail- > FromName =' LSGO Lab' / / smtp login account enter the qq number in string format here $mail- > Username = '805795955 authorization qq.compose; / / the password for smtp login uses the generated authorization code (the latest authorization code that I just asked you to save) $mail- > Password =' *' / / set the email address of the sender. Fill in the above mentioned "sender mailbox" $mail- > From = '805795955 email qq.compose; / / whether the email body is html encoded Note here is a method that is no longer the property true or false $mail- > isHTML (true). / / set the recipient mailbox address this method has two parameters, the first parameter is the recipient mailbox address, the second parameter is the nickname set to the address, the different mailbox system will automatically deal with the changes, where the second parameter does not have much meaning $mail- > addAddress (blog online notification in $to,' time') / / if you add multiple recipients, you can call the method multiple times / / $mail- > addAddress ('* @ qq.com',' blog online notification'); / / add the subject of the email $mail- > Subject = $title / / if isHTML is set to true at the top of the message body, it can be a complete html string, such as: use the file_get_contents function to read the local html file $mail- > Body = $content / / add an attachment to the message the method also has two parameters. The first parameter is the directory where the attachment is stored (either the relative directory or the absolute directory). The second parameter is the name of the attachment in the email attachment / / $mail- > addAttachment ('. / d.jpg.jpg.jpg`) / / similarly, this method can call multiple attachments / / $mail- > addAttachment ('. / Jlib-1.1.0.js','Jlib.js'); $status = $mail- > send (); / / simple judgment and prompt information if ($status) {return true;} else {return false;}}

Call method:

I am just a simple call here. Please decide what you need according to your own needs.

Public function sendEmail () {$flag = sendMail ('805795955' blog online notice', 'Welcome to the blog in time'); if ($flag) {echo true;} else {echo false;} about "how to use phpmailer to send mail in PHP" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report