ThinkPHP Example of using PHPMailer to send emails

韦小宝
Release: 2023-03-17 16:36:01
Original
2057 people have browsed it

I believe many students have usedthinkphp, and thethinkphpframework itself also has a class library. In this article, we will talk aboutthinkphphow 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 = '尊敬的先生/女士:
非常抱歉,您接受到这封邮件是因您的好友邀请您加入我们的CRM系统体验当中,请点击以下链接注册账户######
如您没有相关意向,请忽略'; if(!$mail->send()) { echo '邮件发送失败.'; echo '错误信息: ' . $mail->ErrorInfo; } else { echo '邮件发送成功'; } }
Copy after login

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
Copy after login

The second step is to create a new common.php file in ThinkPHP, common file and write the following code:


Copy after login


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, );
Copy after login

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(); } }
Copy after login

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:

".$_POST['content']."";//邮件内容 $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 "
"; if($state==""){ echo "对不起,邮件发送失败!请检查邮箱填写是否有误。"; echo "点此返回"; exit(); } echo "恭喜!邮件发送成功!!"; echo "点此返回"; echo "
"; ?>
Copy after login

The above are two examples of ThinkPHP usingPHPMailer 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!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!