php5.5使用PHPMailer-5.2發(fā)送郵件的完整步驟
前言
這幾天一直被郵件發(fā)送功能搞得頭大,作為一個(gè)小白,遇到坑總是難免的。今天終于把phpmailer搞定了,下面就來總結(jié)一下
PHPMailer - A full-featured email creation and transfer class for PHP。
在PHP環(huán)境中可以使用PHPMailer來創(chuàng)建和發(fā)送郵件。
最新版本(20181012)是PHPMailer 6.0.5,這個(gè)無法兼容php5.5以下的環(huán)境。由于我需要維護(hù)php5.3的項(xiàng)目,需要切換到PHPMailer5.2來發(fā)送郵件。
下載地址: https://github.com/PHPMailer/PHPMailer/releases/tag/v5.2.24
下面話不多說了,來一起看看詳細(xì)的介紹吧
基本使用
下載解壓后。新建一個(gè)測試demo。
<?php require 'PHPMailerAutoload.php'; $mail = new PHPMailer; $mail->SMTPDebug = 3; // Enable verbose debug output $mail->isSMTP(); // Set mailer to use SMTP $mail->Host = 'smtp.exmail.qq.com'; // Specify main and backup SMTP servers $mail->SMTPAuth = true; // Enable SMTP authentication $mail->Username = 'xxx@qq.com'; // SMTP username $mail->Password = 'yourpassword'; // SMTP password $mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted $mail->Port = 465; // TCP port to connect to $mail->setFrom('fromWho@qq.com', 'Mailer'); $mail->addAddress('toWhom@qq.com', 'Ryan Miao'); // Add a recipient $mail->addAddress('ellen@example.com'); // Name is optional // $mail->addReplyTo('info@example.com', 'Information'); $mail->addCC('cc@example.com'); $mail->addBCC('bcc@example.com'); $mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name $mail->isHTML(true); // Set email format to HTML $mail->Subject = 'Here is the subject'; $mail->Body = 'This is the HTML message body <b>in bold!</b>'; $mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; if(!$mail->send()) { echo 'Message could not be sent.'; echo 'Mailer Error: ' . $mail->ErrorInfo; } else { echo 'Message has been sent'; }
開啟SMTPDebug可以查看日志
`0` No output
`1` Commands
`2` Data and commands
`3` As 2 plus connection status
`4` Low-level data output
錯(cuò)誤信息保存在 $mail->ErrorInfo
對(duì)象中。
保存為mail.php, 命令行執(zhí)行
php mail.php
即可看到日志,以及郵件發(fā)送成功。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
學(xué)習(xí)php設(shè)計(jì)模式 php實(shí)現(xiàn)建造者模式
這篇文章主要介紹了php設(shè)計(jì)模式中的建造者模式,使用php實(shí)現(xiàn)建造者模式,感興趣的小伙伴們可以參考一下2015-12-12幾個(gè)實(shí)用的PHP內(nèi)置函數(shù)使用指南
本文給大家推薦了7個(gè)不經(jīng)常被用到,但實(shí)際很實(shí)用,功能很強(qiáng)大的php內(nèi)置函數(shù),用好了,可以省去小伙伴們很多的時(shí)間的。2014-11-11php使用自定義函數(shù)實(shí)現(xiàn)漢字分割替換功能示例
這篇文章主要介紹了php使用自定義函數(shù)實(shí)現(xiàn)漢字分割替換功能,結(jié)合實(shí)例形式分析了php針對(duì)漢字的遍歷、轉(zhuǎn)換與分割操作相關(guān)技巧,需要的朋友可以參考下2017-01-01PHP實(shí)現(xiàn)字符串大小寫轉(zhuǎn)函數(shù)的功能實(shí)例
這篇文章主要給大家介紹了關(guān)于利用PHP如何實(shí)現(xiàn)字符串大小寫轉(zhuǎn)函數(shù)功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友一起來看看啊2019-02-02PHP動(dòng)態(tài)創(chuàng)建Web站點(diǎn)的方法
在這一篇中我寫了一些動(dòng)態(tài)創(chuàng)建Web站點(diǎn)的一些內(nèi)容,例如黏性表單、發(fā)送電子郵件、日期函數(shù)等。希望能對(duì)大家有所幫助,別忘了好評(píng)哦。2011-08-08