Home > Backend Development > PHP Tutorial > ThinkPHP Example of using PHPMailer to send emails

ThinkPHP Example of using PHPMailer to send emails

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

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    = &#39;尊敬的先生/女士:<br/>非常抱歉,您接受到这封邮件是因您的好友邀请您加入我们的CRM系统体验当中,请点击以下链接注册账户<a href=####>######</a><br/>如您没有相关意向,请忽略&#39;;
if(!$mail->send()) {
echo &#39;邮件发送失败.&#39;;
echo &#39;错误信息: &#39; . $mail->ErrorInfo;
} else {
echo &#39;邮件发送成功&#39;;
}
}
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.<br/>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:

<br/>
Copy after login

<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(
//&#39;配置项&#39;=>&#39;配置值&#39;
&#39;MAIL_ADDRESS&#39;=>&#39;799783009@qq.com&#39;, // 邮箱地址
&#39;MAIL_LOGINNAME&#39;=>&#39;799783009@qq.com&#39;, // 邮箱登录帐号
&#39;MAIL_SMTP&#39;=>&#39;smtp.qq.com&#39;, // 邮箱SMTP服务器
&#39;MAIL_PASSWORD&#39;=>&#39;******&#39;, // 邮箱密码
&#39;SHOW_PAGE_TRACE&#39;=>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[&#39;title&#39;]) && !empty($_POST[&#39;content&#39;])){
if(SendMail("597417106@qq.com",$_POST[&#39;tile&#39;],$_POST[&#39;content&#39;]))
echo&#39;发送成功!&#39;;
else
echo&#39;发送失败&#39;;
}
$this->assign(&#39;title&#39;,&#39;测试标题&#39;);
$this->display();
}
}
Copy after login

Okay, it’s just that simple <br/>

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[&#39;toemail&#39;];//发送给谁
$smtpuser = "new2008oh";//SMTP服务器的用户帐号
$smtppass = "您的邮箱密码";//SMTP服务器的用户密码
$mailtitle = $_POST[&#39;title&#39;];//邮件主题
$mailcontent = "<h1>".$_POST[&#39;content&#39;]."</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=&#39;width:300px; margin:36px auto;&#39;>";
if($state==""){
echo "对不起,邮件发送失败!请检查邮箱填写是否有误。";
echo "<a href=&#39;index.html&#39;>点此返回</a>";
exit();
}
echo "恭喜!邮件发送成功!!";
echo "<a href=&#39;index.html&#39;>点此返回</a>";
echo "</div>";
?>
Copy after login

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!

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template