本篇记录的是我的发邮件的代码整理。用phpmailer实现发邮件功能
下载phpmailer地址 https://github.com/phpmailer/phpmailer
<?phpuse phpmailer\phpmailer\phpmailer;use phpmailer\phpmailer\exception;require './phpmailer/src/exception.php';require './phpmailer/src/phpmailer.php';require './phpmailer/src/smtp.php';$mail = new phpmailer(true); // passing `true` enables exceptionstry { //server settings $mail->smtpdebug = 2; // enable verbose debug output $mail->issmtp(); // set mailer to use smtp $mail->host = 'smtp.qq.com'; // specify main and backup smtp servers $mail->smtpauth = true; // enable smtp authentication $mail->username = 'xxx@qq.com'; // smtp username $mail->password = 'xxxx'; // smtp password qq邮箱授权码 $mail->smtpsecure = 'tls'; // enable tls encryption, `ssl` also accepted $mail->port = 587; // tcp port to connect to //recipients $mail->setfrom('xxx@qq.com', 'mailer'); $mail->addaddress('xxx@qq.com', 'joe user'); // add a recipient $mail->addaddress('xxx@qq.com'); // name is optional $mail->addreplyto('xxx@qq.com', 'information'); $mail->addcc('xxx@qq.com'); $mail->addbcc('xxx@qq.com'); //attachments //$mail->addattachment('/var/tmp/file.tar.gz'); // add attachments //$mail->addattachment('/tmp/image.jpg', 'new.jpg'); // optional name //content $mail->ishtml(true); // set email format to html $mail->subject = 'email title'; $mail->body = 'email body'; $mail->altbody = 'this is the body in plain text for non-html mail clients'; $mail->send(); echo 'message has been sent';} catch (exception $e) { echo 'message could not be sent. mailer error: ', $mail->errorinfo;}
qq邮箱授权码获取方式如下图:
相关文章推荐:
thinkphp框架中视图的讲解(附代码)
php+redis+mysq如何l处理高并发(实例代码)
如何使用php获取来访者的ip地址(代码)
以上就是php使用phpmailer如何发送邮件(附代码)的详细内容。
