{"id":130,"date":"2006-07-11T23:49:30","date_gmt":"2006-07-11T15:49:30","guid":{"rendered":"http:\/\/www.zengz.com\/?p=130"},"modified":"2006-07-11T23:49:30","modified_gmt":"2006-07-11T15:49:30","slug":"phpmailer%e4%bd%bf%e7%94%a8gmail-smtp%e7%9a%84%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"http:\/\/www.zengz.com\/?p=130","title":{"rendered":"phpmailer\u4f7f\u7528gmail SMTP\u7684\u65b9\u6cd5"},"content":{"rendered":"<blockquote data-secret=\"k6qfL3iljP\" class=\"wp-embedded-content\"><p><a href=\"http:\/\/www.fwolf.com\/blog\/post\/155\">\u7ec8\u4e8e\u80fd\u591f\u901a\u8fc7phpmailer\u4f7f\u7528gmail\u8d26\u53f7\u53d1\u9001\u90ae\u4ef6\u4e86<\/a><\/p><\/blockquote>\n<p><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" src=\"http:\/\/www.fwolf.com\/blog\/post\/155\/embed#?secret=k6qfL3iljP\" data-secret=\"k6qfL3iljP\" width=\"600\" height=\"338\" title=\"&#8220;\u7ec8\u4e8e\u80fd\u591f\u901a\u8fc7phpmailer\u4f7f\u7528gmail\u8d26\u53f7\u53d1\u9001\u90ae\u4ef6\u4e86&#8221; &#8212; Fwolf&#039;s Blog\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/p>\n<p>\u7ec8\u4e8e\u80fd\u591f\u901a\u8fc7phpmailer\u4f7f\u7528gmail\u8d26\u53f7\u53d1\u9001\u90ae\u4ef6\u4e86<br \/>\nphpmailer\uff08\u73b0\u5728\u7684\u7248\u672c\u662f1.73\uff09\u662f\u4e00\u4e2a\u5f88\u597d\u7528\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5f88\u65b9\u4fbf\u7684\u4f7f\u7528php\u8bed\u8a00\u53d1\u9001\u90ae\u4ef6\uff0c\u652f\u6301smtp\u53ca\u9a8c\u8bc1\uff0c\u6211\u4eec\u4e00\u76f4\u90fd\u7528\u5b83\u3002<\/p>\n<p>\u4f46\u662f\uff0c\u7531\u4e8egmail\u7684smtp\u91c7\u7528\u4e86ssl\u8fde\u63a5\uff1a<\/p>\n<p>Outgoing Mail (SMTP) Server &#8211; requires TLS: smtp.gmail.com (use authentication)<br \/>\nUse Authentication: Yes<br \/>\nUse STARTTLS: Yes (some clients call this SSL)<br \/>\nPort: 465 or 587<br \/>\n\u4f7f\u7528phpmailer\u5c31\u65e0\u6cd5\u6b63\u5e38\u8fde\u63a5gmail\u7684\u53d1\u4fe1\u670d\u52a1\u5668\u4e86\uff0c\u5e76\u4e14\u8fd9\u4e2a\u95ee\u9898\u4e00\u76f4\u6ca1\u6709\u5f97\u5230phpmailer\u7684\u5b98\u65b9\u89e3\u51b3\uff0c\u4e0d\u8fc7\u5728sf.net\u4e0a\u9762\u7684\u8ba8\u8bba\u91cc\u5012\u662f\u627e\u5230\u4e86\u4e00\u70b9\u8d44\u6599\uff0c\u91c7\u7528\u4e0b\u9762\u8fd9\u79cd\u65b9\u6cd5\u5c31\u53ef\u4ee5\u8fde\u63a5gmail\u4e86\u3002<br \/>\n<!--more--><br \/>\n\u4fee\u6539class.smtp.php\uff0c\u7b2c101\u884c\uff0c\u628a<\/p>\n<p>$this-&gt;smtp_conn = fsockopen($host, # the host of the server<br \/>\n\u6539\u6210<\/p>\n<p>$this-&gt;smtp_conn = fsockopen(\u2019ssl:\/\/\u2019 . $host, # the host of the server<br \/>\n\u8fd9\u6837\u5c31\u53ef\u4ee5\u4e86\uff0c\u5c31\u662f\u5236\u5b9a\u4f7f\u7528ssl\u534f\u8bae\u8fde\u63a5\u4e3b\u673a\uff0c\u5f53\u7136php\u7684openssl\u6a21\u5757\u5fc5\u987b\u6253\u5f00\uff1a<\/p>\n<p>extension=php_openssl.dll<br \/>\n\u4f46\u662f\u8fd9\u6837\u5c31\u4e0d\u80fd\u4f7f\u7528\u975essl\u7684\u4e3b\u673a\u4e86\uff0c\u6211\u4e5f\u4e0d\u50cf\u5728include\u76ee\u5f55\u4e0b\u9762\u653e\u4e24\u4efdphpmailer\uff0c\u4e8e\u662f\uff0c\u53c8\u7422\u78e8\u51fa\u4e86\u4e00\u79cd\u66f4\u597d\u7684\u65b9\u5f0f\uff1a<\/p>\n<p>\u6253\u5f00class.phpmailer.php\uff0c\u5728\u5927\u6982543\u884c\u9644\u8fd1\uff0c\u51fd\u6570SmtpConnect()\u4e2d\uff0c\u627e\u5230\uff1a<\/p>\n<p>$this-&gt;smtp-&gt;do_debug = $this-&gt;SMTPDebug;<br \/>\n$hosts = explode(\u201d;\u201d, $this-&gt;Host);<br \/>\n$index = 0;<br \/>\n$connection = ($this-&gt;smtp-&gt;Connected());<\/p>\n<p>\/\/ Retry while there is no connection<br \/>\nwhile($index Port;<br \/>\n}<\/p>\n<p>\u8fd9\u4e00\u6bb5\u7684\u90e8\u5206\u529f\u80fd\u5c31\u662f\uff0c\u5982\u679c\u4f60\u8f93\u5165\u7684\u4e3b\u673a\u540d\u4e2d\u5e26\u6709\u7aef\u53e3\u53f7\uff0c\u81ea\u52a8\u7684\u8bc6\u522b\u51fa\u6765\uff0c\u8bbe\u60f3\u867d\u597d\uff0c\u4f46\u5c31\u662f\u8fd9\u90e8\u5206\u8ba9\u6211\u4eec\u65e0\u6cd5\u76f4\u63a5\u5728\u8c03\u7528\u7684\u65f6\u5019\u4f7f\u7528ssl\u683c\u5f0f\u7684\u4e3b\u673a\u540d\uff0c\u56e0\u4e3a\u201cssl:\/\/xxx\u201d\u7684\u5f62\u5f0f\u4f1a\u88ab\u8bef\u8ba4\u4e3a\u662f\u4e3b\u673a\uff1a\u7aef\u53e3\u53f7\u7684\u5f62\u5f0f\uff0c\u53e6\u5916\u7aef\u53e3\u53f7\u4e00\u822c\u90fd\u6bd4\u8f83\u56fa\u5b9a\uff0c\u6211\u4eec\u624b\u5de5\u8bbe\u7f6e\u597d\u4e86\uff0c\u4e5f\u4e0d\u5fc5\u4e00\u5b9a\u8981\u548c\u4e3b\u673a\u4e00\u8d77\u8d4b\u503c\uff0c\u6240\u4ee5\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u540e\u9762\u6dfb\u52a0\uff1a<\/p>\n<p>\/\/Modify by Fwolf @ 2006-4-14, to enable ssl mail connection<br \/>\n$host = $this-&gt;Host;<br \/>\n$port = $this-&gt;Port;<br \/>\n\u5c31\u53ef\u4ee5\u4e86\uff0c\u4f7f\u7528\u6b63\u5e38smtp\u4e3b\u673a\u7684\u65f6\u5019\uff0c\u7528\u6cd5\u4e0d\u53d8\uff0c\u4f7f\u7528gmail\u7684\u65f6\u5019\uff0c\u4f7f\u7528ssl:\/\/smtp.gmail.com\u4f5c\u4e3a\u4e3b\u673a\u540d\u5c31\u53ef\u4ee5\u4e86\uff0c\u552f\u4e00\u7a0d\u5fae\u9ebb\u70e6\u4e00\u4e9b\u7684\u5c31\u662f\u7aef\u53e3\u9700\u8981\u624b\u5de5\u6307\u5b9a\u2014\u2014\u5176\u5b9e\u4e5f\u4e0d\u9ebb\u70e6\u3002<\/p>\n<p>\u6309\u7167\u4e0a\u9762\u7684\u914d\u7f6e\u66f4\u6539\u540e\uff0c\u4f7f\u7528gmail\u8d26\u53f7\u53d1\u9001\u90ae\u4ef6\u65f6\u8fd8\u4f1a\u6709\u4e00\u6761\u8b66\u544a\u4fe1\u606f\uff1a<\/p>\n<p>Warning: fgets(): SSL: fatal protocol error in H:\\php_includes\\phpmailer_ssl\\cla<br \/>\nss.smtp.php on line 1024<br \/>\n\u8fd9\u5404\u8b66\u544a\u4fe1\u606f\u5728php\u7684\u5e2e\u52a9\u91cc\u9762\u6709\uff0c\u597d\u50cf\u662f\u5728\u4f7f\u7528ssl\u8fde\u63a5\u7684\u65f6\u5019\uff0c\u8bfb\u6587\u4ef6\u8bfb\u5230\u6587\u4ef6\u5c3e\u7684\u65f6\u5019\u51fa\u73b0\u7684\u95ee\u9898\uff0c\u9700\u8981\u624b\u5de5\u5c4f\u853d\uff0c\u6216\u8005\u4eba\u4e3a\u63a7\u5236\u8bfb\u7684\u957f\u5ea6\uff0c\u6211\u4eec\u7528\u6700\u7b80\u5355\u7684\u65b9\u5f0f\u7981\u6b62\u8b66\u544a\u4fe1\u606f\u663e\u793a\u5c31\u53ef\u4ee5\u4e86\uff0c\u627e\u5230class.smtp.php\u76841024\u884c\uff0c\u5728fgets($this-&gt;smtp_conn,515)\u51fd\u6570\u524d\u9762\u52a0\u4e0a\u4e2a@\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u4f7f\u7528phpmailer\u53d1\u9001\u90ae\u4ef6\u7684\u51fd\u6570\uff1a<\/p>\n<p>function send_mail($to_address, $to_name ,$subject, $body, $attach = \u2018\u2019)<br \/>\n{<br \/>\n\/\/\u4f7f\u7528phpmailer\u53d1\u9001\u90ae\u4ef6<br \/>\nrequire_once(\u201dphpmailer\/class.phpmailer.php\u201d);<br \/>\n$mail = new PHPMailer();<br \/>\n$mail-&gt;IsSMTP(); \/\/ set mailer to use SMTP<br \/>\n$mail-&gt;CharSet = \u2018utf-8\u2032;<br \/>\n$mail-&gt;Encoding = \u2018base64\u2032;<br \/>\n$mail-&gt;From = \u2018fwolf.mailagent@gmail.com\u2019;<br \/>\n$mail-&gt;FromName = \u2018Fwolf\u2019;<br \/>\n\/\/$mail-&gt;Sender = \u2018fwolf.mailagent@gmail.com\u2019;<br \/>\n\/\/$mail-&gt;ConfirmReadingTo = \u2018fwolf.mailagent@gmail.com\u2019; \/\/\u56de\u6267\uff1f<\/p>\n<p>$mail-&gt;Host = \u2019ssl:\/\/smtp.gmail.com\u2019;<br \/>\n$mail-&gt;Port = 465; \/\/default is 25, gmail is 465 or 587<br \/>\n$mail-&gt;SMTPAuth = true;<br \/>\n$mail-&gt;Username = \u201cfwolf.mailagent@gmail.com\u201d;<br \/>\n$mail-&gt;Password = \u201cxxx\u201d;<\/p>\n<p>$mail-&gt;&gt;ddAddress($to_address, $to_name);<br \/>\n\/\/$mail-&gt;AddReplyTo(\u2019fwolf.aide@gmail.com\u2019, \u201cFwolf\u201d); \/\/\u9488\u5bf9gmail\u65e0\u7528\uff0cgmail\u662fIn-Reply-To:\uff0cphpmailer\u9ed8\u8ba4\u751f\u6210\u7684\u662fReply-to:<br \/>\n$mail-&gt;WordWrap = 50;<br \/>\nif (!empty($attach))<br \/>\n$mail-&gt;AddAttachment($attach);<br \/>\n$mail-&gt;IsHTML(false);<br \/>\n$mail-&gt;Subject = $subject;<br \/>\n$mail-&gt;Body = $body;<br \/>\n\/\/$mail-&gt;AltBody = \u201cThis is the body in plain text for non-HTML mail clients\u201d;<\/p>\n<p>if(!$mail-&gt;Send())<br \/>\n{<br \/>\necho \u201cMail send failed.\\r\\n\u201d;<br \/>\necho \u201cError message: \u201d . $mail-&gt;ErrorInfo . \u201c\\r\\n\u201d;<br \/>\nreturn false;<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\necho(\u201dSend $attach to $to_name  successed.\\r\\n\u201d);<br \/>\nreturn true;<br \/>\n}<br \/>\n\/\/echo \u201cMessage has been sent\u201d;<br \/>\n\/\/<br \/>\n}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7ec8\u4e8e\u80fd\u591f\u901a\u8fc7phpmailer\u4f7f\u7528gmail\u8d26\u53f7\u53d1\u9001\u90ae\u4ef6\u4e86 \u7ec8\u4e8e\u80fd\u591f\u901a\u8fc7phpmailer\u4f7f\u7528gmail\u8d26\u53f7\u53d1 &hellip; <a href=\"http:\/\/www.zengz.com\/?p=130\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201cphpmailer\u4f7f\u7528gmail SMTP\u7684\u65b9\u6cd5\u201d<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-130","post","type-post","status-publish","format-standard","hentry","category-1"],"_links":{"self":[{"href":"http:\/\/www.zengz.com\/index.php?rest_route=\/wp\/v2\/posts\/130","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.zengz.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.zengz.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.zengz.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.zengz.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=130"}],"version-history":[{"count":0,"href":"http:\/\/www.zengz.com\/index.php?rest_route=\/wp\/v2\/posts\/130\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.zengz.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.zengz.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=130"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.zengz.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}