This article is mainly for everyone to introduce the Phpmailer send mail function, with a certain reference value, interested in small partners can refer to
PHP built-in mail functions are not easy to use, in addition to other languages, bloggers prefer the object-oriented package management mode, so Phpmailer became my first choice to send mail with PHP, here to share to you.
Library Import
Here we use composer for package management, the following is the JSON file:
{ "name": "", "description": "Test The Mail", "require": { "Phpmailer/phpmailer": "*" }}
Using the sample
<?php//include ("./vendor/autoload.php"); include ("./vendor/phpmailer/ Phpmailer/class.phpmailer.php "); include ("./vendor/phpmailer/phpmailer/class.smtp.php "); function Postmail ($to, $ Subject= "", $body = "") {$mail = new Phpmailer (); #创建PHPMailer实例 $mail->charset = ' utf-8 '; #设置编码形式, the default is Iso-8859-1 $mail ->ISSMTP (); #表示使用SMTP服务 $mail->smtpauth = true; #使用SMTP认证 $mail->host = ' smtp.163.com '; #SMTP主机名, here take 163 for example $mail->username = "xxxx@163.com"; #SMTP服务的用户名 $mail->password = "xxxx"; #SMTP用户的密码 (now more in the form of authorization codes) #下面的部分是用户可见的 $mail->setfrom ("xxxx@163.com", "userName"); #第二个参数, the user sees the sender's name, can be arbitrarily changed $mail->subject = $subject; #邮件主题 $mail->msghtml ($body); #邮件内容, this is $mail->addaddress ($to, ") in HTML form; #地址, the second parameter is the recipient, when the sending object is named ' ', the 163 test result is that the SMTP service will change it to ' I ' $mail->addattachment ('./php.png '); #添加附件 #发送 if ($mail->send ()) {echo "Mail sent successfully"; }else {echo "failed to send mail"; }}postmail (' xxxx@qq.com ', ' test ', ' How are you ');?
Test results
Use 163 as the sender, sent to the QQ mailbox, 163 mailboxes are passed the test.