I believe many students have used thinkphp, and the thinkphp framework itself also has a class library. In this article, we will talk about thinkphp how to use external PHPMailer Class library.
Method 1:
1. Download PHPmailer and extract it to the ThinkPHP\Extend\Vendor folder
2. Use the Vendor method that comes with ThinkPHP to load the third-party class library
3. Configure relevant parameters.
4. In order to facilitate calling at will, create a function for sending emails in common.php, and then you can call this function to send emails.
/** * 邮件发送 */ function sendMail(){ // 载入邮件发送类库 Vendor('PHPMailer.PHPMailerAutoload'); $mail = new PHPMailer; $mail->isSMTP(); //设置PHPMailer使用SMTP服务器发送Email $mail->Host = 'smtp.163.com'; //指定SMTP服务器 可以是smtp.126.com, gmail, qq等服务器 自行查询 $mail->SMTPAuth = true; $mail->CharSet='UTF-8'; //设置字符集 防止乱码 $mail->Username = 'username@163.com'; //发送人的邮箱账户 $mail->Password = 'xxxxxxxxxx'; //发送人的邮箱密码 $mail->Port = 25; //SMTP服务器端口 $mail->From = 'user@admin.com'; //发件人邮箱地址 $mail->FromName = '在路上'; //发件人名称 $mail->addAddress('guest@test.com'); // 收件人邮箱地址 此处可以发送多个 $mail->WordWrap = 50; // 换行字符数 $mail->isHTML(true); // 设置邮件格式为HTML $mail->Subject = '青岛XXX'; //邮件标题 $mail->Body = '尊敬的先生/女士:<br/>非常抱歉,您接受到这封邮件是因您的好友邀请您加入我们的CRM系统体验当中,请点击以下链接注册账户<a href=####>######</a><br/>如您没有相关意向,请忽略'; if(!$mail->send()) { echo '邮件发送失败.'; echo '错误信息: ' . $mail->ErrorInfo; } else { echo '邮件发送成功'; } }
After defining the function, if you want to send emails, directly Call sendMail();
to set the sending content, sending title, and recipients as variables, for example: sendMail($body, $title, $recipient) The recipient can be in the form of an array, in Just loop through the function and you're done!! Some configurations of the SMTP server and sender can be written into the Conf\config.php file, and can be directly called by the C() method.
Method 2:
The first step is to download the PHPMailer.class.php compressed package (there is an attachment download above this article)
After decompression, there will be the following three File:
1.class.pop3.php 2.class.smtp.php 3.PHPMailer.class.php PHPMailer.class.php这个文件就是核心的文件,把这个文件放到ThinkPHP的扩展包下路径如\下:ThinkPHP\Extend\Library
The second step is to create a new common.php file in ThinkPHP, common file and write the following code:
<br/>
The third step is to write the following code in ThinkPHP, the conf.php file:
Note: The following is the test of my own QQ mailbox. The parameters of each mailbox are different. The specific parameters are as follows: The mailbox shall prevail. The stmp of qq mailbox needs to be opened by yourself in the mailbox.
returnarray( //'配置项'=>'配置值' 'MAIL_ADDRESS'=>'799783009@qq.com', // 邮箱地址 'MAIL_LOGINNAME'=>'799783009@qq.com', // 邮箱登录帐号 'MAIL_SMTP'=>'smtp.qq.com', // 邮箱SMTP服务器 'MAIL_PASSWORD'=>'******', // 邮箱密码 'SHOW_PAGE_TRACE'=>true, );
The fourth step is left to use. In ThinkPHP, write the following code in the IndexAction.class.php file:
// 本类由系统自动生成,仅供测试用途 classIndexAction extendsAction { publicfunctionindex(){ if(!empty($_POST['title']) && !empty($_POST['content'])){ if(SendMail("597417106@qq.com",$_POST['tile'],$_POST['content'])) echo'发送成功!'; else echo'发送失败'; } $this->assign('title','测试标题'); $this->display(); } }
Okay, it’s just that simple
Method 2 is considered a standard method, overall They are all placed in the thinkphp class. The editor recommends using the second solution.
php uses the smtp class to easily send emails
When you are still struggling with the fact that the built-in mail() function of php cannot send emails, then you are very lucky now. This article is here Can help you!
php uses the smtp class to send emails, which is a tried and tested method. I have been using it for a long time and basically there has been no problem. In the background of this blog, when the blogger replies to a message, an email with a new reply prompt will be automatically sent to the netizen, which is also implemented using the method in this article.
The method of sending emails using the smtp class is actually very simple and stable. The class has been written by others, and you only need to call it. You can send emails with just a few simple configurations. Are you looking forward to giving it a try?
The following is the core code:
<?php require_once "email.class.php"; //******************** 配置信息 ******************************** $smtpserver = "smtp.126.com";//SMTP服务器 $smtpserverport =25;//SMTP服务器端口 $smtpusermail = "new2008oh@126.com";//SMTP服务器的用户邮箱 $smtpemailto = $_POST['toemail'];//发送给谁 $smtpuser = "new2008oh";//SMTP服务器的用户帐号 $smtppass = "您的邮箱密码";//SMTP服务器的用户密码 $mailtitle = $_POST['title'];//邮件主题 $mailcontent = "<h1 id="POST-content">".$_POST['content']."</h1>";//邮件内容 $mailtype = "HTML";//邮件格式(HTML/TXT),TXT为文本邮件 //************************ 配置信息 **************************** $smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证. $smtp->debug = false;//是否显示发送的调试信息 $state = $smtp->sendmail($smtpemailto, $smtpusermail, $mailtitle, $mailcontent, $mailtype); echo "<div style='width:300px; margin:36px auto;'>"; if($state==""){ echo "对不起,邮件发送失败!请检查邮箱填写是否有误。"; echo "<a href='index.html'>点此返回</a>"; exit(); } echo "恭喜!邮件发送成功!!"; echo "<a href='index.html'>点此返回</a>"; echo "</div>"; ?>
The above are two examples of ThinkPHP using PHPMailer to send emails. For more information, please go to PHP Chinese website search~
Related recommendations:
Teach everyone how to use thinkphp to make short links
Thinkphp's implementation method of executing native SQL statements
Five ways thinkphp can update the database.
Code example for implementing multi-table related query in thinkphp
The above is the detailed content of ThinkPHP Example of using PHPMailer to send emails. For more information, please follow other related articles on the PHP Chinese website!

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

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.

SublimeText3 Mac version
God-level code editing software (SublimeText3)