Home  >  Article  >  Backend Development  >  PHPMAILER implements PHP email function

PHPMAILER implements PHP email function

不言
不言Original
2018-04-18 14:52:592126browse

This article mainly introduces PHPMAILER to implement PHP email function in detail. It has certain reference value. Interested friends can refer to it.

The example in this article shares with everyone PHPMAILER to implement PHP email function. The specific code of the email function is for your reference. The specific content is as follows

Step one:Open the website to download PHPMailer. PHPMailer requires PHP's sockets extension support, and logging in to the QQ mailbox SMTP server is required. Encrypted through SSL, PHP must also include openssl support.

Step 2: Use the phpinfo() function to view the socket and openssl extension information (wamp server enables this extension by default).

openssl If it is not enabled, please open the php.ini file to enable it.

First check whether extension=php_openssl.dll in php.ini exists. If it exists, remove the preceding comment character ';' , If this line does not exist, add extension=php_openssl.dll.

PHPMailer core file

##Step 3: QQ Email Settings

All mainstream mailboxes support the SMTP protocol, but not all mailboxes are enabled by default. You can manually enable it in the mailbox settings.

After providing the account and password, the third-party service can log in to the SMTP server and use it to control the mail transfer method.

Step 4: Start the SMTP service

Select the IMAP/SMTP service and click to start the service

Step 5:Verify password

Send the text message "Configure email client" to 1069-0700-69

Step 6: Get the authorization code

##SMTP server authentication password, which needs to be kept properly (PS: There are no spaces in the password)

Step 7:

PHP Send Email
Basic Code

The following code demonstrates the use of PHPMailer. Pay attention to the configuration process of the PHPMailer instance.

// 引入PHPMailer的核心文件
require_once("PHPMailer/class.phpmailer.php");
require_once("PHPMailer/class.smtp.php");
 
// 实例化PHPMailer核心类
$mail = new PHPMailer();
// 是否启用smtp的debug进行调试 开发环境建议开启 生产环境注释掉即可 默认关闭debug调试模式
$mail->SMTPDebug = 1;
// 使用smtp鉴权方式发送邮件
$mail->isSMTP();
// smtp需要鉴权 这个必须是true
$mail->SMTPAuth = true;
// 链接qq域名邮箱的服务器地址
$mail->Host = 'smtp.qq.com';
// 设置使用ssl加密方式登录鉴权
$mail->SMTPSecure = 'ssl';
// 设置ssl连接smtp服务器的远程服务器端口号
$mail->Port = 465;
// 设置发送的邮件的编码
$mail->CharSet = 'UTF-8';
// 设置发件人昵称 显示在收件人邮件的发件人邮箱地址前的发件人姓名
$mail->FromName = '发件人昵称';
// smtp登录的账号 QQ邮箱即可
$mail->Username = '12345678@qq.com';
// smtp登录的密码 使用生成的授权码
$mail->Password = '**********';
// 设置发件人邮箱地址 同登录账号
$mail->From = '12345678@qq.com';
// 邮件正文是否为html编码 注意此处是一个方法
$mail->isHTML(true);
// 设置收件人邮箱地址
$mail->addAddress('87654321@qq.com');
// 添加多个收件人 则多次调用方法即可
$mail->addAddress('87654321@163.com');
// 添加该邮件的主题
$mail->Subject = '邮件主题';
// 添加邮件正文
$mail->Body = &#39;<h1>Hello World</h1>&#39;;
// 为该邮件添加附件
$mail->addAttachment(&#39;./example.pdf&#39;);
// 发送邮件 返回状态
$status = $mail->send(); 

I use the code in thinkphp5.0

/**
* 邮件发送
* @param $to 接收人
* @param string $subject 邮件标题
* @param string $content 邮件内容(html模板渲染后的内容)
* @throws Exception
* @throws phpmailerException
*/
function send_email($to,$subject=&#39;&#39;,$content=&#39;&#39;){
  vendor(&#39;phpmailer.PHPMailerAutoload&#39;);
//require_once &#39;vendor/phpmailer/PHPMailerAutoload.php&#39;;
  $mail = new PHPMailer;
  $arr = db(&#39;config&#39;)->where(&#39;inc_type&#39;,&#39;smtp&#39;)->select();
  $config = convert_arr_kv($arr,&#39;name&#39;,&#39;value&#39;);
  $mail->CharSet = &#39;UTF-8&#39;; //设定邮件编码,默认ISO-8859-1,如果发中文此项必须设置,否则乱码
  $mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
  $mail->SMTPDebug = 0;
//调试输出格式
//$mail->Debugoutput = &#39;html&#39;;
//smtp服务器
  $mail->Host = $config[&#39;smtp_server&#39;];
//端口 - likely to be 25, 465 or 587
  $mail->Port = $config[&#39;smtp_port&#39;];
 
 
 
  if($mail->Port === 465) $mail->SMTPSecure = &#39;ssl&#39;;// 使用安全协议
//Whether to use SMTP authentication
  $mail->SMTPAuth = true;
//发送邮箱
  $mail->Username = $config[&#39;smtp_user&#39;];
//密码
  $mail->Password = $config[&#39;smtp_pwd&#39;];
//Set who the message is to be sent from
  $mail->setFrom($config[&#39;smtp_user&#39;],$config[&#39;email_id&#39;]);
//回复地址
//$mail->addReplyTo(&#39;replyto@example.com&#39;, &#39;First Last&#39;);
//接收邮件方
  if(is_array($to)){
    foreach ($to as $v){
      $mail->addAddress($v);
    }
  }else{
    $mail->addAddress($to);
  }
 
 
 
  $mail->isHTML(true);// send as HTML
//标题
  $mail->Subject = $subject;
//HTML内容转换
  $mail->msgHTML($content);
//Replace the plain text body with one created manually
//$mail->AltBody = &#39;This is a plain-text message body&#39;;
//添加附件
//$mail->addAttachment(&#39;images/phpmailer_mini.png&#39;);
//send the message, check for errors
  return $mail->send();
}

Related recommendations:


How PHP uses PHPMailer to send emails and share them

PHP uses PHPMailer to send emails and attachments

PHPMailer implements the function of sending emails using QQ mailbox


##

The above is the detailed content of PHPMAILER implements PHP email function. For more information, please follow other related articles on 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