Home >Backend Development >PHP Tutorial >PHP plugin PHPMailer sends email
This article mainly introduces the function of sending emails in PHP in detail. A PHPMailer plug-in can easily realize the function of sending emails, which has certain reference value. Interested friends can refer to it
The examples of this article are We have shared the specific code for sending emails in ThinkPHP3.2.3 for your reference. The specific content is as follows
First step: Download a PHPMailer plug-in online. After downloading and decompressing, we only need to use two of them here. files, as shown below:
Place the two files class.phpmailer.php and class.smtp.php respectively to
ThinkPHP/Library/Vendor/ PHPMailer/class.phpmailer.php (note the case)
ThinkPHP/Library/Vendor/PHPMailer/class.smtp.php
Note: The current placement is thinkPHP's default third-party class library directory. If it is defined in index.php such as define('VENDOR_PATH',APP_PATH.'Common/Vendor/'); then the file placement path must be the same as it to avoid class 'PHPMailer' not found situation.
2. Create a user-defined function file Application/Home/Common/function.php and place the following functions:
/** * 功能:邮件发送函数 * @param string $to 目标邮箱 * @param string $subject 邮件主题(标题) * @param string $to 邮件内容 * @return bool true */ function sendMail($to, $subject, $content) { vendor('PHPMailer.class#smtp'); vendor('PHPMailer.class#phpmailer'); //注意这里的大小写哦,不然会出现找不到类,PHPMailer是文件夹名字,class#phpmailer就代表class.phpmailer.php文件名 $mail = new PHPMailer(); // 装配邮件服务器 if (C('MAIL_SMTP')) { $mail->IsSMTP(); } $mail->Host = C('MAIL_HOST'); //这里的参数解释见下面的配置信息注释 $mail->SMTPAuth = C('MAIL_SMTPAUTH'); $mail->Username = C('MAIL_USERNAME'); $mail->Password = C('MAIL_PASSWORD'); $mail->SMTPSecure = C('MAIL_SECURE'); $mail->CharSet = C('MAIL_CHARSET'); // 装配邮件头信息 $mail->From = C('MAIL_USERNAME'); $mail->AddAddress($to); $mail->FromName = C('MAIL_FROMNAME'); $mail->IsHTML(C('MAIL_ISHTML')); // 装配邮件正文信息 $mail->Subject = $subject; $mail->Body = $content; // 发送邮件 if (!$mail->Send()) { return FALSE; } else { return TRUE; } }
3. In the above function, the C method is used to load some configuration information, so we have to add the following configuration information to the configuration file (default/Application/Home/Conf/config.php):
<?php return array( //其他配置项省略...... // 配置邮件发送服务器 'MAIL_SMTP' => TRUE, 'MAIL_HOST' => 'smtp.163.com', //邮件发送SMTP服务器 'MAIL_SMTPAUTH' => TRUE, 'MAIL_USERNAME' => '123***@163.com', //SMTP服务器登陆用户名 'MAIL_PASSWORD' => '123456abc', //SMTP服务器登陆密码 'MAIL_SECURE' => 'tls', 'MAIL_CHARSET' => 'utf-8', 'MAIL_ISHTML' => TRUE, 'MAIL_FROMNAME' => '某某网站客户', );
4. Start calling. Assuming access through the URL /?m=home&c=index&a=send, then we will correspondingly call it in Application/Home/Controller/IndexController.class Add the method to the .php file as follows:
<?php namespace Home\Controller; use Think\Controller; class IndexController extends Controller { public function index(){ } public function send(){ if(sendMail('vsiryxm@qq.com','你好!邮件标题','这是一篇测试邮件正文!')){ echo '发送成功!'; } else{ echo '发送失败!'; } } }
The above is the entire content of this article. I hope it will be helpful to everyone’s learning, and I hope everyone will support PHP. Chinese website.
For more articles related to the PHP plug-in PHPMailer sending emails, please pay attention to the PHP Chinese website!