谷歌smtp服务器使用PHPMailer来发送邮件,由于有个国外项目需要使用到邮件服务器来发送服务器,自己搭建邮件服务器的成本太大了,于是就想到gmail ,使用gmail的邮件服务器来发送邮件。
第一步:设置gmail邮箱账号,登录进google.com谷歌账号(需要翻墙) ,进入邮箱服务器,点击右上角的设置,然后选择转发和 POP/IMAP,启用POP和IMAP这两项的功能.
第二步:降低谷歌账号的安全性访问:进入账号链接:https://www.google.com/settings/security/lesssecureapps , 设置“启用”按钮会自动保存,这个是为了降低谷歌的安全检查措施。 如果被停用了需要更改的安全设备性质才能保存。
第三步:设置PHPMailer 里代码, PHPMailer最新代码可以在github(https://github.com/PHPMailer)里下面,自行搜索下就可以了
/** * This example shows settings to use when sending via Google's Gmail servers. */ //SMTP needs accurate times, and the PHP time zone MUST be set //This should be done in your php.ini, but this is how to do it if you don't have access to that date_default_timezone_set('Etc/UTC'); require 'PHPMailerAutoload.php'; //Create a new PHPMailer instance $mail = new PHPMailer; //Tell PHPMailer to use SMTP $mail->isSMTP(); //Enable SMTP debugging // 0 = off (for production use) // 1 = client messages // 2 = client and server messages $mail->SMTPDebug = 2; //这里是调试模式,发布版的时候记得设置为0关闭调试 //Ask for HTML-friendly debug output $mail->Debugoutput = 'html'; //Set the hostname of the mail server $mail->Host = 'smtp.gmail.com'; // use // $mail->Host = gethostbyname('smtp.gmail.com'); // if your network does not support SMTP over IPv6 //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission $mail->Port = 465; //587端口是使用tsl 访问的时候使用,ssl使用的是465端口 //Set the encryption system to use - ssl (deprecated) or tls $mail->SMTPSecure = 'ssl'; //Whether to use SMTP authentication $mail->SMTPAuth = true; //Username to use for SMTP authentication - use full email address for gmail $mail->Username = "xxx@gmail.com";//账号 //Password to use for SMTP authentication $mail->Password = "xxxx——xxx";//密码 //Set who the message is to be sent from $mail->setFrom('xxxx@gmail.com', 'First Last'); //发送的邮箱和用户名 //Set an alternative reply-to address $mail->addReplyTo('xxx@qq.com', 'First Last');//接受者的邮箱和用户名 //Set who the message is to be sent to $mail->addAddress('xxxx@qq.com', 'John Doe'); //Set the subject line $mail->Subject = 'PHPMailer GMail SMTP test'; //设置邮箱的标题 //Read an HTML message body from an external file, convert referenced images to embedded, //convert HTML into a basic plain-text alternative body $mail->msgHTML('Read an HTML message body from an external file', ''); //Replace the plain text body with one created manually $mail->AltBody = 'This is a plain-text message body'; //设置邮箱的内容 //Attach an image file //$mail->addAttachment('images/phpmailer_mini.png'); //邮件的附件 //send the message, check for errors if (!$mail->send()) { echo "Mailer Error: " . $mail->ErrorInfo; } else { echo "Message sent!"; }
最后都可以正常发送成功了。
常见问题:SMTP Error: Could not authenticate.
SERVER -> CLIENT: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbtS534-5.7.14 YickOWIdF8q5K7jQq5P7kIzO2h33RlLIxn3txsgk16ySaWe6Dj-YL0v9oos7IpUl8mK-He534-5.7.14 pabCsnluFCZvmIU3cPcNeIVLhJxcMY8u1TeDMt1qJOBZvPjsce54XY-OZGaMq6NR-sx42L534-5.7.14 k-DjsBznESNTRVXqtymeKkFwwI0lpo_aLrav9PyILKArZoR8g8lpiZ2BB52PecQCmNDTF8534-5.7.14 yetKj2j8YaHAuXEnyii8C9BDoLFYY> Please log in via your web browser and534-5.7.14 then try again.534-5.7.14 Learn more at534 5.7.14 https://support.google.com/mail/answer/78754 c28sm43682609pfj.77 - gsmtp
SMTP ERROR: Password command failed: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbtS534-5.7.14 YickOWIdF8q5K7jQq5P7kIzO2h33RlLIxn3txsgk16ySaWe6Dj-YL0v9oos7IpUl8mK-He534-5.7.14 pabCsnluFCZvmIU3cPcNeIVLhJxcMY8u1TeDMt1qJOBZvPjsce54XY-OZGaMq6NR-sx42L534-5.7.14 k-DjsBznESNTRVXqtymeKkFwwI0lpo_aLrav9PyILKArZoR8g8lpiZ2BB52PecQCmNDTF8534-5.7.14 yetKj2j8YaHAuXEnyii8C9BDoLFYY> Please log in via your web browser and534-5.7.14 then try again.534-5.7.14 Learn more at534 5.7.14 https://support.google.com/mail/answer/78754 c28sm43682609pfj.77 - gsmtp
SMTP Error: Could not authenticate.
CLIENT -> SERVER: QUIT
SERVER -> CLIENT: 221 2.0.0 closing connection c28sm43682609pfj.77 - gsmtp
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
解决方法:这个是由于之前登陆过,谷歌应该是有缓存的原因,需要刷新下登陆信息,进入链接:https://accounts.google.com/b/0/DisplayUnlockCaptcha 重启刷新下登陆,然后再放我就可以正常使用了