Home  >  Article  >  Backend Development  >  ThinkPHP How to send emails through PHPMailer to corporate mailboxes

ThinkPHP How to send emails through PHPMailer to corporate mailboxes

php中世界最好的语言
php中世界最好的语言Original
2017-12-20 14:33:593882browse

Maybe you need to send emails to website users due to work needs, but with so many customers you cannot send them manually one by one, so I will bring you a good way to use PHP to help you solve this tedious problem. task.

I have been using the 163 mailbox to send files that need to be processed, but if the operation is too frequent [or there are other problems], it will cause the sending to fail. On the contrary, it seems that there has not been a similar situation when using QQ, but The QQ mailbox configuration is slightly different from that of 163. I’ll post it here to prevent my friends from stepping into a trap. I won’t go into more details below. Let’s take a look at the detailed introduction.

Advantages of PHPMailer:

Can run on any platform

Supports SMTP authentication

Specify multiple recipients when sending emails , Cc address, Bcc address and reply address; Note: Adding Cc and Bcc is only supported by SMTP mode under the win platform

Supports multiple email encodings including: 8bit, base64, binary and quoted-printable

Supports redundant SMTP servers, that is, you can specify the main SMTP server address or only the backup SMTP server

Supports emails with attachments, and you can add attachments in any format to emails—of course it’s up to you The server has enough bandwidth to support

custom email header information, which is similar to sending header information in PHP through the header function

supports making the email body into HTMl content , then you can insert pictures into the email body

Flexible debug support

Tested and compatible SMTP servers include: Sendmail, qmail, Postfix, Imail, Exchange, etc.

1. Download PHPMailer

Because there are many versions, I will share the packaged version I use here. Friends who use other versions can use their own

Download address: http://xiazai.jb51.net/201711/yuanma/phpmailer(jb51.net).rar

2. Place PHPMailer

I I created a folder named [Plugin] in the root directory specifically for various plug-ins, and then directly placed PHPMailer under the Plugin folder


3. Let’s code

I will put my code here, you can modify it as needed

function sendMail($to,$title,$content){
 require('./Plugin/phpmailer/class.phpmailer.php');
 try {
  $mail = new \PHPMailer(true);
  $mail->IsSMTP();
  $mail->SMTPSecure = 'ssl';
  $mail->CharSet = 'UTF-8';
  $mail->SMTPAuth = true; //开启认证
  $mail->Port = 465; //网易为25
  $mail->Host = "smtp.qq.com";
  $mail->Username = "******"; //qq此处为邮箱前缀名 163为邮箱名
  $mail->Password = "******";
  $mail->AddReplyTo("******@qq.com", "******");//回复地址
  $mail->From = "******@qq.com";
  $mail->FromName = '******';
  $mail->AddAddress($to);
  $mail->Subject = $title;
  $mail->Body = $content;
  $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; //当邮件不支持html时备用显示
  $mail->WordWrap = 80; // 设置每行字符串的长度
//$mail->AddAttachment("f:/test.png"); //可以添加附件
  $mail->IsHTML(true);
  $mail->Send();
  echo '邮件已发送';
 } catch (phpmailerException $e) {
  echo "邮件发送失败:" . $e->errorMessage();
 }
 }


Copy the above code changes directly into your The control machine can

Things to note:

$mail->Port = 465; //网易163 25
$mail->Host = "smtp.qq.com";
$mail->Username = "**"; //qq此处为邮箱前缀名 163为邮箱名
$mail->Password = "******";


In this case, NetEase’s Port is 25

Username, NetEase can directly use the email name. For QQ mailbox, please enter the email prefix [user name] "eg.1214982635@qq.com Please enter 1214982635"

Password NetEase directly Enter your email password [if you have an authorization code, enter the authorization code], please enter the authorization code via QQ, and send a text message to generate


I believe you have mastered it after reading these cases. Method, for more exciting information, please pay attention to other related articles on the php Chinese website!

Related reading:

php uses git deployment environment

Some use cases of Git

Detailed explanation of javascript data types and git usage code

The above is the detailed content of ThinkPHP How to send emails through PHPMailer to corporate mailboxes. 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