Home  >  Article  >  Backend Development  >  PHP plugin PHPMailer sends email

PHP plugin PHPMailer sends email

高洛峰
高洛峰Original
2017-03-01 16:22:551633browse

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:

PHP plugin PHPMailer sends email

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

PHP plugin PHPMailer sends email

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:

PHP plugin PHPMailer sends email

/**
 * 功能:邮件发送函数
 * @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(
 //其他配置项省略......
 // 配置邮件发送服务器
 &#39;MAIL_SMTP&#39;   => TRUE,
 &#39;MAIL_HOST&#39;   => &#39;smtp.163.com&#39;,   //邮件发送SMTP服务器
 &#39;MAIL_SMTPAUTH&#39; => TRUE,
 &#39;MAIL_USERNAME&#39; => &#39;123***@163.com&#39;,  //SMTP服务器登陆用户名
 &#39;MAIL_PASSWORD&#39; => &#39;123456abc&#39;,    //SMTP服务器登陆密码
 &#39;MAIL_SECURE&#39;   => &#39;tls&#39;,
 &#39;MAIL_CHARSET&#39;  => &#39;utf-8&#39;,
 &#39;MAIL_ISHTML&#39;   => TRUE,
 &#39;MAIL_FROMNAME&#39; => &#39;某某网站客户&#39;,
 );

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(&#39;vsiryxm@qq.com&#39;,&#39;你好!邮件标题&#39;,&#39;这是一篇测试邮件正文!&#39;)){
   echo &#39;发送成功!&#39;;
  }
  else{
   echo &#39;发送失败!&#39;;
  }
 }
 }

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!

Statement:
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